Click here to Skip to main content
11,410,536 members (59,059 online)
Click here to Skip to main content

A small C# Class for impersonating a User

, 11 Apr 2005 CPOL
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:

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
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

QuestionThanks!! PinmemberMember 867365126-Jan-14 23:55 
GeneralThanks! PinmemberMember 863079324-Jan-14 3:02 
GeneralThanks PinmemberAnkit Mishra9-Jan-14 23:01 
Questionhi, how can i use this class in workgroup network Pinmembermutlu6424-Dec-13 1:56 
Questiongreat job! Pinmemberbarronca9-Nov-13 3:55 
GeneralMy vote of 5 PinmemberGwunhar4-Sep-13 11:17 
GeneralMy vote of 5 PinprofessionalMenon Santosh8-Aug-13 7:02 
GeneralMy vote of 5 PinmemberSleazyOtto9-Jul-13 11:59 
GeneralMy vote of 5 PinmemberVHGN27-Jun-13 22:38 
QuestionThanks Bro! PinmemberDuke Ames22-Jun-13 15:57 
QuestionWindows Server 2012 PinmemberRiqiTang13-May-13 3:06 
GeneralMy vote of 5 Pinmember Orcun Iyigun7-Apr-13 22:45 
GeneralMy vote of 5 PinmemberSebastianN_N3-Apr-13 12:26 
QuestionAD Directory catastrophic failure PinmemberMacUseless12-Dec-12 3:52 
GeneralThanks! Pinmembersumit ch26-Nov-12 22:45 
Questionnot working for SharePoint 2010 code Pinmemberabcrahul29-Jun-12 4:12 
AnswerRe: not working for SharePoint 2010 code PinmemberWayne Dailey13-Sep-12 9:01 
AnswerRe: not working for SharePoint 2010 code PinsitebuilderUwe Keim13-Sep-12 19:10 
SuggestionImproved version without CA warnings and memory leaks [modified] Pinmemberpietvredeveld25-Apr-12 12:06 
GeneralRe: Improved version without CA warnings and memory leaks PinsitebuilderUwe Keim25-Apr-12 13:11 
Sure! You could simply link a here for others until I can include it.
Wollen Sie ganz einfach Ihre eigene Homepage erstellen, ohne HTML-Kenntnisse, einfach, professionell und mit viel Freude? Probieren Sie unser Desktop Content Management System (CMS) Zeta Producer für Windows aus. Komplett mit eigenem Shop, Gästebuch, Weblog, Bildergalerien, Integration von YouTube-Videos. Wir haben eine aktive Anwender-Community, schnellen Support, sympathische Support-Mitarbeiter.

GeneralRe: Improved version without CA warnings and memory leaks Pinmemberpietvredeveld26-Apr-12 11:01 
QuestionImpersonator and WebRequest doesn´t work Pinmembervicprepr18-Apr-12 5:15 
QuestionNice Pinmemberopik taufik29-Mar-12 19:06 
QuestionWorks as expected with ASP.NET MVC3 and .Net 4 in Windows 7 PinmemberReve1018-Mar-12 8:19 
Questionbravo PinmemberMember 863703210-Feb-12 6:51 
GeneralMy vote of 5 PinmemberKris Clayton27-Jan-12 1:19 
QuestionNot Working in Win Sever 2008 R2 Pinmemberanilkuchi1-Nov-11 12:39 
AnswerRe: Not Working in Win Sever 2008 R2 Pinmemberchardog3-Dec-11 13:51 
GeneralVery Nice & Easy PinmemberSrikanth. Vemulapalli16-Oct-11 20:43 
QuestionMy Vote Pinmemberahmed farag Ibrahim8-Oct-11 2:43 
AnswerRe: My Vote PinsitebuilderUwe Keim8-Oct-11 22:40 
GeneralMy vote of 5 Pinmemberbscaer7-Oct-11 11:11 
QuestionCall LogonUser PinmemberMember 822764117-Sep-11 14:38 
QuestionImpersonating to grant writing files privilege ? PinmemberPatrice Dargenton14-Sep-11 4:58 
AnswerRe: Impersonating to grant writing files privilege ? PinmemberPatrice Dargenton18-Sep-11 22:52 
QuestionWorks great for an ASP.Net Web application using IIS and SQl Server PinmemberTarang Deshpande30-Aug-11 12:23 
QuestionDon't working on Windows Server 2008 R2 and Windows 7 ;-( [modified] Pinmembertdelgreco22-Aug-11 2:17 
Generalconfirmed working on win2008 r2 web Pinmemberxtwinstarx2-Aug-11 17:40 
GeneralRe: confirmed working on win2008 r2 web PinsitebuilderUwe Keim2-Aug-11 20:18 
GeneralRe: confirmed working on win2008 r2 web Pinmemberanilkuchi1-Nov-11 12:37 
GeneralMy vote of 4 PinmemberAth110-Jun-11 4:25 
Generalipersonator ro sqlserver sspi windows authetication Pinmemberyacov859420-Mar-11 2:54 
GeneralUnknown Username or Bad Password PinmemberSingh Saab10-Nov-10 10:27 
GeneralMy vote of 5 PinmemberM Vaqqas26-Sep-10 22:23 
GeneralWindows 7 Pinmemberpsyeye21-Sep-10 6:35 
QuestionWithout "Act as part of operating system" PinmemberHabib Ahmed29-Aug-10 2:24 
GeneralMy vote of 5 PinmemberKarsten Otto27-Aug-10 8:35 
GeneralThanks a lot ^^ Pinmemberliani_8428-Jun-10 18:20 
QuestionDoes anyone have problems logging back into their machine after using Impersonator? Pinmemberdiyrection4-Jun-10 2:30 
AnswerRe: Does anyone have problems logging back into their machine after using Impersonator? PinmemberMember 375330916-Nov-11 23:26 

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

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

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