c# - ValidateCredentials 为未知用户返回 true?

标签 c# .net-4.0 active-directory

我在使用 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 传递:

In each version of this function, the userName string can be in one of a variety of different formats. For a complete list of the acceptable types of formats, see the ADS_NAME_TYPE_ENUM documentation.

...其中列出了这种形式的用户名。但这会导致 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;

欢迎任何和所有(明智的)建议——我正在为此挠头,因为它违背了所有人的期望。

我应该补充一点:这是在我的机器上以我自己的身份运行的,它们都是 primary 域的成员。但是,我也尝试以子域 (runas/user:subdomain\user cmd) 的用户身份在我机器上的命令提示符下运行它,结果完全相同。

最佳答案

稍后进行了一些谷歌搜索(并不是我整天都在谷歌上进进出出试图找到它)​​,我已经found the answer .

简单地说,如果在域中启用了 Guest 帐户,ValidateCredentials 将为未知用户返回 TRUE。我刚刚检查了 development.company.com 中 guest 用户的状态,果然该帐户已启用。如果我禁用了 guest 帐户,ValidateCredentials 会正确返回 false。

这是一个相当基本的陷阱,我不确定我是否热衷于这种行为......遗憾的是它没有在 MSDN 上明确提及。

关于c# - ValidateCredentials 为未知用户返回 true?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7336193/

相关文章:

c# - 创建结构变量 C#

c# - 如何比较不同尺寸的图像

c# - 无法访问/找到公共(public)枚举/类

java - 在一个域中进行身份验证并在 Java 中查询另一个域中的用户

java - 请检查 JNDI 领域配置

c# - 使用 C# 检查 Active Directory 中是否存在 UserID

c# - 进行 DPI 缩放的简单方法?

Silverlight 4 程序集共享问题

c# - 白鲸记的异常(exception)

c# - 无法删除 TableLayoutPanel 中控件之间的间距?