C# PrincipalContext The network path was not found

梦想与她 提交于 2021-01-27 18:35:12

问题


I am trying to use PrincipalContext to check if a local user group exists on a remote computer.

I am having problems with PrincipalContext:

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

It works in such scenarios:

  • local to local machine
  • local to virtual machine
  • domain machine to workgroup machine

However it doesn't work in opposite direction:

  • virtual machine to local host
  • workgroup machine to domain machine

I am getting these errors:

Unhandled Exception: System.IO.FileNotFoundException: The network path was not found.

Unhandled Exception: System.Runtime.InteropServices.COMException: The network path was not found.

The first exception is for virtual machine, second for workgroup machine.

All machines have user with the same name and password and the code was executed from that user.

How to solve this issue?


回答1:


I found the answer. It looks that DirectoryServices doesn't work on remote Windows 7 or newer. I guess when a computer is in a workgroup then it is local and we can connect and when it is in a domain then it is remote.

I followed steps described here:
System.IO.FileNotFoundException: The network path was not found. Exception while using DirectoryEntry object on windows 7
and here:
http://www.peppercrew.nl/index.php/2011/09/connect-to-remote-registry-fails-with-an-error-is-preventing-this-key-from-being-opened/

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

However I can't change services and registry settings on production servers. I found such way to get group:

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


来源:https://stackoverflow.com/questions/25060655/c-sharp-principalcontext-the-network-path-was-not-found

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!