问题
In my application I need to get Active Directory forest and list the domains hierarchy.
I was given the Active Directory account that has necessary permissions. However my development computer is NOT on the domain and can not be added to the domain.
Here is the code that I am using to get the forest and access the root domain:
// I also tried to add a Global Catalog port: 192.168.2.11:3268, no success
DirectoryContext dc = new DirectoryContext(
DirectoryContextType.Forest, "192.168.2.11", “Login1”, “pass1”);
Forest f = Forest.GetForest(dc);
Console.WriteLine("Forest name: " + f.Name);
Console.WriteLine("Root domain: " + f.RootDomain);
How do I modify it to discover domains in case I run it on a computer that is not on a domain?
回答1:
Even the computer is not joined to domain, you can still set its DNS server to one that can resolve the names in AD.
Ideally you can use the same DNS server that is used by the DC.
After the above change, you should be able to create the Forest
object with the forest name. Then, to get the domains, simply call the Domains
property of Forest
.
来源:https://stackoverflow.com/questions/27908447/how-to-list-domains-running-code-from-a-non-domain-computer