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
Tuesday, June 19, 2007
Impersonation in C#
Subscribe to:
Post Comments (Atom)
23 comments:
Thanks a lot.. it was really helpful.
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.
Thanks ! It's very helpful !
Thank you for this code. It has helped me .
Bless you
Zeinah
Fantastic. I have been looking for this for a while. Thank you!
I like it.
Thanks, very helpful.
hope to have a demo
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.
Thanks for the solution - it's the first one among LOTS of posted solutions which really actually works.
Thanks a lot!
this code doesn't work with seven.
The error is "error 1346".
Any idea ?
Thks
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
Thank you so much its perfect :)
kazim
can u pls send me a code of impersonate at my id jesikasoni@yahoo.com ..
Thanx..
Download link is down =(
The link is not working... couldn't get myself enjoyed.
Hello,
Thanks for this site. Unfortunatly, the site which hosted the zip sample seems .. to have been sold out :)
Manitra.
Sniffff, the link is down ;'(
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
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.
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!
chirurgie esthetique tunisie
botox Tunisie
Worked! :)
Post a Comment