Validate a username and password against Active Directory?

You have to use System.DirectoryServices;

Here are the guts of the code

public static bool CheckUserInActiveDirectory(string LoginName,string Password)
{
bool _isReturn = false;
DirectoryEntry objEntry = new DirectoryEntry("LDAP://" + "Your Domain Name", LoginName, Password);
try
{
string strFilter = "(&(objectClass=user)(sAMAccountName=" + LoginName + "))";

DirectorySearcher objSearcher = new DirectorySearcher();
objSearcher.SearchRoot = objEntry;
objSearcher.PageSize = 1000;
objSearcher.Filter = strFilter;
objSearcher.PropertiesToLoad.Add("name");
SearchResultCollection colResults = objSearcher.FindAll();
if (colResults.Count > 0)
{
_isReturn = true;
}
objEntry.Close();
}
catch (Exception ex)
{
//ErrorLogger.LogError("GetUserInActiveDirectory : CheckUserInActiveDirectory", ex.Message.ToString(), EventLogEntryType.Error);
objEntry.Close();
_isReturn = false;
}
return _isReturn;
}

Happy Coding.....

No comments:

Post a Comment