Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect to Active Directory with Principal Context?

Tags:

I've been at this for a while and I'm always getting:

System.DirectoryServices.AccountManagement.PrincipalServerDownException

Which I think means my connection setup(connection string) is wrong.

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

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

I've tried the following connecting 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 I can make this connection work?

PS: The ip is correct seen as I've used it to ping and it's working.

PPS: I really, really need this working ASAP if you have any suggestions at all they're all welcome.

like image 979
Vítor Martins Avatar asked Apr 13 '15 14:04

Vítor Martins


People also ask

What is a principal context?

PrincipalContext(ContextType) Initializes a new instance of the PrincipalContext class with the specified context type. PrincipalContext(ContextType, String) Initializes a new instance of the PrincipalContext class with the specified context type and name.

What is System DirectoryServices AccountManagement?

System. DirectoryServices. AccountManagement manages directory objects independent of the System.

What is C# DirectoryEntry?

The DirectoryEntry class presents a node or object in the Active Directory hierarchy. The Add method creates a request to create a new entry in the container. The Find method returns the child with the specified name. The Remove method deletes a child DirectoryEntry from this collection.


1 Answers

If you look at the documentation for the PrincipalContext constructors, it should be quite clear:

public PrincipalContext(ContextType contextType, string name)

or

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

So you basically need:

  • your context type (here: ContextType.Domain)
  • the domain name (try just the "Netbios" name, e.g. "YOURDOMAIN" - or leave NULL for "default" domain)
  • optionally a container (as an LDAP path - a "distinguished" name, full path but without any LDAP:// prefix)

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");
like image 90
marc_s Avatar answered Sep 23 '22 17:09

marc_s