c# – 从WMI运行exe时的网络认证

我有一个需要使用WMI运行并访问网络共享的C#exe.但是,当我访问该共享时,我得到一个UnauthorizedAccessException.如果我直接运行exe,可以访问该共享.在这两种情况下,我都使用相同的用户帐户.

我的应用程序有两个部分,一个在本地PC上运行的GUI客户端和在远程PC上运行的后端进程.当客户端需要连接到后端时,首先使用WMI启动远程进程(代码如下).远程进程执行了许多操作,包括使用Directory.GetDirectories()访问网络共享并将其报告回客户端.

当客户端使用WMI自动启动远程进程时,无法访问网络共享.但是,如果我使用远程桌面连接到远程计算机并手动启动后端进程,则访问网络共享成功.

在WMI呼叫中指定的用户和远程桌面会话登录用户是一样的,所以权限应该是一样的,不是吗?

我在MSDN条目中看到Directory.Exists(),它指出“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下载)而不是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();

相关文章

C#项目进行IIS部署过程中报错及其一般解决方案_c#iis执行语句...
微信扫码登录PC端网站应用的案例(C#)_c# 微信扫码登录
原文地址:http://msdn.microsoft.com/en-us/magazine/cc163...
前言 随着近些年微服务的流行,有越来越多的开发者和团队所采...
最近因为比较忙,好久没有写博客了,这篇主要给大家分享一下...
在多核CPU在今天和不久的将来,计算机将拥有更多的内核,Mic...