Search code examples
c#impersonation

Impersonate Current User from Service


Don't know if this is possible but i would like to impersonate the current logged on user from within a Service? Please note i will not know the username password for the current logged on / active user on the machine..

If anyone has any documentation / samples on how to do this would be a great help...


Solution

  • There can be 0, 1, or more than one user logged in to the machine.

    The best way to deal with any interaction where the service and "the" logged in user interacts is to split this interaction into two (or more) programs.

    The service code just runs whatever code it needs to as a service, and exposes some form of API.

    A second program is run within each users session (e.g. by putting it in the startup folder) and then connects to the service using the API you've designed.

    The service can then keep track of the user programs that are currently running and (if necessary) send requests back to those program(s) asking them to perform activities within each users session.