Click here to Skip to main content
15,891,719 members
Articles / Web Development / ASP.NET
Article

Impersonation using Code

Rate me:
Please Sign up or sign in to vote.
1.36/5 (5 votes)
8 Mar 20072 min read 31.6K   21   3
Impersonation using Code

Impersonation using Code

I was reading about impersonation and delegation. The most common way I found for impersonation is to write a tag "<impersonate = "true" username="Name" password ="password" />" in web.config file. This will impersoname specific user to each request. I want to impersonate network user for only one request.

In my project I have to create a directory which is shared on network and has rights set to network user. If I use impersonation in web.config file, all the request of my application executes under the rights of that network user ehich can be a security threat. I need to impersonate the user for a single request in which I need to create a directory on network shared location.

Below is the code that impersonate the "Anonymous" (is explained in paragraph below code" user for perticular request.


HttpContext context = HttpContext.Current; <br /><br />/* Get the service provider from the context */<br />IServiceProvider iServiceProvider = context as IServiceProvider; <br /><br />/*Get a Type which represents an HttpContext */<br />Type httpWorkerRequestType = typeof(HttpWorkerRequest); <br /><br />/* Get the HttpWorkerRequest service from the service provider <br />NOTE: When trying to get a HttpWorkerRequest type from the <br />HttpContext unmanaged code permission is demanded. */<br /><br />HttpWorkerRequest httpWorkerRequest =<br />iServiceProvider.GetService(httpWorkerRequestType) as HttpWorkerRequest; <br /><br />/* Get the token passed by IIS */<br />IntPtr ptrUserToken = httpWorkerRequest.GetUserToken(); <br /><br />/* Create a WindowsIdentity from the token */<br />WindowsIdentity winIdentity = new WindowsIdentity(ptrUserToken); <br /><br />Response.Write("Before impersonation: " + WindowsIdentity.GetCurrent().Name + "<br>"); <br /><br />/* Impersonate the user */<br />WindowsImpersonationContext impContext = winIdentity.Impersonate(); Response.Write("Impersonating: " + WindowsIdentity.GetCurrent().Name + "<br>"); <br /><br />/* Place resource access code here <br />You can write code for File Access, Directory Creation or<br />delete file or folde */<br /><br /><br />/* Stop impersonating */<br />impContext.Undo(); <br /><br />Response.Write("After Impersonating: " + WindowsIdentity.GetCurrent().Name + "<br>");

Fig - (1) Impersonate user using code.

By saying "Anonymous" user, the user which is set for Anonymous account in IIS. By default this will be "IUser_MachineName". Change this to Network user (or user you want to impersonate) by entering username and password and uncheck "Allow IIS to control password" check box.

To do this click on "Start –> Run " and write " inetmgr". It will show IIS. Right click on virtual directory of your application and select property. Click on "Directory Security" tab. Click "Edit" button at "Anonymous Access and Authentication Cotrol" panel. Here you can change user name and password.

Happy programming.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
SuggestionI was refactor to a metod your code. Pin
Halil Güneş21-Jul-11 1:12
Halil Güneş21-Jul-11 1:12 
GeneralHi Pin
dhanashree_129-Nov-07 23:27
dhanashree_129-Nov-07 23:27 
Hi
After i got u r code i replaced my code with yours with Windows Impersonation but still i am not able to access the Shared Folders that are available on the network.

Avinash
GeneralUsing Pin
Kevin I8-Mar-07 10:35
Kevin I8-Mar-07 10:35 

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.