VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "ImpersonateUser"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = True
'Code for Class Module:

Option Explicit

Const LOGON32_LOGON_INTERACTIVE = 2
Const LOGON32_LOGON_NETWORK = 3
Const LOGON32_LOGON_BATCH = 4
Const LOGON32_LOGON_SERVICE = 5

Const LOGON32_PROVIDER_DEFAULT = 0
Const LOGON32_PROVIDER_WINNT35 = 1
Const LOGON32_PROVIDER_WINNT40 = 2
Const LOGON32_PROVIDER_WINNT50 = 3
''''''''''''''''''''''''''''''''''''''''''''
      Const INTERNET_OPTION_END_BROWSER_SESSION = 42

      Private Declare Function InternetSetOption Lib "wininet.dll" Alias "InternetSetOptionA" _
      (ByVal hInternet As Long, ByVal lOption As Long, ByRef sBuffer As Any, ByVal lBufferLength As Long) As Integer

Public Function flushCredentials() As Integer
   Dim h As Integer
   h = InternetSetOption(0, INTERNET_OPTION_END_BROWSER_SESSION, 0, 0)
   flushCredentials = h
End Function


Public Function Logon(ByVal strAdminUser As String, ByVal strAdminPassword As String, _
                                         ByVal strAdminDomain As String) As Long
  Dim lngTokenHandle, lngLogonType, lngLogonProvider As Long
  Dim blnResult As Boolean
  
  lngLogonType = LOGON32_LOGON_INTERACTIVE
  lngLogonProvider = LOGON32_PROVIDER_DEFAULT
  
  blnResult = RevertToSelf()
  
  blnResult = LogonUser(strAdminUser, strAdminDomain, strAdminPassword, _
                                       lngLogonType, lngLogonProvider, _
                                       lngTokenHandle)
  Logon = Err.LastDllError
  
  'blnResult = ImpersonateLoggedOnUser(lngTokenHandle)
  'Logon = blnResult
  

  
End Function

Public Sub Logoff()
  Dim blnResult As Boolean
  
  blnResult = RevertToSelf()
End Sub

Public Function AuthenticateUser(ByVal strUser As String, _
                                 ByVal strPassword As String, _
                                 ByVal strDomain As String) As Long
    
    AuthenticateUser = Logon(strUser, strPassword, strDomain)
    
    Call Logoff
    
End Function
