.Net offers multiple ways to manage impersonation and its level. The important point to understand is what is being impersonated: the thread or the process also is the impersonation happening on the process or is it happening only on the network. Below classes will show you how to impersonate in all this cases.
First class: ImpersonateManager – allows starting impersonation and will apply to the thread scope. You will need to allow unsafe code in your project build properties. Below program is an example of using the ImpersonateManager.
The ImpersonateManager.cs is like this:
Now this might not be enough for your need, you might need more than thread impersonation.
There are basically two main logon scenarios in this case:
1) The user you want to impersonate is on the same domain as the current process
- Load the profile in the registry (like runas /profile)
- Sample with: ProcessImpersonator.ImpersonateProcess_WithProfile()
2) The user you want to impersonate is on a domain without trust relationship
- Use the specified credentials on the network only (like runas /netuse)
- Sample with : ProcessImpersonator.ImpersonateProcess_NetCredentials()
Below program do exactly this, it will start another executable (located in the same folder and having a name of test.exe).
ProcessImpersonator.cs looks like this:
For more information on the topic: