Tuesday, June 19, 2007

Impersonation in C#

Imagine you are going to create an application that have a method which is working with a file named Test.txt. When you run the application your code throws an error containing this message : "Access Denied on C:\Test\test.txt". After reviewing the code you find out that because the current user of windows does not have access to "C:\Test" directory. Now you want to force your application to use another User information in that block of code, we call this procedure Impersonation.

There is some classes in System.Security.Principal which helps you to achieve this goal. WindowsIdentity and WindowsImpersonationContext are two classes which we are going to use. We want to use Impersonate() method of WindowsIdentity which return a WindowsImpersonationContext. Then after you 've finished working with your file after calling Impersonate() method, you can return you current login of windows using Undo() method of your WindowsImpersonationContext instance. like this:

IntPtr accessToken = IntPtr.Zero;
....
//You have to initialize your accessToken with API calling
....
WindowsIdentity identity = new WindowsIdentity(accessToken);
WindowsImpersonationContext context = identity.Impersonate();
...
// Now your code is using the new WindowsLogin and you can do what ever this login can do
...

//Now you can return to your current login of Windows
context.Undo();


You can download the complete sample code here:
http://www.tabatabaei.info/csharpsamples/Impersonation.zip

21 comments:

killercoder said...

Thanks a lot.. it was really helpful.

Mohamed Saif said...

This is very helpful. Simple and direct, just the way I like it. I used your sample in production website where i don't have a write permission in the website folders. I used the ftp account to write the required files.

tan long said...

Thanks ! It's very helpful !

Anonymous said...

Thank you for this code. It has helped me .
Bless you
Zeinah

Anonymous said...

Fantastic. I have been looking for this for a while. Thank you!

Anonymous said...

I like it.

Fabri said...

Thanks, very helpful.

Jack said...

hope to have a demo

Anonymous said...

Finally, someone got it right!

A lot of sites have similar code but this code actually works and is very simple to follow.

Many Thanks.

Hitech said...

Thanks for the solution - it's the first one among LOTS of posted solutions which really actually works.

Thanks a lot!

Cyrille said...

this code doesn't work with seven.
The error is "error 1346".
Any idea ?

Thks

Amokrane Chentir said...

Can we use impersonation to access a file situated in a remote computer? (given the appropriates logon/pwd). If that's the case, do we need to register this logon/pwd on the local machine?

Thank you

Anonymous said...

Thank you so much its perfect :)


kazim

kinjal said...

can u pls send me a code of impersonate at my id jesikasoni@yahoo.com ..

Thanx..

clue said...

Download link is down =(

Anonymous said...

The link is not working... couldn't get myself enjoyed.

manitra.net said...

Hello,

Thanks for this site. Unfortunatly, the site which hosted the zip sample seems .. to have been sold out :)

Manitra.

Anonymous said...

Sniffff, the link is down ;'(

Web development Company said...

This is also a very good post which I really enjoyed reading. It is not everyday that I have the possibility to see something like this

Fiverr Work said...

Likes significantly make improvements to the acceptance of your site and along with the ideal choice rely; a lot of of us could possibly be attracted to your account. The corporations offering these companies, will source you with non-automated or robot likes and provide you with genuine Instagram likes. buyrealiglikes.com/instagram-like-buy/ The good issue about this is generally that they may be straight away despatched in direction of your account which implies you will not have to have to show up at that significantly. And when you're a company enterprise, escalating your Instagram likes will probably be beneficial.

Anonymous said...

Hi Masoud Tabatabaei! Can you please reupload the sample codes? I really need the solution urgently. Thank you in advance and have a great day!