Click here to Skip to main content
13,800,525 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:

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

PraiseExcellent time saver :) Pin
Simon Doll12-Dec-17 0:28
memberSimon Doll12-Dec-17 0:28 
QuestionVery useful and efficient, thank you! Pin
Member 1302101424-Feb-17 0:42
memberMember 1302101424-Feb-17 0:42 
PraiseThank you! Pin
tnewton12-Dec-16 9:18
membertnewton12-Dec-16 9:18 
PraiseThank you! Pin
Matthias Wirth4-Aug-16 4:33
memberMatthias Wirth4-Aug-16 4:33 
QuestionLOGON32_LOGON_INTERACTIVE, NetworkCredential Pin
EricBDev29-Jul-16 6:19
memberEricBDev29-Jul-16 6:19 
QuestionFileInfo.CopyTo won't work due to insufficient access rights Pin
helelark12319-Apr-16 21:57
memberhelelark12319-Apr-16 21:57 
AnswerRe: FileInfo.CopyTo won't work due to insufficient access rights Pin
Uwe Keim27-Jun-16 2:52
sitebuilderUwe Keim27-Jun-16 2:52 
QuestionFirmenwebseite Pin
Member 1241760026-Mar-16 1:41
groupMember 1241760026-Mar-16 1:41 
QuestionLogon failure: the user has not been granted the requested logon type at this computer' Pin
thund3rstruck2-Nov-15 11:44
memberthund3rstruck2-Nov-15 11:44 
QuestionIts helped me a lot and saved my time !!!! Pin
Rajeshrec27628-Aug-15 1:56
memberRajeshrec27628-Aug-15 1:56 
QuestionThanks vote 5! Pin
Member 1011648027-Aug-15 23:20
memberMember 1011648027-Aug-15 23:20 
QuestionAdding support for local (non-domain) user impersonation Pin
daniel zucker1-Jan-15 8:41
memberdaniel zucker1-Jan-15 8:41 
AnswerRe: Adding support for local (non-domain) user impersonation Pin
Uwe Keim1-Jan-15 8:51
sitebuilderUwe Keim1-Jan-15 8:51 
GeneralRe: Adding support for local (non-domain) user impersonation Pin
Member 117366362-Jun-15 10:25
memberMember 117366362-Jun-15 10:25 
AnswerThank you buddy Pin
aravinth santosh20-Dec-14 10:35
memberaravinth santosh20-Dec-14 10:35 
QuestionThanks! Pin
Emil Ferent18-Nov-14 12:28
memberEmil Ferent18-Nov-14 12:28 
GeneralThank you for posting Pin
Tyler Kalosza25-Jul-14 11:19
memberTyler Kalosza25-Jul-14 11:19 
QuestionGetting “Invalid token for impersonation - it cannot be duplicated” Pin
daniel zucker21-May-14 2:47
memberdaniel zucker21-May-14 2:47 
AnswerRe: Getting “Invalid token for impersonation - it cannot be duplicated” Pin
daniel zucker6-Jul-14 4:07
memberdaniel zucker6-Jul-14 4:07 
couldn't solve this since no one replied but I did implement a retry mechanism (a while loop that depends on this exception message: "Invalid token for impersonation - it cannot be duplicated.") as it always seem to work on the second time.
QuestionGreat Class - where is the best location for this class? Pin
Member 942814419-May-14 5:49
memberMember 942814419-May-14 5:49 
Bug64 Bit / Desktop Window Manager Pin
Roryap24-Apr-14 4:33
memberRoryap24-Apr-14 4:33 
QuestionThanks!!!! Pin
Member 1071915427-Mar-14 9:09
memberMember 1071915427-Mar-14 9:09 
QuestionThanks!! Pin
Member 867365126-Jan-14 23:55
memberMember 867365126-Jan-14 23:55 
GeneralThanks! Pin
Member 863079324-Jan-14 3:02
memberMember 863079324-Jan-14 3:02 
GeneralThanks Pin
Ankit Mishra9-Jan-14 23:01
memberAnkit Mishra9-Jan-14 23:01 

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
Web06 | 2.8.181215.1 | Last Updated 11 Apr 2005
Article Copyright 2005 by Uwe Keim
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid