2

我正在尝试使用 PrincipalContext 检查远程计算机上是否存在本地用户组。

我遇到以下问题PrincipalContext

PrincipalContext ctx = new PrincipalContext(ContextType.Machine, machine, null, ContextOptions.Negotiate)

它适用于以下场景:

  • 本地到本地机器
  • 本地到虚拟机
  • 域机器到工作组机器

但是它在相反的方向上不起作用:

  • 虚拟机到本地主机
  • 工作组机器到域机器

我收到这些错误:

未处理的异常:System.IO.FileNotFoundException:找不到网络路径。

未处理的异常:System.Runtime.InteropServices.COMException:找不到网络路径。

第一个例外是虚拟机,第二个是工作组机器。

所有机器都有相同名称和密码的用户,并且代码是从该用户执行的。

如何解决这个问题?

4

1 回答 1

1

我找到了答案。看起来 DirectoryServices 在远程 Windows 7 或更高版本上不起作用。我猜当计算机在工作组中时,它是本地的,我们可以连接,而当它在域中时,它是远程的。

我按照此处描述的步骤操作:
System.IO.FileNotFoundException:找不到网络路径。
在 Windows 7和此处使用 DirectoryEntry 对象时出现异常:
http ://www.peppercrew.nl/index.php/2011/09/connect-to-remote-registry-fails-with-an-error-is-preventing-this -钥匙从被打开/

Enable File and Print sharing in the Firewall
Start the Remote Registry Service
Add remote user access to this registry entry: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurePipeServers\winreg

但是我无法更改生产服务器上的服务和注册表设置。我找到了这样的方式来获得组:

var server = new DirectoryEntry(string.Format("WinNT://{0},Computer", machine));
DirectoryEntry group = server.Children.Cast<DirectoryEntry>().Where(
    d => d.SchemaClassName.Equals("Group") && d.Name.Equals("Administrators")
).Single<DirectoryEntry>();
于 2014-08-01T07:33:19.967 回答