Search code examples
.netwindowswinapiimpersonation

How to get Calling-Process Windows User Access Token


How can I get the Access Token for the user that created the process that called my application?

I need to use that Token for impersonation, the idea is to access a file in that users applicationData.

P.S. The application that will be impersonating the user is a service running under System.


Solution

  • Check MSDN, all this API is well documented. You probably want to do something like this:

    HANDLE thisToken, thisProcess;
    
    thisProcess = OpenProcess( PROCESS_ALL_ACCESS, FALSE, GetCurrentProcessId() );
    OpenProcessToken( thisProcess, TOKEN_ALL_ACCESS, &thisToken );
    

    http://msdn.microsoft.com/en-us/library/aa379295(v=vs.85).aspx

    Though you probably want less access than that. This will get you the token for the current process.