PrincipalContext.ValidateCredentials对于某些用户失败?

时间:2019-05-16 10:02:42

标签: c# .net authentication active-directory

我有以下代码:

public void AuthenticateActiveDirectoryAccount(string username, string password)
    {
        PrincipalContext context;
        var envSettings = _settingsService.GetGlobalSetting<EnvironmentSettings>().Props;
        string ADServer = envSettings.ActiveDirectory.ServerURI;
        string ADUserName = envSettings.ActiveDirectory.Username;
        string ADUserPassword = envSettings.ActiveDirectory.Password;
        string account = null;

        account = username.ToLower();

        if (ADUserName.Length > 0)
            context = new PrincipalContext(ContextType.Domain, ADServer, ADUserName, ADUserPassword);
        else
            context = new PrincipalContext(ContextType.Domain, ADServer);

        using (context)
        {
            if (!context.ValidateCredentials(account, password))
            {
                throw new Exception();
            }
        }
    }

这对大多数用户来说效果很好,但有些人会遇到以下异常:

  

服务器不处理目录请求:System.DirectoryServices.Protocols.ErrorChecking.CheckAndSetLdapError(Int32错误)\ r \ n vid System.DirectoryServices.Protocols.LdapSessionOptions.FastConcurrentBind()\ r \ n vid System.DirectoryServices.AccountManagement .CredentialValidator.BindLdap(NetworkCredential凭据,ContextOptions contextOptions)\ r \ n vid System.DirectoryServices.AccountManagement.CredentialValidator.Validate(字符串userName,字符串密码)\ r \ n vid System.DirectoryServices.AccountManagement.PrincipalContext.ValidateCredentials(String字符串密码)

首先,我认为失败的用户没有调用AD的权限,但是我确定已使用应具有访问权限的全局AD帐户设置了AdServer,AdUserName和AdUserPassword。

为什么某些用户会收到此异常?

2 个答案:

答案 0 :(得分:0)

将ValidateCredentials更改为此可以解决问题:

context.ValidateCredentials(account, password, ContextOptions.Negotiate | ContextOptions.Signing | ContextOptions.Sealing)

但是,它仍然可能是Active Directory帐户安全性的问题。

答案 1 :(得分:0)

如果除工作组外没有其他域,它将与之一起使用

context.ValidateCredentials(account, password, ContextOptions.Negotiate)