win32security.LogonUser not working

dsavitsk dsavitsk at e-coli.net
Sat Sep 14 18:54:21 EDT 2002


I am trying to run code as another user. I copied the code below from ASPN,
so it would seem that someone has gotten this to work. I keep getting the
error

pywintypes.api_error (1314, 'LogonUser', 'A required privilege is not held
by the client.')

Google has revealed that others gave had the same problem.  does anyone know
a solution, or even a different way to do this?

-doug

link to the original of code below
http://www.google.com/search?q=cache:Jnbt2XVyGfUC:aspn.activestate.com/ASPN/
Python/Reference/Products/ActivePython/PythonWin32Extensions/Windows_NT_Secu
rity_.2d.2d_Impersonation.html+self.handel%3Dwin32security.LogonUser&hl=en&i
e=UTF-8

---------------------------------------------
import win32security
import win32con
import win32api
import sys


class Impersonate:

    def __init__(self):
        self.domain   = '.'
        self.login    = 'myusername'
        self.password = '**********'

    def logon(self):
        self.handel=win32security.LogonUser(self.login,
                                            self.domain,
                                            self.password,

win32con.LOGON32_LOGON_INTERACTIVE,

win32con.LOGON32_PROVIDER_DEFAULT
                                            )
        win32security.ImpersonateLoggedOnUser(self.handel)

    def logoff(self):
        win32security.RevertToSelf() #terminates impersonation
        self.handel.Close() #guarantees cleanup




if __name__ == '__main__':
    a=Impersonate()
    try:
        a.logon() #become the user
        print win32api.GetUserName() #show you're someone else
        a.logoff() #return to normal
    except:
        print sys.exc_type , sys.exc_value





More information about the Python-list mailing list