Question

I am new to LDAP and wanted to connect to a LDAP server using .Net to validate the user credentials. The following code returns an error:

The LDAP server is unavailable

But the validation works fine in Java code. Kindly let me know where I have gone wrong.

using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, "LDAP://192.168.65.201:389/DC=be,DC=ndl,DC=CompanyName,DC=com"))
{
    bool a= pc.ValidateCredentials("myname@CompanyName.com","password"); 
}
Was it helpful?

Solution

First of all - PrincipalContext only works against Active Directory, not against any other LDAP server.

Secondly: you're specifying invalid parameters for the constructor. Check out the MSDN docs on what constructors are available for PrincipalContext.

You can define just a ContextType parameter, in which case the PrincipalContext is constructed against the current domain you're connected to:

var ctx = new PrincipalContext(ContextType.Domain);

Or you can use a constructor with a second string parameter which signifies the domain name of your domain (only the domain name - NOT a complete LDAP path!):

var ctx = new PrincipalContext(ContextType.Domain, "CompanyName.com");

Then you're connected to that specific domain, at the root level.

Or thirdly, you can specify a third parameter which defines the container in that domain to connect to:

var ctx = new PrincipalContext(ContextType.Domain, "CompanyName.com", 
                               "CN=Users,DC=be,DC=ndl,DC=CompanyName,DC=com");

So you'll need to find the appropriate constructor and supply the correct parameters to get this to work - if you're using Active Directory.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top