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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Determine if user is vaild without logon to windows again 1

Status
Not open for further replies.

paulfenwick

Technical User
Sep 16, 2004
20
GB
Hi, Im trying to develop an application (database) for use on a common / shared PC where I need the application to be able to check (authenticate) a user based on their windows username and password without each user loggin on and off every time.

i.e. the PC is logged with either user A or a common resource user account. User B comes along opens the database enters the username and password and the application passes this to an API to check he is a valid user (return a simple yes / no for the username password combination) - if not the database closes.

Is this possible please?

I thiought about / tried using the "logonuser" call in advapi32.dll but didnt manage!

Thanks
 
Hi - OK I found a solution:

First delcare the external dll:

Code:
Public Declare Function LogonUser Lib "advapi32.dll" Alias "LogonUserA" (ByVal lpszUsername As String, ByVal lpszDomain As String, ByVal lpszPassword As String, ByVal dwLogonType As Integer, ByVal dwLogonProvider As Integer, ByRef phToken As Long) As Long

Then this function returns a boolean result for the check:
Code:
Private Function ValidateLogin(ByVal Username As String, ByVal Password As String, ByVal Domain As String) As Boolean

Dim token As Long
Dim result

Const LOGON32_LOGON_INTERACTIVE As Long = 2
Const LOGON32_LOGON_NETWORK As Long = 3
Const LOGON32_PROVIDER_DEFAULT As Long = 0
Const LOGON32_PROVIDER_WINNT50 As Long = 3
Const LOGON32_PROVIDER_WINNT40 As Long = 2
Const LOGON32_PROVIDER_WINNT35 As Long = 1

result = LogonUser(Username, Domain, Password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, token)

If result = 1 Then
    ValidateLogin = True
Else
    ValidateLogin = False
End If

End Function

Simply call the function as needed:
Code:
Private Sub cmb_auth_Click()

If ValidateLogin(Me.txt_name, Me.txt_password, "domain") = True Then
    Me.txt_result = "Correct"
Else
    Me.txt_result = "wrong!"
End If

End Sub

Hope this helps someone.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top