3

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.

Stef
  • 593
  • 3
  • 10
  • 23
  • solution already articulated in SO at http://stackoverflow.com/questions/323608/how-to-get-list-of-all-domains-in-active-directory-using-c-sharp Domain domain = Domain.GetDomain(new DirectoryContext(DirectoryContextType.Domain, "yourDomain", "username", "password")); – Krishna Mar 06 '12 at 08:53
  • Almost. But this does not allow to pass a IP-Address to where the request should be executed! It would require a Domain within the same Forest. – Stef Mar 06 '12 at 10:22
  • just change the type to directoryserver, sorry if I am missing the point of the question System.DirectoryServices.ActiveDirectory.DirectoryContext dc = new DirectoryContext(DirectoryContextType.DirectoryServer, "", "your username", "pwd"); – Krishna Mar 06 '12 at 10:26

1 Answers1

9
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

Krishna
  • 2,451
  • 1
  • 26
  • 31
  • Works great, thanks. I modified it slightly to get the domain straigt out of the domaincontext instead the forest first. – Stef Mar 06 '12 at 20:10