Using C# to authenticate user against LDAP

This username, password within this line:

DirectoryEntry("LDAP://myserver/OU=People,O=mycompany", username, password);

should be for an account that has permission for directory lookup. It could be a service account or testing purpose try with your own. This shouldn’t be the user/pass of someone who you are trying to authenticate.

If you want to authenticate, you can use following steps using PrincipalContext:

using(var context = new PrincipalContext(ContextType.Domain, "mydomain", "mydomain\serviceAcct", "serviceAcctPass")) {
 //Username and password for authentication.
 return context.ValidateCredentials(username, password); 
}

“serviceAcct” = an account within domain users that has permission for directory lookup.
“serviceAcctPass” = password for that service account.
As I said, for testing you can try with your own user/pass context.

Also, make sure supplied username has either “domain\username” or “username@domain” formatting.

Leave a Comment