Option Explicit
Private Const LOGON32_PROVIDER_DEFAULT = 0
Private Const LOGON32_LOGON_BATCH = 4
Private Const LOGON32_LOGON_INTERACTIVE = 2
Private Const LOGON32_LOGON_SERVICE = 5
' Check Windows API for correct function calls
Private Declare Function RevertToSelfa Lib "advapi32.dll" Alias "RevertToSelf" () As Long
Private Declare Function LogonUser Lib "advapi32.dll" Alias "LogonUserA" (ByVal lpszUsername As String, ByVal lpszDomain As String, ByVal lpszPassword As String, ByVal dwLogonType As Long, ByVal dwLogonProvider As Long, phToken As Long) As Long
Private Declare Function ImpersonateLoggedOnUsera Lib "advapi32.dll" Alias "ImpersonateLoggedOnUser" (ByVal hToken As Long) As Long
'local variable(s) to hold property value(s)
Private mvarusername As String 'local copy
Private mvarpassword As String 'local copy
Private mvardomain As String 'local copy
Private mvarservername As String 'local copy
Public Property Let servername(ByVal vData As String)
'used when assigning a value to the property, on the left side of an assignment.
'Syntax: X.servername = 5
mvarservername = vData
End Property
Public Property Get servername() As String
'used when retrieving value of a property, on the right side of an assignment.
'Syntax: Debug.Print X.servername
servername = mvarservername
End Property
Public Property Let domain(ByVal vData As String)
'used when assigning a value to the property, on the left side of an assignment.
'Syntax: X.domain = 5
mvardomain = vData
End Property
Public Property Get domain() As String
'used when retrieving value of a property, on the right side of an assignment.
'Syntax: Debug.Print X.domain
domain = mvardomain
End Property
Public Property Let password(ByVal vData As String)
'used when assigning a value to the property, on the left side of an assignment.
'Syntax: X.password = 5
mvarpassword = vData
End Property
Public Property Get password() As String
'used when retrieving value of a property, on the right side of an assignment.
'Syntax: Debug.Print X.password
password = mvarpassword
End Property
Public Property Let username(ByVal vData As String)
'used when assigning a value to the property, on the left side of an assignment.
'Syntax: X.username = 5
mvarusername = vData
End Property
Public Property Get username() As String
'used when retrieving value of a property, on the right side of an assignment.
'Syntax: Debug.Print X.username
username = mvarusername
End Property
Public Sub Authenticate()
Dim Result As Boolean, Token As Long
Result = RevertToSelfa()
Result = LogonUser(mvarusername, mvardomain, mvarpassword, LOGON32_LOGON_BATCH, LOGON32_PROVIDER_DEFAULT, Token)
If Result Then
Result = ImpersonateLoggedOnUsera(Token)
If Result Then
'client is athenticated
End If
End If
If Not Result Then
ErrorNumber = GetLastError
End If
End Sub
Public Sub Logoff()
Dim Result As Boolean
Result = RevertToSelfa()
End Sub