c# Howto enumerate DCs in a different domain
-
08-12-2019 - |
Question
I would like to enumerate all DCs in a domain (where current user nor computer are member of, and also not in the same forest).
I found this nice method
Domain domain = Domain.GetCurrentDomain();
foreach (DomainController dc in domain.DomainControllers)
...
However, I could not figure out how to do a remote connect with to get the right domain context.
Connecting via
DirectoryEntry child = new DirectoryEntry("LDAP://" + server + "/" + objectDn, userName, password);
works well. But I have no Idea how to get this together.
La solution
using System.DirectoryServices.ActiveDirectory;
...
....
DirectoryContext dc = new DirectoryContext(DirectoryContextType.DirectoryServer, "ip", "user", "pwd"); //change parameters here
Forest forest = Forest.GetForest(dc);
Console.WriteLine(forest.Domains.Count);
the above works for me (gives the correct domain count)
I am testing with a network admin account obviously. Hope this helps
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow