This code will allow you to check to make sure the entered username and password is valid for what ever domain you specify. If you enter the local machine name as the domain it will also validate the windows account.
Code:Public Class LoginForm Declare Function LogonUserA Lib "advapi32.dll" (ByVal lpszUsername As String, ByVal lpszDomain As String, ByVal lpszPassword As String, ByVal dwLogonType As Integer, ByVal dwLogonProvider As Integer, ByRef phToken As IntPtr) As Integer Private Enum LogonType As Long LOGON32_LOGON_INTERACTIVE = 2 LOGON32_LOGON_NETWORK = 3 LOGON32_PROVIDER_DEFAULT = 0 LOGON32_PROVIDER_WINNT50 = 3 LOGON32_PROVIDER_WINNT40 = 2 LOGON32_PROVIDER_WINNT35 = 1 End Enum Private Sub cmdOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOK.Click Dim Result As String Result = ValidateUser(txtUsername.Text, txtPassword.Text, txtDomain.Text) If Result = "True" Then MsgBox("User Validated") Else MsgBox(Result, MsgBoxStyle.Critical, "Error") End If End Sub Private Function ValidateUser(ByVal UserName As String, ByVal Password As String, ByVal Domain As String) As String Dim Token As IntPtr = IntPtr.Zero If LogonUserA(UserName, Domain, Password, LogonType.LOGON32_LOGON_INTERACTIVE, LogonType.LOGON32_PROVIDER_DEFAULT, Token) Then Return True Else Try Throw New System.ComponentModel.Win32Exception(System.Runtime.InteropServices.Marshal.GetLastWin32Error()) Catch ex As Exception Return ex.Message End Try End If End Function End Class


Reply With Quote