【发布时间】:2011-09-07 15:09:04
【问题描述】:
我在这里使用PrincipalContext.ValidateCredentials 看到了一些奇怪的行为。该设置是父/子设置中的两个 Active Directory 域(因此我们有主域 company.com 和子域 development.company.com)。
当我针对主域验证凭据时,ValidateCredentials 的行为符合预期,对于好的用户/通行证对返回 true,对于其他任何内容返回 false。
但是,如果我验证子域中的用户,ValidateCredentials 会为正确的用户名/密码和无效用户返回 true。如果我为有效用户提供了无效密码,它会正确返回 false。
现在我正在解决这个问题,首先执行UserPrincipal.FindByIdentity(),如果用户存在,然后调用ValidateCredentials——但我想了解发生了什么。
我查看的另一种解决方法是将用户名作为domain\username 作为MSDN entry for ValidateCredentials states 传递:
在此函数的每个版本中,用户名字符串可以是以下之一 各种不同的格式。有关可接受的完整列表 格式类型,请参阅 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) 的用户身份在我的机器上的命令提示符下运行它,结果完全相同。
【问题讨论】:
-
我有两个域。我可以使用我的域的 ip 地址来调用验证凭据,如果我只是传递来自任一域的用户的用户名或密码,它就会成功。我有点惊讶我不必提供这些额外的信息,并且有点困惑我如何确定我正在验证来自正确域的正确用户。 (不可能每个人都说一个 jsmith 吗?)
标签: c# .net-4.0 active-directory