Skip to main content
Email Password   helpLost your password?

Introduction

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).

Background

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 (username, domain, 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 username, 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.

Conclusion

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.

References

In addition to the links in the article, the following references might be of interest:

  1. Google search for "Windows Impersonation"

History

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralNice work, but help Pin
turbohansen
14:10 21 Aug '09  
GeneralRe: Nice work, but help Pin
Bit-Smacker
11:00 17 Sep '09  
GeneralNice one Pin
fluectho
4:56 24 Jun '09  
GeneralGreat job Pin
Richard Hollis
12:43 20 Mar '09  
GeneralRe: Great job Pin
Member 1752718
12:19 8 Apr '09  
GeneralThis does not seem to work if a Win XP workstation is on a WorkGroup... Pin
danvmn
5:44 2 Mar '09  
GeneralThanks Pin
carladinmc
5:20 26 Feb '09  
GeneralCan I use this C# class in Vb project Pin
Member 3481590
6:43 2 Feb '09  
GeneralHow do you use Impersonator without the using clause? Pin
danvmn
5:58 23 Jan '09  
GeneralRe: How do you use Impersonator without the using clause? Pin
Uwe Keim
6:02 23 Jan '09  
GeneralRe: How do you use Impersonator without the using clause? Pin
danvmn
9:20 23 Jan '09  
GeneralRe: How do you use Impersonator without the using clause? Pin
danvmn
9:50 23 Jan '09  
GeneralRe: How do you use Impersonator without the using clause? Pin
danvmn
6:57 23 Jan '09  
RantSecurity Risk Pin
RobinVossen
21:51 11 Jan '09  
GeneralOpenFileDialog Pin
RugbyLeague
5:54 15 Dec '08  
GeneralWell done!!! Pin
uhcl
12:20 5 Nov '08  
GeneralNice job Pin
Jugortha
8:08 16 Sep '08  
GeneralRe: Nice job Pin
Uwe Keim
9:25 16 Sep '08  
GeneralRe: Nice job Pin
finiduck
2:32 30 Oct '08  
GeneralExcellent Pin
RVW
7:58 11 Sep '08  
GeneralGreat Job Pin
netizenk
14:32 27 Aug '08  
GeneralCould not find a part of the path Pin
RugbyLeague
3:35 15 Aug '08  
GeneralRe: Could not find a part of the path Pin
finiduck
2:27 30 Oct '08  
GeneralExcellent Pin
Emad Attia
8:04 30 Jul '08  
GeneralDoesn't work on Windows 2000 Pin
weitech
22:50 12 Jun '08  


Last Updated 11 Apr 2005 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2009