Showing posts with label windows. Show all posts
Showing posts with label windows. Show all posts

Thursday, January 15, 2009

Custom Active Directory user authentication

This function helps to authenticate user in active directory. Can use provide windows authentication in a form authentication websites too.

DNS Name format: "LDAP://DNSName"

private bool ValidateUser(string dnsname,string username,string password)
{
try
{
DirectoryEntry entry = new DirectoryEntry(dnsname, username, password);
DirectorySearcher mySearcher = new DirectorySearcher(entry);
SearchResultCollection results;
mySearcher.Filter = "cn";
results = mySearcher.FindAll();
if (results == null)
{
lblError.Text = "Invalid User";
return false;
}
else
{
lblError.Text = "User Found";
return true;
}
}
catch
{
lblError.Text = "Invalid User";
return false;
}
}