我有一个 C# exe,需要使用 WMI 运行并访问网络共享。但是,当我访问共享时,我收到 UnauthorizedAccessException。如果我直接运行 exe,则可以访问共享。我在这两种情况下都使用相同的用户帐户。
我的应用程序有两个部分,一个在本地 PC 上运行的 GUI 客户端和一个在远程 PC 上运行的后端进程。当客户端需要连接到后端时,它首先使用 WMI 启动远程进程(代码复制如下)。远程进程执行许多操作,包括使用 Directory.GetDirectories() 访问网络共享并向客户端报告。
当客户端使用 WMI 自动启动远程进程时,它无法访问网络共享。但是,如果我使用远程桌面连接到远程计算机并手动启动后端进程,则对网络共享的访问会成功。
WMI 调用中指定的用户和登录远程桌面会话的用户相同,因此权限应该相同,不是吗?
我在 MSDN 条目中看到目录.Exists() http://msdn.microsoft.com/en-us/library/system.io.directory.exists.aspx它指出“Exists 方法不执行网络身份验证。如果您在未经预先身份验证的情况下查询现有网络共享,Exists 方法将返回 false。”我认为这是相关的?如何确保用户在 WMI 会话中得到正确的身份验证?
ConnectionOptions opts = new ConnectionOptions();
opts.Username = username;
opts.Password = password;
ManagementPath path = new ManagementPath(string.Format("\\\\{0}\\root\\cimv2:Win32_Process", remoteHost));
ManagementScope scope = new ManagementScope(path, opts);
scope.Connect();
ObjectGetOptions getOpts = new ObjectGetOptions();
using (ManagementClass mngClass = new ManagementClass(scope, path, getOpts))
{
ManagementBaseObject inParams = mngClass.GetMethodParameters("Create");
inParams["CommandLine"] = commandLine;
ManagementBaseObject outParams = mngClass.InvokeMethod("Create", inParams, null);
}
按照上面 Isalamon 建议的链接(谢谢)后,我按照 Jestro 的建议并使用 psexec.exe 重写(可以从http://technet.microsoft.com/en-us/sysinternals/bb897553.aspx http://technet.microsoft.com/en-us/sysinternals/bb897553.aspx) 而不是 WMI。这样做感觉有点麻烦,但似乎可行。
为遇到类似问题的人提供新代码:
Process proc = new Process();
proc.StartInfo.FileName = "PsExec.exe";
proc.StartInfo.Arguments = string.Format("\\\\{0} -d -u {1}\\{2} -p {3} {4}",
remoteHost,
domain,
username,
password,
commandLine);
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.UseShellExecute = false;
proc.Start();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)