Friday, April 29, 2011

using C# how to verify active directory user accounts

Following code will help for developing active directory user account validations

1)System.DirectoryServices assembly need to add the reference for active project


2)following code using we can verify Active directory user account


//For Login buttonClick Event

private void Login_Click(object sender, EventArgs e)
{


if (AuthenticateUser("Domain Name", "User Name", "passWord"))
{
MessageBox.Show("User Logged In");
}
else
{
MessageBox.Show("Unable to Authenticate Using the Supplied Credentials");

}

}


//verfication methoed

public bool AuthenticateUser(string domainName, string userName, string password)
{
bool ret = false;

try
{
DirectoryEntry de = new DirectoryEntry("LDAP://" + domainName,
userName, password);
DirectorySearcher dsearch = new DirectorySearcher(de);
SearchResult results = null;

results = dsearch.FindOne();

ret = true;
}
catch
{
ret = false;
}

return ret;
}

happy coding.....