是否可以使用过期密码验证Active Directory用户?

时间:2011-04-07 12:02:06

标签: vb.net active-directory

我有一个使用AD对用户进行身份验证的Web表单。我希望能够使用过期密码对用户进行身份验证,并在身份验证后将其重定向到密码更改页面。

例如,如果网站管理员重置用户密码,我会使用下面的方法,让用户在下次登录时重置密码。

Public Shared Sub ForceUserToResetPassword(ByVal LDAP_URI As String, ByVal UserName As String, ByVal       Auth_UserName As String, ByVal Auth_Password As String)
    Dim LDAPEntry As DirectoryEntry = Nothing
    Try
        LDAPEntry = New DirectoryEntry(LDAP_URI, Auth_UserName, Auth_Password, AuthenticationTypes.Secure)
        Dim LDAPSearch As New DirectorySearcher()
        LDAPSearch.SearchRoot = LDAPEntry
        LDAPSearch.Filter = "(&(objectClass=user)(sAMAccountName=" & UserName & "))"


        LDAPSearch.SearchScope = SearchScope.Subtree
        Dim results As SearchResult = LDAPSearch.FindOne()
        If Not (results Is Nothing) Then
            LDAPEntry = New DirectoryEntry(results.Path, Auth_UserName, Auth_Password, AuthenticationTypes.Secure)
        End If

        LDAPAccess.SetProperty(LDAPEntry, "pwdLastSet", 0)
        LDAPEntry.CommitChanges()

    Catch ex As Exception

    End Try
End Sub

这样做会使用户的密码过期。如果用户尝试使用新密码登录,则身份验证将失败,并显示“登录失败:未知用户名或密码错误”。

这是我的身份证明。方法:

Public Shared Function AuthADuser(ByVal LDAP_URI As String, ByVal UserName As String, ByVal password As String, ByVal Auth_UserName As String, ByVal Auth_Password As String) As Boolean
    Dim IsAuth As Boolean = False
    Dim LDAPEntry As DirectoryEntry = Nothing
    Try
        LDAPEntry = New DirectoryEntry(LDAP_URI, UserName, password, AuthenticationTypes.Secure)
        Dim tmp As [Object] = LDAPEntry.NativeObject
        IsAuth = True
    Catch ex As Exception
        LDAPEntry.Dispose()
        If ex.Message.StartsWith("The server is not operational") Then
            IsAuth = False
        ElseIf ex.Message.StartsWith("Logon failure:") Then
            Throw New ApplicationException("The Username and password combination are not valid to enter the system.")
        End If
    Finally
        LDAPEntry.Close()
    End Try
    Return IsAuth
End Function

有解决方法吗?

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

据我了解,如果用户需要在下次登录时更改密码(用户密码已过期),Active-Directory将不允许我们使用LDAP来确定他的密码是否无效。这是因为用户必须更改密码。我找到了here以下解决方案:

要确定密码是否过期,您可以调用Win32:LogonUser(),并检查以下2个常量的Windows错误代码:

ERROR_PASSWORD_MUST_CHANGE = 1907
ERROR_PASSWORD_EXPIRED = 1330

我有非官方回答。作为管理员,您为pwdLastSet设置为0的用户将pwdLastSet设置为-1。这样做的效果是使Active-Directory认为密码刚刚更改。然后,使用AuthADuser方法检查密码。然后你把pwdLastSet放回到0.我不测试它,但想象一下,它在安全观点上并不那么干净(在法国我们称之为“ bricolage ”)

告诉我它是否有效?

我希望它有所帮助;

JP