'''
''' Validates whether the passed Windows user Id and password are valid.
'''
''' The Windows user name with domain prepended if necessary.
''' The user's Windows password.
''' The user's Windows password again.
''' True if the user's ID and password are correct.
'''
Public Function ValidateUserIdPassword(ByVal szUserName As String, ByVal szPassword As String, ByVal szPasswordVerify As String) As Boolean
Dim bRetVal As Boolean = False
Console.WriteLine("Validating user {0}", szUserName)
If szPassword.Equals(szPasswordVerify) Then
Console.WriteLine("Passwords match.")
AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal)
' Retrieve the computer name
Dim machName As String = Environment.MachineName
Dim MyNamespace As IADsOpenDSObject
Dim oUserValidation As Object
Dim DN As String = "LDAP://rootDSE"
Dim ADS_AUTHENTICATION_SECURE As New ADS_AUTHENTICATION_ENUM
MyNamespace = GetObject("LDAP:")
'For authentication, pass in a variable for the user name and password that you wish to use for
'authentication purposes. It is recommended that you use the ADS_AUTHENTICATION_SECURE flag for
'security reasons.
Try
oUserValidation = MyNamespace.OpenDSObject(DN, szUserName, szPassword, ADS_AUTHENTICATION_SECURE)
bRetVal = True
Catch ex As Exception
' Do nothing
Console.WriteLine("Error {0}", ex.Message)
End Try
End If
\ Return bRetVal
End Function
'''
''' Validates a Windows user Id, domain and password combination.
'''
''' The Windows user name. This can be in the form of username, domain\username, or username@domain. If the domain is specified like this, leave the szUserDomain parameter blank.
''' The Windows machine name or domain. If the domain is specified as part of the user name, pass an empty string in this parameter.
''' The user's password.
''' A password validation check.
''' True if all the information is correct. False if one or more items were incorrect.
''' Uses the Windows API LogonUser function to check the security context. The context is restore before the function exits.
Public Function ValidateUserIdPassword(ByVal szUserName As String, ByVal szUserDomain As String, ByVal szPassword As String, ByVal szPasswordVerify As String) As Boolean
' Set default return value.
Dim bRetVal As Boolean = False
' Compare passwords.
If szPassword.Equals(szPasswordVerify) Then
' Password is same in password and password verify variables.
If szUserDomain = "" Then
If szUserName.Contains("\") Then
' Pull the domain out of the user name.
szUserDomain = szUserName.Substring(0, szUserName.IndexOf("\"))
' Set user name to just user name.
szUserName = szUserName.Substring(szUserName.IndexOf("\") + 1)
ElseIf szUserName.Contains("@") Then
' User Name is in form of "user@domain"
' Pull the domain out of the user name.
szUserDomain = szUserName.Substring(szUserName.IndexOf("@") + 1)
' Set user name to just user name.
szUserName = szUserName.Substring(0, szUserName.IndexOf("@"))
Else
' A "." refers to the local system.
szUserDomain = "."
End If
End If
Console.WriteLine("Validating user {0}, Domain {1}", szUserName, szUserDomain)
Dim lphToken As IntPtr = IntPtr.Zero
' Attempt to log in to the machine/domain. Function returns True or False depending on the user Id, password and domain work.
bRetVal = LogonUser(szUserName, szUserDomain, szPassword, LogonType.LOGON32_LOGON_INTERACTIVE, LogonProvider.LOGON32_PROVIDER_DEFAULT, lphToken)
' Close the handle received.
CloseHandle(lphToken)
' Revert security to the currently logged in user.
RevertToSelf()
End If
Return bRetVal
End Function
|