VB - Check Windows Password

By , 11/10/2007
(1 ratings)
This function checks a windows user password whether it is valid and matching to the given username.
Translate to C#
Private Declare Auto Function LogonUser Lib "advapi32.dll" ( _
    ByVal lpszUsername As String, _
    ByVal lpszDomain As String, _
    ByVal lpszPassword As String, _
    ByVal dwLogonType As LogonType, _
    ByVal dwLogonProvider As Integer, _
    ByRef phToken As IntPtr) _
As Integer

Private Declare Auto Function CloseHandle Lib "kernel32.dll" ( _
    ByVal hObject As IntPtr) _
As Boolean

Public Enum LogonType As Integer
    LOGON32_LOGON_INTERACTIVE = 2
    LOGON32_LOGON_NETWORK = 3
    LOGON32_LOGON_BATCH = 4
    LOGON32_LOGON_SERVICE = 5
    LOGON32_LOGON_UNLOCK = 7
    LOGON32_LOGON_NETWORK_CLEARTEXT = 8
    LOGON32_LOGON_NEW_CREDENTIALS = 9
End Enum

Public Function IsNTPasswordValid(ByVal Username As String, ByVal Password As String, Optional ByVal Domain As String = "") As Boolean
    Dim Token As New IntPtr
    LogonUser(Username, Domain, Password, LogonType.LOGON32_LOGON_INTERACTIVE, 0, Token)
    CloseHandle(Token)
    If Token.ToInt32 <> 0 Then Return True
End Function

Tagged with Password, Windows.

1 Comments

By Bradley, 5/29/2012
There is a problem with the end fuction

[code=VB.NET]Public Function IsNTPasswordValid(ByVal Username As String, ByVal Password As String, Optional ByVal Domain As String = "") As Boolean
Dim Token As New IntPtr
LogonUser(Username, Domain, Password, LogonType.LOGON32_LOGON_INTERACTIVE, 0, Token)
CloseHandle(Token)
If Token.ToInt32 <> 0 Then Return True
End Function [/code]
it states function IsNTPasswordValid don't return a value on all code paths are you missing a "Return" statement
any help would be appricated but other than that fucntion the code is excepted any reasion why this maybe the case
Kind Regards

Bradley
 

Log in, to comment!