LDAP UserValidation
public bool IsAuthenticated(string domain, string username, string pwd)
{
//string adPath = "LDAP://yourCompanyName.com/DC=yourCompanyName,DC=com";
string _path = string.Empty;//"LDAP://cyb-ms.com/DC=cyb-ms,DC=com";
string _filterAttribute = string.Empty ;
string domainAndUsername = domain + @"\" + username;
DirectoryEntry entry = new DirectoryEntry(_path,domainAndUsername,pwd);
try
{
// Bind to the native AdsObject to force authentication.
Object obj = entry.NativeObject; //TODO: NO USE OF THIS LINE, can remove
DirectorySearcher search = new DirectorySearcher(entry);
search.Filter = "(SAMAccountName=" + username + ")";
search.PropertiesToLoad.Add("cn");
SearchResult result = search.FindOne();
if (null == result)
{
return false;
}
// Update the new path to the user in the directory
_path = result.Path;
_filterAttribute = (String)result.Properties["cn"][0];
}
catch (Exception ex)
{
//throw new Exception("Error authenticating user. " + ex.Message);
return false;
}
return true;
}
{
//string adPath = "LDAP://yourCompanyName.com/DC=yourCompanyName,DC=com";
string _path = string.Empty;//"LDAP://cyb-ms.com/DC=cyb-ms,DC=com";
string _filterAttribute = string.Empty ;
string domainAndUsername = domain + @"\" + username;
DirectoryEntry entry = new DirectoryEntry(_path,domainAndUsername,pwd);
try
{
// Bind to the native AdsObject to force authentication.
Object obj = entry.NativeObject; //TODO: NO USE OF THIS LINE, can remove
DirectorySearcher search = new DirectorySearcher(entry);
search.Filter = "(SAMAccountName=" + username + ")";
search.PropertiesToLoad.Add("cn");
SearchResult result = search.FindOne();
if (null == result)
{
return false;
}
// Update the new path to the user in the directory
_path = result.Path;
_filterAttribute = (String)result.Properties["cn"][0];
}
catch (Exception ex)
{
//throw new Exception("Error authenticating user. " + ex.Message);
return false;
}
return true;
}