Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

Validate a username and password against Active Directory?

How can I validate a username and password against Active Directory? I simply want to check if a username and password are correct.

1 Answer

We do this on our Intranet

You have to use System.DirectoryServices;

Here are the guts of the code

using (DirectoryEntry adsEntry = new DirectoryEntry(path, strAccountId, strPassword))
using (DirectorySearcher adsSearcher = new DirectorySearcher(adsEntry))
//adsSearcher.Filter = "(&(objectClass=user)(objectCategory=person))";
adsSearcher.Filter = "(sAMAccountName=" + strAccountId + ")";

SearchResult adsSearchResult = adsSearcher.FindOne();
bSucceeded = true;

strAuthenticatedBy = "Active Directory";
strError = "User has been authenticated by Active Directory.";
catch (Exception ex)
// Failed to authenticate. Most likely it is caused by unknown user
// id or bad strPassword.
strError = ex.Message;

Login / Signup to Answer the Question.