Authenticate a user to AD or AD LDS with C#

Since the .NET 3.5 framework it's very easy to validate a user to an ADAM or AD LDS user store. The following lines of code is all you need:

var pc = new PrincipalContext(ContextType.ApplicationDirectory, "adserver:50000", "o=org,c=nl", ContextOptions.Negotiate, @"domain\user", "password");
Console.WriteLine("Connected to: {0}", pc.ConnectedServer);
var validUser = pc.ValidateCredentials("userToValidate", "passwordToValidate", ContextOptions.SimpleBind);
Console.WriteLine(validUser);

Note that the useraccount MUST be enabled (msDS-UserAccountDisabled = FALSE) and that userPrincipalName should be CN value. Otherwise the validatecredentials method always will return false.

Post a comment