对于未知用户,ValidateCredentials返回true?
我在这里使用PrincipalContext.ValidateCredentials
看到一些奇怪的行为。 设置是父/子设置中的两个Active Directory域(因此我们有主域company.com
和子域development.company.com
)。
当我针对主域validation凭据时, ValidateCredentials
按预期运行,对于良好的用户/传递对返回true,对其他任何对返回false。
但是,如果我在子域中validation用户,则ValidateCredentials
将为良好的用户名/密码和无效用户返回true。 如果我向有效用户提供无效密码,则会正确返回false。
现在我首先通过UserPrincipal.FindByIdentity()
解决它,如果用户存在,那么调用ValidateCredentials
– 但我想了解发生了什么。
我看过的另一个解决方法是将用户名作为domain\username
传递给ValidateCredentials
的MSDN条目 :
在此函数的每个版本中,userName字符串可以是各种不同格式之一。 有关可接受格式类型的完整列表,请参阅ADS_NAME_TYPE_ENUM文档。
…列出了这种forms的用户名。 但这会导致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;
任何和所有(明智的)建议都欢迎 – 我正在摸不着头脑,因为它违背了所有的期望。
我应该补充一下:这是我自己在我的机器上运行的,两者都是主域的成员。 但是我也尝试在我的机器上以命令提示符的forms运行它作为子域的runas /user:subdomain\user cmd
( runas /user:subdomain\user cmd
),结果完全相同。
之后有一些谷歌搜索(不是我整天都在谷歌里试图找到这个),我找到了答案 。
简而言之,如果在域中启用了Guest帐户,则ValidateCredentials将为未知用户返回TRUE。 我刚刚在development.company.com上检查了来宾用户的状态,确定该帐户已启用。 如果我禁用了来宾帐户,则ValidateCredentials会正确返回false。
这是一个相当基本的问题,不确定我是否热衷于这种行为……可惜它没有在MSDN上明确提到过。
它可能与此有关:
ValidateCredentials方法绑定到构造函数中指定的服务器。 如果username和password参数为null,则validation构造函数中指定的凭据。 如果构造函数中未指定凭据,并且username和password参数为null,则此方法将validation当前主体的默认凭据 。