1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| /// <summary>
/// Tries to connect LDAP user to server and tests reading the directory.
/// </summary>
/// <param name="pUserName">[in] LDAP user login.</param>
/// <param name="pPassword">[in] LDAP user password.</param>
/// <param name="pLDAPpath">[in] LDAP path.</param>
/// <param name="error">[out] Exception caught while trying to access LDAP.</param>
/// <returns><c>true</c> if succeeded, <c>false</c> if failed for any reason.</returns>
public static bool AuthenticateLDAPUser(string pUserName, string pPassword, string pLDAPpath, out Exception error)
{
try
{
DirectoryEntry de = new DirectoryEntry(pLDAPpath, pUserName, pPassword, AuthenticationTypes.ReadonlyServer|AuthenticationTypes.Secure);
DirectorySearcher searcher = new DirectorySearcher(de);
SearchResult result = searcher.FindOne();
error = null;
return (result != null); // if one element is found in the LDAP, the login/password are valid
}
catch(Exception e)
{ error = e; }
return false;
} |
Partager