网络身份验证从WMI运行exe文件时, [英] Network Authentication when running exe from WMI

查看:211
本文介绍了网络身份验证从WMI运行exe文件时,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要使用WMI运行并访问网络共享一个C#exe文件。然而,当我访问该共享我得到一个UnauthorizedAccessException。如果我运行exe文件直接共享访问。我使用的是相同的用户帐户在两种情况下。

有两个部分我的应用程序,本地PC和一个远程PC上运行的后台程序上运行的GUI客户端。当客户端需要连接到后端首先启动使用WMI(低于code转载)远程进程。远程进程做了很多事情,包括访问使用Directory.GetDirectories()和报告网络共享返回给客户端。

在远程过程是使用WMI客户端会自动启动,它无法访问网络共享。不过,如果我连接到远程计算机上使用远程桌面,手动启动后台进程,访问网络共享成功。

specifed在WMI调用以及用户的用户登录远程桌面会话都是一样的,所以权限应该是一样的,不应该他们?

我在MSDN条目<一见href="http://msdn.microsoft.com/en-us/library/system.io.directory.exists.aspx">Directory.Exists()它指出存在方法不执行网络验证。如果查询现有的网络共享没有被pre-认证,则存在方法将返回false。我认为这是相关的?正确的是我如何能保证用户身份验证的WMI会议?

  ConnectionOptions选择采用=新ConnectionOptions();

opts.Username =用户名;
opts.Password =密码;

ManagementPath路径=新ManagementPath(的String.Format(\\\\ {0} \\ \\根CIMV2:Win32_Process的,REMOTEHOST));

管理范围范围=新的管理范围(道路,选择采用);

scope.Connect();

ObjectGetOptions getopts的=新ObjectGetOptions();
使用(ManagementClass mngClass =新ManagementClass(范围,路径,getopts的))
{
    ManagementBaseObject inParams = mngClass.GetMethodParameters(创建);
    inParams [的CommandLine] =命令行;
    ManagementBaseObject outParams = mngClass.InvokeMethod(创建,inParams,NULL);
}
 

解决方案

已经按照上述(感谢)由Isalamon建议的链接我也跟着Jestro的建议,并使用psexec.exe(可从的 http://technet.microsoft.com/en-us/sysinternals/bb897553.aspx )的WMI代替。这感觉就像一个有点杂牌组装电脑做这种方式,但它似乎工作。

新的code的人谁遇到类似的问题:

 过程PROC =新工艺();
proc.StartInfo.FileName =PsExec.exe;
proc.StartInfo.Arguments =的String.Format(\\\\ {0} -d -u {1} \\ {2} -p {3} {4},
                                         REMOTEHOST,
                                         域,
                                         用户名,
                                         密码,
                                         命令行);
proc.StartInfo.CreateNoWindow = TRUE;
proc.StartInfo.UseShellExecute = FALSE;
proc.Start();
 

I have a C# exe that needs to be run using WMI and access a network share. However, when I access the share I get an UnauthorizedAccessException. If I run the exe directly the share is accessible. I am using the same user account in both cases.

There are two parts to my application, a GUI client that runs on a local PC and a backend process that runs on a remote PC. When the client needs to connect to the backend it first launches the remote process using WMI (code reproduced below). The remote process does a number of things including accessing a network share using Directory.GetDirectories() and reports back to the client.

When the remote process is launched automatically by the client using WMI, it cannot access the network share. However, if I connect to the remote machine using Remote Desktop and manually launch the backend process, access to the network share succeeds.

The user specifed in the WMI call and the user logged in for the Remote Desktop session are the same, so the permissions should be the same, shouldn't they?

I see in the MSDN entry for Directory.Exists() it states "The Exists method does not perform network authentication. If you query an existing network share without being pre-authenticated, the Exists method will return false." I assume this is related? How can I ensure the user is authenticated correctly in a WMI session?

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);
}

解决方案

Having followed the link suggested by Isalamon above (thanks) I followed Jestro's advice and have rewritten using psexec.exe (which can be downloaded from http://technet.microsoft.com/en-us/sysinternals/bb897553.aspx) instead of WMI. It feels like a bit of a kludge to do it this way, but it seems to work.

New code for anyone who is experiencing similar problems:

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();

这篇关于网络身份验证从WMI运行exe文件时,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆