我通过用户名和密码来检查用户是否在Active Directory中有效。
这是我的代码:
private bool ValidUser(string name, string userPwd)
{
string UserName = "XXXXXXXXXX";
string Password = "XXXXXXXXXXXXX";
DirectoryEntry objRootEntry = new DirectoryEntry("XXXXXXXX.com", UserName, Password);
DirectorySearcher objADSearcher = new DirectorySearcher(objRootEntry);
objADSearcher.Filter = ("(&(sAMAccountType=xxxxxxxxx)(samAccountName=" + name + "))");
SearchResult objResult = objADSearcher.FindOne();
DirectoryEntry objLoginEntry = (objResult != null) ? objResult.GetDirectoryEntry() : null;
if (objLoginEntry != null)
{
return true;
}
return false;
}
现在它只检查用户名。我需要检查输入密码(userPwd)是否与Active目录匹配。怎么做。
请帮帮我。
答案 0 :(得分:1)
//在目录条目中查找时输入密码就足够了。不需要再次检查
检查此详细代码
public bool ValidateUser(string domain, string username, string password,string LdapPath, out string Errmsg)
{
Errmsg = "";
string domainAndUsername = domain + @"\" + username;
DirectoryEntry entry = new DirectoryEntry(LdapPath, domainAndUsername, password);
try
{
// Bind to the native AdsObject to force authentication.
Object obj = entry.NativeObject;
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
LdapPath = result.Path;
string _filterAttribute = (String)result.Properties["cn"][0];
}
catch (Exception ex)
{
Errmsg = ex.Message;
throw new Exception("Error authenticating user." + ex.Message);
}
}