“用户必须在下次登录时更改密码”时LDAP验证失败。有解决方案吗

时间:2011-04-14 14:06:34

标签: c# ldap

我在设置“用户必须在下次登录时更改密码”时遇到用户验证问题。

以下是我验证用户的方式:

Boolean ValidateUser(String userName, String password)
{
    try
    {
        var userOk = new DirectoryEntry("LDAP://<my LDAP server>", 
                                        userName, 
                                        password, 
                                        AuthenticationTypes.Secure 
                                      | AuthenticationTypes.ServerBind);
        return true;
    }
    catch (COMException ex)
    {
        if (ex.ErrorCode == -2147023570) // 0x8007052E -- Wrong user or password
            return false;
        else
            throw;
    }
}

如果设置了“必须更改密码”,则会按预期捕获COMException,但ErrorCode与密码错误相同。

有谁知道如何解决这个问题?

我需要一个返回代码,告知密码正确并且用户必须更改密码。

我不想在C#中实现Kerberos只是为了在用户必须更改密码时检查该死的标志。

3 个答案:

答案 0 :(得分:4)

经过长时间的互联网搜索,一些带有错误信息的实证工作以及通过Win32API进行的一些探索,我提出了一个迄今为止有效的解决方案。

Boolean ValidateUser(String userName, String password)
{
  try
  {
    var user = new DirectoryEntry("LDAP://<my LDAP server>", 
                    userName, 
                    password);
    var obj = user.NativeObject;
    return true;
  }
  catch (DirectoryServicesCOMException ex)
  {
    /*
     * The string " 773," was discovered empirically and it is related to the
     * ERROR_PASSWORD_MUST_CHANGE = 0x773 that is returned by the LogonUser API.
     * 
     * However this error code is not in any value field of the 
     * error message, therefore we need to check for the existence of 
     * the string in the error message.
     */
     if (ex.ExtendedErrorMessage.Contains(" 773,"))
        throw new UserMustChangePasswordException();

     return false;
  }
  catch
  {
     throw;
  }
}

答案 1 :(得分:2)

不幸的是,使用错误消息并不是一种简单的方法来验证帐户登录被拒绝的原因。因此,了解LDAP环境如何管理用户帐户非常重要。在Microsoft Active Directory中, userAccountControl 字段用于处理大多数帐户状态。以下是常见的 userAccountControl 位列表:

LDAP_UF_ACCOUNT_DISABLE = 2
LDAP_UF_HOMEDIR_REQUIRED = 8
LDAP_UF_LOCKOUT = 16
LDAP_UF_PASSWD_NOTREQD = 32
LDAP_UF_PASSWD_CANT_CHANGE = 64
LDAP_UF_ENCRYPTED_TEXT_PASSWORD_ALLOWED = 128
LDAP_UF_NORMAL_ACCOUNT = 512
LDAP_UF_INTERDOMAIN_TRUST_ACCOUNT = 2048
LDAP_UF_WORKSTATION_TRUST_ACCOUNT = 4096
LDAP_UF_SERVER_TRUST_ACCOUNT = 8192
LDAP_UF_DONT_EXPIRE_PASSWD = 65536
LDAP_UF_MNS_LOGON_ACCOUNT = 131072
LDAP_UF_SMARTCARD_REQUIRED = 262144
LDAP_UF_TRUSTED_FOR_DELEGATION = 524288
LDAP_UF_NOT_DELEGATED = 1048576
LDAP_UF_USE_DES_KEY_ONLY = 2097152
LDAP_UF_DONT_REQUIRE_PREAUTH = 4194304
LDAP_UF_PASSWORD_EXPIRED = 8388608
LDAP_UF_TRUSTED_TO_AUTHENTICATE_FOR_DELEGATION = 16777216
LDAP_UF_NO_AUTH_DATA_REQUIRED = 33554432
LDAP_UF_PARTIAL_SECRETS_ACCOUNT = 67108864

请记住,这些通常是合并的。因此,例如,如果具有普通帐户(LDAP_UF_NORMAL_ACCOUNT)的用户也被禁用(LDAP_UF_ACCOUNT_DISABLE),则LDAP字段 userAccountControl 将设置为514(因为512 + 2 = 514)

现在,回答原始问题:当用户帐户设置为&#34;密码必须更改&#34;时,AD只会将LDAP_UF_PASSWORD_EXPIRED添加到 userAccountControl 字段。所以: 普通帐户:LDAP_UF_NORMAL_ACCOUNT + LDAP_UF_PASSWORD_EXPIRED = 8389120

对于此特定实例(密码已过期),这是迄今为止最常见的值,但它不是唯一的值。在评估帐户是否设置为密码已过期时,您需要考虑所有可能的选项。

当然,这不是验证设置的最简单方法,但它是最可靠的方式。

答案 2 :(得分:1)

谢谢你。这适合我。使用this link 我在异常发生后扩大了响应,如下所示:

Catch ex As DirectoryServicesCOMException
        Dim msg As String = Nothing
        Select Case True
            Case ex.ExtendedErrorMessage.Contains("773")
                msg = "Error 773. User must change password at next logon is set.​ Please contact support."
            Case ex.ExtendedErrorMessage.Contains("525")
                msg = "User not found"
            Case ex.ExtendedErrorMessage.Contains("52e")
                msg = "Invalid credentials"
            Case ex.ExtendedErrorMessage.Contains("530")
                msg = "Not permitted to logon at this time​"
            Case ex.ExtendedErrorMessage.Contains("531")
                msg = "Not permitted to logon at this workstation​"
            Case ex.ExtendedErrorMessage.Contains("532")
                msg = "Password expired"
            Case ex.ExtendedErrorMessage.Contains("533")
                msg = "Account disabled"
            Case ex.ExtendedErrorMessage.Contains("701")
                msg = "Account expired"
            Case ex.ExtendedErrorMessage.Contains("775")
                msg = "User account is locked"
        End Select
        If msg IsNot Nothing Then
            errorLabel.Text = ex.Message & " " & msg
        Else
            errorLabel.Text = ex.Message
        End If
    End Try