Click here to Skip to main content
13,862,320 members
Click here to Skip to main content
Add your own
alternative version


235 bookmarked
Posted 11 Apr 2005
Licenced CPOL

A small C# Class for impersonating a User

, 11 Apr 2005
Rate this:
Please Sign up or sign in to vote.
Introduced a class that simplifies Windows Impersonation in C#.


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


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:

  1. Google search for "Windows Impersonation"


  • 2005-04-11: Created first version of article.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Uwe Keim
Chief Technology Officer Zeta Producer Desktop CMS
Germany Germany
Uwe does programming since 1989 with experiences in Assembler, C++, MFC and lots of web- and database stuff and now uses ASP.NET and C# extensively, too. He has also teached programming to students at the local university.

In his free time, he does climbing, running and mountain biking. Recently he became a father of a cute boy.

Some cool, free software from us:

Deutschsprachiges Forum zu Elektromobilität und Tesla  
Windows 10 Ereignisanzeige  
German Developer Community  
Free Test Management Software - Intuitive, competitive, Test Plans. Download now!  
Homepage erstellen - Intuitive, very easy to use. Download now!  
Send large Files online for free by Email

You may also be interested in...

Comments and Discussions

GeneralNice job Pin
Jugortha16-Sep-08 8:08
memberJugortha16-Sep-08 8:08 
GeneralRe: Nice job Pin
Uwe Keim16-Sep-08 9:25
sitebuilderUwe Keim16-Sep-08 9:25 
GeneralRe: Nice job Pin
finiduck30-Oct-08 2:32
memberfiniduck30-Oct-08 2:32 
GeneralExcellent Pin
richardw4811-Sep-08 7:58
memberrichardw4811-Sep-08 7:58 
GeneralGreat Job Pin
netizenk27-Aug-08 14:32
professionalnetizenk27-Aug-08 14:32 
GeneralCould not find a part of the path Pin
RugbyLeague15-Aug-08 3:35
memberRugbyLeague15-Aug-08 3:35 
GeneralRe: Could not find a part of the path Pin
finiduck30-Oct-08 2:27
memberfiniduck30-Oct-08 2:27 
GeneralExcellent Pin
Emad Attia30-Jul-08 8:04
memberEmad Attia30-Jul-08 8:04 
GeneralDoesn't work on Windows 2000 Pin
csharpguy7612-Jun-08 22:50
membercsharpguy7612-Jun-08 22:50 
GeneralRe: Doesn't work on Windows 2000 Pin
juggler30-Nov-09 4:44
memberjuggler30-Nov-09 4:44 
GeneralExtended Version Pin
Uwe Keim22-May-08 7:27
sitebuilderUwe Keim22-May-08 7:27 
GeneralRe: Extended Version Pin
Uwe Keim22-May-08 7:27
sitebuilderUwe Keim22-May-08 7:27 
GeneralSimple, to the point, elegant Pin
Mike Devenney20-May-08 5:31
memberMike Devenney20-May-08 5:31 
GeneralTo log onto another DOMAIN use change as others have shown. Pin
ToddJunk13-May-08 9:51
memberToddJunk13-May-08 9:51 
GeneralRe: To log onto another DOMAIN use change as others have shown. Pin
Member 324928019-Jun-08 4:01
memberMember 324928019-Jun-08 4:01 
QuestionWhat about GINA Pin
thanatoster5-May-08 23:18
memberthanatoster5-May-08 23:18 
GeneralExcellent Pin
ffowler24-Apr-08 11:34
memberffowler24-Apr-08 11:34 
GeneralWonderful Pin
dapoussin7-Apr-08 7:20
memberdapoussin7-Apr-08 7:20 
GeneralGreat work Pin
m.conta7-Apr-08 6:33
memberm.conta7-Apr-08 6:33 
Generalnot working for (remote) file operations Pin
tomtom198026-Mar-08 3:40
membertomtom198026-Mar-08 3:40 
GeneralRe: not working for (remote) file operations Pin
tomtom198026-Mar-08 22:50
membertomtom198026-Mar-08 22:50 
GeneralRe: not working for (remote) file operations Pin
Uwe Keim26-Mar-08 22:56
sitebuilderUwe Keim26-Mar-08 22:56 
GeneralRe: not working for (remote) file operations Pin
tomtom198027-Mar-08 4:09
membertomtom198027-Mar-08 4:09 
GeneralRe: not working for (remote) file operations Pin
stancrm4-Dec-08 23:58
memberstancrm4-Dec-08 23:58 
GeneralDoes not work for remote local accounts [modified] Pin
Jonnystar13-Mar-08 13:46
memberJonnystar13-Mar-08 13:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web02 | 2.8.190214.1 | Last Updated 11 Apr 2005
Article Copyright 2005 by Uwe Keim
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid