-
Dec 18th, 2008, 08:49 AM
#1
Thread Starter
Hyperactive Member
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."
-
Dec 12th, 2013, 08:48 AM
#2
New Member
Re: Validate a User Account
How can I validate also an local user... i putted the machine name in txtdomain, but doesnt' work
-
Dec 12th, 2013, 06:01 PM
#3
Re: Validate a User Account
Originally Posted by jackdroid
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:
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.
https://get.cryptobrowser.site/30/4111672
-
Dec 22nd, 2013, 09:16 AM
#4
Re: Validate a User Account
Try a "." instead ...
Also I would suggest using managed code such as this:
vb Code:
Public Function ValidateUser(Username As String, Password As String)
Using context As New System.DirectoryServices.AccountManagement.PrincipalContext(DirectoryServices.AccountManagement.ContextType.Machine)
Return (context.ValidateCredentials(Username, Password))
End Using
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
Last edited by i00; Dec 22nd, 2013 at 09:28 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|