Hi hope my following code would help you. you can use this function directly once you include system.directoryservices
///
<summary>
/// Following method authenticate username and password against entry in active directory (LDAP)
/// returns true if valid user else false.
/// this function need using System.DirectoryServices;
/// </summary>
/// <param name="userName">Network login id</param>
/// <param name="password">Login password</param>
/// <returns></returns>
private bool IsValidUser(string userName, string password)
{
DirectoryEntry de_root = new DirectoryEntry("LDAP://rootDSE");
DirectoryEntry my_LDAP = new DirectoryEntry("LDAP://" + de_root.Properties["defaultNamingContext"].Value.ToString(), userName, password);
DirectorySearcher ds_searcher = new DirectorySearcher(my_LDAP);
try
{
ds_searcher.SearchScope =
SearchScope.Subtree;
ds_searcher.Filter =
"(&(objectClass=User) (SAMAccountName=" + userName + "))";
SearchResult sr_result = ds_searcher.FindOne();
return true;
}
catch (DirectoryServicesCOMException ex)
{
messagebox.show("Login Exception");
return false;
}
catch(Exception ex)
{
messagebox.show("Login Exception");
return false;
}
finally
{
//Do not delete following three lines:
de_root.Dispose();
my_LDAP.Dispose();
ds_searcher.Dispose();
}
}