我在这里看到一些奇怪的行为PrincipalContext.ValidateCredentials
。该设置是父/子设置中的两个 Active Directory 域(因此我们有主域company.com
和子域development.company.com
).
当我根据主域验证凭据时,ValidateCredentials
行为符合预期,对于良好的用户/通行证对返回 true,对于其他任何情况返回 false。
但是,如果我验证子域中的用户,ValidateCredentials
对于好的用户名/密码和无效的用户都返回 true。如果我向有效用户提供无效密码,它会正确返回 false。
现在我正在解决这个问题UserPrincipal.FindByIdentity()
首先,如果用户存在,则调用ValidateCredentials
——但我想了解发生了什么事。
我看过的另一个解决方法是将用户名传递为domain\username
作为MSDN 条目ValidateCredentials states http://msdn.microsoft.com/en-us/library/system.directoryservices.accountmanagement.principalcontext.validatecredentials.aspx:
在此函数的每个版本中,用户名字符串可以是以下之一
各种不同的格式。有关可接受的完整列表
格式类型,请参阅 ADS_NAME_TYPE_ENUM 文档。
...其中列出了这种形式的用户名。但这会导致ValidateCredentials
无论我传入什么用户名和密码组合,始终返回 true。
相关代码是:
bool authenticated = false;
// Various options tried for ContextOptions, [etc] inc. explicit username/password to bind to AD with -- no luck.
using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, domain, null, ContextOptions.Negotiate, null, null))
{
log(pc.ConnectedServer + " => " + pc.UserName + " => " + pc.Name + " => " + pc.Container);
using (var user = UserPrincipal.FindByIdentity(pc, IdentityType.SamAccountName, username))
{
if (user != null)
{
log(user.DistinguishedName + "; " + user.DisplayName);
authenticated = pc.ValidateCredentials(username, password);
} else {
log("User not found");
// Debug only -- is FindByIdentity() needed. This should always return
// false, but doesn't.
authenticated = pc.ValidateCredentials(username, password);
}
}
}
return authenticated;
欢迎任何和所有(合理的)建议——我对此感到摸不着头脑,因为它违背了所有的期望。
我应该补充一点:这是我自己在我的机器上运行的,两者都是主域的成员。不过,我也尝试以子域的用户身份在我的计算机上的命令提示符中运行它(runas /user:subdomain\user cmd
)得到完全相同的结果。