The term "Impersonation" in a programming context refers to a technique that executes the code under another user context than the user who originally started an application, i.e. the user context is temporarily changed once or multiple times during the execution of an application.
The reason for doing this is to perform tasks that the current user context of an application is not allowed to do. Of course you could grant the user executing an application more privileges, but usually this is a bad idea (due to security constraints) or impossible (e.g. if you don't have full administrative access to a machine to do so).
This article presents an easy-to-use class to impersonate a user. While writing this, I found out that Marc Merrit had written an article ("Windows Impersonation using C#") that uses the same Microsoft knowledge base code (from Q306158) that I have used. The code presented in my article differs in the fact that you could use it inside a
using-block to safely release resources and that I use slightly more exceptions to report errors. But from a first look, both his and my article do the same job, so it's up to you to decide what to do.
(For the latest changes, please see the History section below).
I wrote the
Impersonator class because of a need to write a web page with ASP.NET to make a server reboot. In order to do this, I needed to impersonate the part of my code that does the actual reboot.
The constructor of the class internally calls the Windows function
LogonUser through P/Invoke. Please see the MSDN documentation of the function for a full description of all three parameters (
password) to the constructor.
Please note: The user context that initiates the impersonation (i.e. not the user context to which it is switched to) needs to have the "Act as part of operating system" privilege set.
Using the code
To use the code, you simply construct the
Impersonator class and pass the
domain and the
password to the constructor. If you place an instance of the class inside a
using-block, you need no further steps.
The following is a schematic example of how to use the class:
using ( new Impersonator( "myUsername", "myDomainname", "myPassword" ) )
<code that executes under the new context>
An example project demonstrating the technique is included in the download of this article (please look at the "Program.cs" for the main demonstration source file). Also the complete source code of the class is included inside the source file "Impersonator.cs".
To include the
Impersonator class into your project, simply copy and add the source file "Impersonator.cs" to your project, so that it gets compiled with your project.
In this article, I've shown you a small class to quickly and easily impersonate a part of your code to run under another user context. Hopefully you'll find this class useful.
For questions, comments and remarks, please use the commenting section at the bottom of this article.
In addition to the links in the article, the following references might be of interest:
- Google search for "Windows Impersonation"
- 2005-04-11: Created first version of article.