Using the LogonUser API

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Jerry West (jw_at_comcast.net)
Date: 10/02/04

  • Next message: Tom Esh: "Re: Aaarrrggghhh -- that ugly flicker!"
    Date: Sat, 2 Oct 2004 12:33:12 -0700
    
    

    I am attempting to impersonate a user using the LogonUser and related APIs.
    Whenever I call LogonUser on a Windows 2000 system the call fails with the
    error: ERROR_PRIVILEGE_NOT_HELD

    The MSDN indicates this is a issue specific to Win2K and is related to the
    calling process having the SE_TCB_NAME privilege. I am using the
    Administrator credentials when calling the LogonUser API. I am also
    validating the Admin account against the local account database. Finally,
    the current logged on account is the same Admin account as the credentials
    being passed into LogonUser. Given these facts it seems that I shouldn't
    need the SE_TCB_NAME privilege since I am already logged in as Admin and the
    credentials I'm passing to LogonUser are the same.

    How can I obtain this privilege?

    Thanks in advance!

    JW

    The code I am using is:

    Private Const LOGON32_LOGON_INTERACTIVE = 2
    Private Const LOGON32_PROVIDER_DEFAULT = 0

    Private Const PI_NOUI = 1

    Private Type PROFILEINFO
        dwSize As Long
        dwFlags As Long
        lpUserName As Long
        lpProfilePath As Long
        lpDefaultPath As Long
        lpServerName As Long
        lpPolicyPath As Long
        hProfile As Long
    End Type

    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 ImpersonateLoggedOnUser Lib "advapi32" ( _
        ByVal hToken As Long) As Long
    Private Declare Function RevertToSelf Lib "advapi32.dll" () As Long
    Private Declare Function CloseHandle Lib "kernel32" ( _
        ByVal hObject As Long) As Long
    Private Declare Function LoadUserProfile Lib "userenv.dll" Alias _
        "LoadUserProfileA" (ByVal hToken As Long, _
        ByVal lpProfileInfo As Long) As Boolean

    Public Function Impersonate(sUser As String, sDomain As String, _
        sPwd As String, hToken As Long, _
        Optional bLoadProfile As Boolean = False, _
        Optional hProfile As Long) As Long

        Dim UserToken As Long
        Dim ProfileHandle As Long
        Dim aUser() As String
        Dim sUserName As String
        Dim lRes As Long

        lRes = LogonUser(sUser, sDomain, sPwd, LOGON32_LOGON_INTERACTIVE, _
            LOGON32_PROVIDER_DEFAULT, UserToken)

        If lRes <> 0 Then
            If bLoadProfile Then
                lRes = LoadProfile(sUser, UserToken, ProfileHandle)
            Else
                lRes = 0
            End If
            If lRes = 0 Then
                lRes = ImpersonateLoggedOnUser(UserToken)
                If lRes = 0 Then
                    CloseHandle UserToken
                    UserToken = 0
                End If
            End If
        End If

        If lRes = 0 Then lRes = Err.LastDllError Else lRes = 0

        Impersonate = lRes
        hToken = UserToken
        hProfile = ProfileHandle
    End Function

    Public Function LoadProfile(sUserName As String, hToken As Long, _
        hProfile As Long) As Long

        Dim PI As PROFILEINFO
        Dim lpPI As Long

        PI.dwSize = Len(PI)
        PI.dwFlags = PI_NOUI
        PI.lpUserName = StrPtr(sUserName)
        PI.lpProfilePath = 0
        PI.lpDefaultPath = 0
        PI.lpServerName = 0
        PI.lpPolicyPath = 0

        lpPI = VarPtr(PI)

        If LoadUserProfile(hToken, lpPI) Then
            hProfile = PI.hProfile
            LoadProfile = 0
        Else
            LoadProfile = Err.LastDllError
        End If
    End Function

    Public Function Revert(hToken As Long) As Long
        CloseHandle hToken
        If RevertToSelf <> 0 Then
            Revert = 0
        Else
            Revert = Err.LastDllError
        End If
    End Function


  • Next message: Tom Esh: "Re: Aaarrrggghhh -- that ugly flicker!"

    Relevant Pages

    • Re: Alpha search to load a list box
      ... Dim strTemp As String ... Private Sub LblAlpha_MouseDown(Button As Integer, Shift As Integer, X ... Dim StartX As Long, WidthX As Long ... Private Declare Function apiSelectObject Lib "gdi32" Alias ...
      (microsoft.public.access.formscoding)
    • Re: Alpha search to load a list box
      ... Dim strTemp As String ... Private Declare Function apiSelectObject Lib "gdi32" Alias ... Dim newfont As Long ' Handle to our Font Object we created. ...
      (microsoft.public.access.formscoding)
    • ListView.SelectedItem cannot be modified
      ... Dim objFind As LV_FINDINFO ... Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd ... lpClassName As String, ByVal lpWindowName As String) As Long ... 'CompareDates: This is the sorting routine that gets passed to the ...
      (microsoft.public.vb.controls)
    • Re: Need to call windows scheduler.
      ... Private Declare Function OpenSCManager Lib "advapi32.dll" Alias ... "OpenSCManagerA" (ByVal lpMachineName As String, ... Dim lhSCM As Long, lhService As Long, sState As String, lReturn ...
      (microsoft.public.access.formscoding)
    • Re: changing vbOKCancel button title
      ... Private Declare Function GetCurrentThreadId Lib "kernel32" _ ... ByVal lpCaption As String, _ ... Dim mbFlags2 As VbMsgBoxStyle ... SetDlgItemText wParam, vbAbort, But1 ...
      (microsoft.public.excel.misc)