用于在VB.Net中对用户进行身份验证的LogonUser API

用于在VB.Net中对用户进行身份验证的LogonUser API,vb.net,authentication,active-directory,Vb.net,Authentication,Active Directory,使用vb.net中的LogonUser对用户进行身份验证时遇到问题。我正在使用LOGON32\u LOGON\u网络的LogonType(3) 文档指定使用此选项进行简单身份验证,并且不缓存凭据 我遇到的问题是,如果用户刚刚更改了密码,他们可以使用旧密码或新密码登录 什么LogonType适合进行简单身份验证 以下是我当前使用的代码: Public Shared Function login(ByVal domain As String, ByVal userid As String, ByVa

使用
vb.net
中的
LogonUser
对用户进行身份验证时遇到问题。我正在使用
LOGON32\u LOGON\u网络的
LogonType
(3)

文档指定使用此选项进行简单身份验证,并且不缓存凭据

我遇到的问题是,如果用户刚刚更改了密码,他们可以使用旧密码或新密码登录

什么
LogonType
适合进行简单身份验证

以下是我当前使用的代码:

Public Shared Function login(ByVal domain As String, ByVal userid As String, ByVal pwd As String, ByVal logonType As Integer, ByRef errorMessage As String) As Boolean
        'The Windows NT user token.
        Dim token As IntPtr = IntPtr.Zero
        Dim LOGON32_PROVIDER_DEFAULT As Integer = 0

        'Get the user token for the specified user, machine, and password using the unmanaged LogonUser method.

        If LogonUserA(userid, domain, pwd, logonType, LOGON32_PROVIDER_DEFAULT, token) <> 0 Then
            If Not token.Equals(IntPtr.Zero) Then
                CloseHandle(token)
            End If
            Return True
        End If

        'Call GetLastError to try to determine why logon failed if it did not succeed.
        Dim ret As Integer = GetLastError()
        Select Case ret
            Case 1326
                errorMessage = "Logon failure: unknown user name or bad password."
            Case 1331
                errorMessage = "Logon failure: account currently disabled."
            Case 1330
                errorMessage = "Logon failure: the specified account password has expired."
            Case 1907
                errorMessage = "The user's password must be changed before logging on the first time."
            Case 1909
                errorMessage = "The referenced account is currently locked out and may not be logged on to."
            Case Else
                errorMessage = "Logon failure: unknown code = " & ret
        End Select
        If Not token.Equals(IntPtr.Zero) Then
            CloseHandle(token)
        End If
        Return False
    End Function
公共共享函数登录(ByVal域作为字符串,ByVal userid作为字符串,ByVal pwd作为字符串,ByVal logonType作为整数,ByRef errorMessage作为字符串)作为布尔值
'Windows NT用户令牌。
作为IntPtr=IntPtr.Zero的Dim令牌
Dim LOGON32\u提供程序\u默认为整数=0
'使用非托管LogonUser方法获取指定用户、计算机和密码的用户令牌。
如果LogonUserA(用户ID、域、pwd、logonType、LOGON32\u提供者\u默认值、令牌)为0,则
如果不是token.Equals(IntPtr.Zero),则
CloseHandle(令牌)
如果结束
返回真值
如果结束
'调用GetLastError以尝试确定登录失败的原因(如果未成功)。
Dim ret As Integer=GetLastError()
选择案例ret
案例1326
errorMessage=“登录失败:未知用户名或错误密码。”
案件1331
errorMessage=“登录失败:帐户当前已禁用。”
案件1330
errorMessage=“登录失败:指定的帐户密码已过期。”
案例1907
errorMessage=“首次登录前必须更改用户密码。”
案例1909
errorMessage=“引用的帐户当前已锁定,可能无法登录。”
其他情况
errorMessage=“登录失败:未知代码=“&ret”
结束选择
如果不是token.Equals(IntPtr.Zero),则
CloseHandle(令牌)
如果结束
返回错误
端函数