【发布时间】:2010-11-26 12:18:55
【问题描述】:
目前我使用以下代码针对某些 AD 对用户进行身份验证:
DirectoryEntry entry = new DirectoryEntry(_path, username, pwd);
try
{
// Bind to the native AdsObject to force authentication.
Object obj = entry.NativeObject;
DirectorySearcher search = new DirectorySearcher(entry) { Filter = "(sAMAccountName=" + username + ")" };
search.PropertiesToLoad.Add("cn");
SearchResult result = search.FindOne();
if (result == null)
{
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);
}
这非常适合根据用户名验证密码。
问题在于总是返回一般错误“登录失败:未知用户名或密码错误”。身份验证失败时。
但是,当帐户被锁定时,身份验证也可能失败。
我怎么知道它是否因为被锁定而失败?
我看到一些文章说你可以使用:
Convert.ToBoolean(entry.InvokeGet("IsAccountLocked"))
或者做类似here解释的事情
问题是,每当您尝试访问 DirectoryEntry 上的任何属性时,都会引发相同的错误。
关于如何找到身份验证失败的实际原因的任何其他建议? (帐户被锁定/密码过期等)
我连接的 AD 不一定是 Windows 服务器。
【问题讨论】:
标签: c# active-directory ldap