Click here to Skip to main content
Click here to Skip to main content

A small C# Class for impersonating a User

By , 11 Apr 2005
 

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

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

License

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
Member
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:
 
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
Some random fun stuff in German

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5 Pinmemberbscaer7 Oct '11 - 10:11 
QuestionCall LogonUser PinmemberMember 822764117 Sep '11 - 13:38 
QuestionImpersonating to grant writing files privilege ? PinmemberPatrice Dargenton14 Sep '11 - 3:58 
AnswerRe: Impersonating to grant writing files privilege ? PinmemberPatrice Dargenton18 Sep '11 - 21:52 
QuestionWorks great for an ASP.Net Web application using IIS and SQl Server PinmemberTarang Deshpande30 Aug '11 - 11:23 
QuestionDon't working on Windows Server 2008 R2 and Windows 7 ;-( [modified] Pinmembertdelgreco22 Aug '11 - 1:17 
Generalconfirmed working on win2008 r2 web Pinmemberxtwinstarx2 Aug '11 - 16:40 
GeneralRe: confirmed working on win2008 r2 web PinsitebuilderUwe Keim2 Aug '11 - 19:18 
GeneralRe: confirmed working on win2008 r2 web Pinmemberanilkuchi1 Nov '11 - 11:37 
GeneralMy vote of 4 PinmemberAth110 Jun '11 - 3:25 
Generalipersonator ro sqlserver sspi windows authetication Pinmemberyacov859420 Mar '11 - 1:54 
GeneralUnknown Username or Bad Password PinmemberSingh Saab10 Nov '10 - 9:27 
GeneralMy vote of 5 PinmemberM Vaqqas26 Sep '10 - 21:23 
GeneralWindows 7 Pinmemberpsyeye21 Sep '10 - 5:35 
QuestionWithout "Act as part of operating system" PinmemberHabib Ahmed29 Aug '10 - 1:24 
GeneralMy vote of 5 PinmemberKarsten Otto27 Aug '10 - 7:35 
Genial, simple and 100% working
GeneralThanks a lot ^^ Pinmemberliani_8428 Jun '10 - 17:20 
QuestionDoes anyone have problems logging back into their machine after using Impersonator? Pinmemberdiyrection4 Jun '10 - 1:30 
AnswerRe: Does anyone have problems logging back into their machine after using Impersonator? PinmemberMember 375330916 Nov '11 - 22:26 
GeneralMan yo usaved my life Pinmemberhesar5 May '10 - 6:00 
GeneralRe: Man yo usaved my life PinmvpUwe Keim5 May '10 - 6:55 
GeneralRe: Man yo usaved my life PinmemberMember 44398461 Jul '11 - 8:51 
GeneralThis 5 year old article is still helping to solve coding problems! PinmemberRCoate19 Apr '10 - 16:31 
GeneralRe: This 5 year old article is still helping to solve coding problems! PinmvpUwe Keim19 Apr '10 - 17:41 
Generalthanks Pingroupbitayeganeh5 Mar '10 - 21:32 
GeneralThanks Uwe. You realy helped me. PinmemberTamir Nof10 Feb '10 - 3:39 
GeneralWebsite deployed on https logonuser API stopped working [modified] PinmemberAmit Kumar Thakur13 Dec '09 - 19:17 
GeneralNice work, but help Pinmemberturbohansen21 Aug '09 - 13:10 
GeneralRe: Nice work, but help PinmemberBit-Smacker17 Sep '09 - 10:00 
GeneralNice one Pinmemberfluectho24 Jun '09 - 3:56 
GeneralGreat job PinmemberRichard Hollis20 Mar '09 - 11:43 
GeneralRe: Great job PinmemberMember 17527188 Apr '09 - 11:19 
GeneralThis does not seem to work if a Win XP workstation is on a WorkGroup... Pinmemberdanvmn2 Mar '09 - 4:44 
GeneralRe: This does not seem to work if a Win XP workstation is on a WorkGroup... PinmemberMattsterP11 Dec '09 - 15:32 
GeneralThanks Pinmembercarladinmc26 Feb '09 - 4:20 
QuestionCan I use this C# class in Vb project PinmemberMember 34815902 Feb '09 - 5:43 
QuestionHow do you use Impersonator without the using clause? Pinmemberdanvmn23 Jan '09 - 4:58 
AnswerRe: How do you use Impersonator without the using clause? PinsitebuilderUwe Keim23 Jan '09 - 5:02 
GeneralRe: How do you use Impersonator without the using clause? Pinmemberdanvmn23 Jan '09 - 8:20 
GeneralRe: How do you use Impersonator without the using clause? Pinmemberdanvmn23 Jan '09 - 8:50 
AnswerRe: How do you use Impersonator without the using clause? Pinmemberdanvmn23 Jan '09 - 5:57 
RantSecurity Risk PinmemberRobinVossen11 Jan '09 - 20:51 
GeneralRe: Security Risk Pinmemberhesar5 May '10 - 6:03 
GeneralOpenFileDialog PinmemberRugbyLeague15 Dec '08 - 4:54 
GeneralWell done!!! Pinmemberuhcl5 Nov '08 - 11:20 
GeneralNice job PinmemberJugortha16 Sep '08 - 7:08 
GeneralRe: Nice job PinsitebuilderUwe Keim16 Sep '08 - 8:25 
GeneralRe: Nice job Pinmemberfiniduck30 Oct '08 - 1:32 
GeneralExcellent PinmemberRVW11 Sep '08 - 6:58 
GeneralGreat Job Pinmembernetizenk27 Aug '08 - 13:32 

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.

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