How to connect to Active Directory with the main context?

I have been for a while and I always get:

System.DirectoryServices.AccountManagement.PrincipalServerDownException

I think my connection setup (connection string) is incorrect.

When I write "dsquery server" in cmd on the computer where I get Active Directory:

"CN = DCESTAGIO, CN = SERVERS, CN = Default-First-Site-Name, CN = Sites, CN = Configuration, DC = estagioit, DC = local"

I tried the following connection in the following ways:

1

PrincipalContext thisPrincipalContext = new PrincipalContext(ContextType.Domain, "192.168.56.101", "DC=estagioit,DC=local"); 

2:

 PrincipalContext thisPrincipalContext = new PrincipalContext(ContextType.Domain, "192.168.56.101/DC=estagioit,DC=local"); 

3:

 PrincipalContext thisPrincipalContext = new PrincipalContext(ContextType.Domain, "192.168.56.101/CN=DCESTAGIO,DC=estagioit,DC=local"); 

4

 PrincipalContext thisPrincipalContext = new PrincipalContext(ContextType.Domain, "192.168.56.101/CN=DCESTAGIO,CN=SERVERS,CN=Default-First-Site-Name,CN=Sites,CN=Configuration,DC=estagioit,DC=local"); 

5:

 PrincipalContext thisPrincipalContext = new PrincipalContext(ContextType.Domain, "LDAP://192.168.56.101/CN=Users,DC=estagioit,DC=local"); 

And some other ways ...

Any ideas on what's wrong and how can I make this connection work?

PS: ip correctly shows how I used it for ping, and it works.

PSS: I really really need this job as soon as possible, if you have any suggestions, they are all welcome.

+5
source share
1 answer

If you look at the documentation for PrincipalContext constructors, this should be perfectly clear:

 public PrincipalContext(ContextType contextType, string name) 

or

 public PrincipalContext(ContextType contextType, string name, string container) 

So you basically need to:

  • your context type (here: ContextType.Domain )
  • domain name (just try the name "Netbios", for example, "YOURDOMAIN", or leave NULL for the "default" domain).
  • optional container (as the LDAP path - "excellent" name, full path, but without the LDAP:// prefix LDAP:// )

So try something like this:

 PrincipalContext thisPrincipalContext = new PrincipalContext(ContextType.Domain, "ESTAGIOIT"); 

or

 PrincipalContext thisPrincipalContext = new PrincipalContext(ContextType.Domain, null); // default domain 

or

 PrincipalContext thisPrincipalContext = new PrincipalContext(ContextType.Domain, "ESTAGIOIT", "DC=estagioit,DC=local"); 

or

 PrincipalContext thisPrincipalContext = new PrincipalContext(ContextType.Domain, null, "CN=Users,DC=estagioit,DC=local"); 
+15
source

All Articles