Kat
Kat

Reputation: 493

.NET Active Directory PrincipalContext setup

I'm having difficulty setting up my authentication with active directory.

I have a LDAP server at ldap://server-1 and the domain is "DC=company,DC=com"

Following this answer, I tried to setup the scheme like:

using(PrincipalContext pc = new PrincipalContext(ContextType.Domain, "ldap://server-1", "DC=company,DC=com"))
{
    // validate the credentials
    bool isValid = pc.ValidateCredentials("myuser", "mypassword");
}

But I keep getting error The server could not be contacted / The LDAP server is unavailable. I've tried lots of variations of using the server name and the domain but I don't know much about the LDAP protocol, so don't know if I'm setting things up right.

I know the LDAP is available because I get results from cmd line dsquery DC=company,DC=com - type queries.

Upvotes: 1

Views: 1042

Answers (1)

marc_s
marc_s

Reputation: 754230

I would try this - use the server name and the dc= strings as your server name - and leave the "starting point" out (and also: make sure to use ALL UPPERCASE on your LDAP:// prefix - that's crucial!):

using(PrincipalContext pc = new PrincipalContext(ContextType.Domain, "LDAP://server-1/dc=company,dc=com"))
{
    // validate the credentials
    bool isValid = pc.ValidateCredentials("myuser", "mypassword");
}

Does that work?

Update: read my own answer to that question you linked to again, you need to supply this: a domain name (just a string; can be NULL to indicate the "default" domain) to the second parameter, and optionally a container DN to the third parameter - so try this:

using(PrincipalContext pc = new PrincipalContext(ContextType.Domain, null, "dc=company,dc=com"))
{
    // validate the credentials
    bool isValid = pc.ValidateCredentials("myuser", "mypassword");
}

Upvotes: 3

Related Questions