Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Windows Password Authentication

Status
Not open for further replies.

Swi

Programmer
Feb 4, 2002
1,963
US
I have written an app that uses the following code:

Code:
Option Explicit
Public Declare Function LogonUser _
    Lib "advapi32" _
    Alias "LogonUserA" ( _
    ByVal lpszUser As String, _
    ByVal lpszDomain As String, _
    ByVal lpszPass As String, _
    ByVal dwLogonType As Long, _
    ByVal dwLogonProvider As Long, _
    Handle As Long) As Long
Public Declare Function CloseHandle Lib "kernel32" (Handle As Long) As Long

Public Const LOGON32_LOGON_INTERACTIVE = 2
Public Const LOGON32_LOGON_NETWORK = 3
Public Const LOGON32_LOGON_BATCH = 4
Public Const LOGON32_LOGON_SERVICE = 5
Public Const LOGON32_PROVIDER_DEFAULT = 0
Public Const FORMAT_MESSAGE_FROM_SYSTEM = &H1000
Public Const FORMAT_MESSAGE_ALLOCATE_BUFFER = &H100
Public Const FORMAT_MESSAGE_FROM_HMODULE = &H800
Public Const NERR_BASE = 2100
Public Const MAX_NERR = NERR_BASE + 899 ' This is the last error in NERR range.
Public Const LOAD_LIBRARY_AS_DATAFILE = &H2

Public Declare Function LoadLibraryEx _
    Lib "kernel32" _
    Alias "LoadLibraryExA" ( _
    ByVal lpLibFileName As String, _
    ByVal hFile As Long, _
    ByVal dwFlags As Long) As Long

Public Declare Function FreeLibrary Lib "kernel32" (ByVal hLibModule As Long) As Long

Public Declare Function FormatMessage _
    Lib "Kernel32.dll" _
    Alias "FormatMessageA" ( _
    ByVal Flags As Long, _
    ByVal Source As Long, _
    ByVal MessageID As Long, _
    ByVal LanguageID As Long, _
    ByVal Buffer As String, _
    ByVal Size As Long, _
    args As Any) As Long

Public Declare Function LocalFree Lib "Kernel32.dll" (ByVal Handle As Long) As Long

Public Function Login(strUser As String, _
    strPass As String, _
    strerror As String, _
    Optional strDomain As String) As Boolean
    
Dim lngError As Long
Dim lngHandle As Long

strDomain = vbNullString

    lngError = LogonUser( _
        strUser, _
        strDomain, _
        strPass, _
        LOGON32_LOGON_INTERACTIVE, _
        LOGON32_PROVIDER_DEFAULT, _
        lngHandle)
        
    If lngError = 0 Then
        strerror = ErrorMessage(Err.LastDllError)
        Login = False
    Else
        Login = True
        lngError = CloseHandle(lngHandle)
        
    End If
    
End Function

Public Function ErrorMessage(lCode As Long) As String

Dim lngError As Long
Dim ptrBuffer As Long
Dim strMessage As String
Dim hModule As Long
Dim lngFlags As Long
Dim str As String

        lngFlags = FORMAT_MESSAGE_FROM_SYSTEM

         If (lCode >= NERR_BASE And lCode <= MAX_NERR) Then
            hModule = LoadLibraryEx("netmsg.dll", 0&, _
                      LOAD_LIBRARY_AS_DATAFILE)
            If (hModule <> 0) Then
                lngFlags = lngFlags Or FORMAT_MESSAGE_FROM_HMODULE
            End If
        End If

    strMessage = Space$(256)
    lngError = FormatMessage( _
        lngFlags, _
        hModule, _
        lCode, _
        0&, _
        strMessage, _
        256, _
        0&)
        If (hModule <> 0) Then
            lngError = FreeLibrary(hModule)
        End If
    ErrorMessage = strMessage

End Function

The issue is that when one of the users run it on his machine he can not authenticate but if he types in another user's name and password it does work. Any ideas?

Swi
 
I can't find the specifics of the API to be sure, but I'm betting the call is probably failing because the user is already logged on to that machine (since by the code you are logging them in to the local machine).
 
Also, you might get more responses to this question in the VB WIN32API forum.
 
Windows sends the current username and password first, before showing the login box. Websites know your username and password to your box when you log on to a secured site.

That's why having the same un/pw is good for home networks.

-David
2006 Microsoft Valueable Professional (MVP)
2006 Dell Certified System Professional (CSP)
 
Thanks for the replys. I was pulled off of this for a bit but am taking a look at it again.

Swi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top