Validate a User Account-VBForums
Results 1 to 4 of 4

Thread: Validate a User Account

  1. #1

    Thread Starter
    Hyperactive Member .NetNinja's Avatar
    Join Date
    Oct 2008
    Location
    USA
    Posts
    281

    Arrow Validate a User Account

    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
    "Don't try to be a great man. Just be a man and let history make its own judgement."

  2. #2
    New Member
    Join Date
    Dec 2013
    Posts
    1

    Re: Validate a User Account

    How can I validate also an local user... i putted the machine name in txtdomain, but doesnt' work

  3. #3
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,082

    Re: Validate a User Account

    Quote Originally Posted by jackdroid View Post
    How can I validate also an local user... i putted the machine name in txtdomain, but doesnt' work
    No sure what you mean but try putting "Local Host" in the txtDamain textbox.

    This is only an example.
    vb.net Code:
    1. Result = ValidateUser("Nightwalker83", "password", "Local Host")
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    Please consider giving me some rep points if I help you a lot.
    Links to my code examples can now be found on my website: My websites
    Please rate my post if you find it helpful!
    Technology is a dangerous thing in the hands of an idiot! I am that idiot.

  4. #4
    Frenzied Member i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    1,844

    Re: Validate a User Account

    Try a "." instead ...

    Also I would suggest using managed code such as this:

    vb Code:
    1. Public Function ValidateUser(Username As String, Password As String)
    2.         Using context As New System.DirectoryServices.AccountManagement.PrincipalContext(DirectoryServices.AccountManagement.ContextType.Machine)
    3.             Return (context.ValidateCredentials(Username, Password))
    4.         End Using
    5.     End Function

    Just whipped up and tested ... local machine only since that's what your using

    .. oh and you will need to add a reference to System.DirectoryServices.AccountManagement

    Kris

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.