C # How to list DC in another domain

I would like to list all domain controllers in a domain (where the current user or computer is a member, and also not in the same forest).

I found this good method

Domain domain = Domain.GetCurrentDomain();
foreach (DomainController dc in domain.DomainControllers)


However, I couldn't figure out how to make the remote connection get the correct domain context.

Connection via

DirectoryEntry child = new DirectoryEntry("LDAP://" + server + "/" + objectDn, userName, password);


works good. But I have no idea how to do this together.


source to share

1 answer

using System.DirectoryServices.ActiveDirectory;



DirectoryContext dc = new DirectoryContext(DirectoryContextType.DirectoryServer, "ip", "user", "pwd"); //change parameters here
Forest forest = Forest.GetForest(dc);    


above works for me (gives correct number of domains)

I am testing with a network administrator account. Hope it helps



All Articles