Thursday, June 02, 2005

LDAP/Active Directory Authentication

Fucntion that authenticates username and password against active directory:
public bool AuthenticateLDAPUser(string LDAPServerIpAddress, string username, string password)
{
try
{
DirectoryEntry de_eForceDirectory = new DirectoryEntry("LDAP://" + LDAPServerIpAddress ,username, password);
DirectorySearcher deSearcher = new DirectorySearcher (de_eForceDirectory);
SearchResult searchResult = deSearcher.FindOne();
DirectoryEntry de_result = searchResult.GetDirectoryEntry();
if(de_result.Username==null)
{ return false; }
else
{ return true; }
}
catch (Exception ex)
{
throw ex;
}
}
Calling Code
MessageBox.Show("Authentication returned: " + AuthenticateLDAPUser("10.6.0.1", "rajiv", "password").ToString()); // Where 10.6.0.1 is your Active Directory Server.

0 Comments:

Post a Comment

<< Home