Tuesday, 22 September 2015

Impersonation of Web Users in ASP.NET/SharePoint 2013 without a password

There seemed to be a lack of samples available to demonstrate how Windows impersonation can be done within the context of a web application (such as SharePoint 2013 or ASP.NET). Most of the examples use the "LogonUser" Windows API call to get a user token. e.g. https://msdn.microsoft.com/en-us/library/chf6fbt4.aspx. However - that call requires a password to work. You don't really want all your user passwords to have to sit in a secure store to enable impersonation!

In my scenario, I had to write to a file through an existing COM Component via a .NET COM Interop library. It depended on the write operation being done from the context of a valid user - otherwise the file wouldn't be stamped correctly with author metadata.

To do this, I had to use an overload of the WindowsIdentity constructor which accepts a UPN (User Principal Name). From there, you can impersonate users within your code at will.

NOTE: the account that is doing the impersonation (e.g. svcSP) will need to have the "Act as Part of the Operating System" right as defined in your Local User Policy for this to work.

Code Sample:


void Main()
{
 var userName = "LOCALDEV\\david.klein";
 PrincipalContext ctx = new PrincipalContext(ContextType.Domain);

 var user = UserPrincipal.FindByIdentity(ctx, userName);

 if (user != null)
 {
 var upn = user.UserPrincipalName;
 Debug.Print(upn); 
 WindowsIdentity id = new WindowsIdentity(upn);
 WindowsImpersonationContext wic = id.Impersonate();    
 try
  {
   // Do what you need here under the impersonation context.
   var currentId = WindowsIdentity.GetCurrent().Name; 
   Debug.Print(currentId);
  }
  finally
  {
   wic.Undo();
  }
 }
}

22 comments:

Tweety P said...

Your content is awesome . You have done a great job and its very useful for me . I appreciate your effort and I hope that you will get more positive comments from the web users.
SMO Services Chennai

Giri Mani 2 said...

Got a creative information. Understand well in this. This gives the easy technique of experiment. New technologies are developed more. so techniques are also improved. Thank you for this information.
Digital Marketing Company in Chennai

Xplorant Seo said...

Wonderful blog.. Thanks for sharing informative blog.. its very useful to me..

iOS Training in Chennai

Evangeline Mitul said...

Thank you for taking the time to provide us with your valuable information. We strive to provide our candidates with excellent care and we take your comments to heart.As always, we appreciate your confidence and trust in us.

Best Dental Clinic In Velachery

john stany said...


Nice it seems to be good post... It will get readers engagement on the article since readers engagement plays an vital role in every

blog.. i am expecting more updated posts from your hands.
Android App Development Company
mobile App Development Company

john stany said...

I am expecting more interesting topics from you. And this was nice content and definitely it will be useful for many people.
iOS App Development Company
iOS App Development Company

louis philip said...

These ways are very simple and very much useful, as a beginner level these helped me a lot thanks fore sharing these kinds of useful and knowledgeable information.
Fitness SMS
Fitness Text
Salon SMS
Salon Text
Investor Relation SMS
Investor Relation Text

sunitha vishnu said...

great and nice blog thanks sharing..I just want to say that all the information you have given here is awesome...Thank you very much for this one.
web design Company
web development Company
web design Company in chennai
web development Company in chennai
web design Company in India
web development Company in India

isabella jacob said...

it is really amazing...thanks for sharing....provide more useful information...
Mobile app development company

isabella jacob said...

it is really amazing...thanks for sharing....provide more useful information...
Mobile app development company

Pradeep Ogen said...

Get Details of Flooring Tiles Manufacturers And Dealers. ... Heat Resistance Tiles, Heat Resistant Terrace Tiles. Thanks you for sharing
tile bonder manufacturer in delhi

Pradeep Ogen said...

Lifestyle Magazine India; Lifestyle is a elite magazine circulated to the elite at page parties. Lifestyle Magazine also circulated to the elite at clubs, lounge bars etc.
Lifestyle Magazine

Nadeem Malik said...

hi, nice information is given in this blog. Thanks for sharing this type of information, it is so useful for me. nice work keep it up.
digital marketing company in delhi

Akash Thakur said...

nice topic which you have choose.
second is, the information which you have provided is better then other blog.
so nice work keep it up. And thanks for sharing.
Epoxy Grout manufacturer in delhi

Vikash Kumar said...

Laminated Doors manufacturer in hubli

Amase LED Lighting said...

hi, nice information is given in this blog. Thanks for sharing this type of information, it is so useful for me. nice work keep it up. led lawn lights in delhi

Afiah B said...

All are saying the same thing repeatedly, but in your blog I had a chance to get some useful and unique information, I love your writing style very much, I would like to suggest your blog in my dude circle, so keep on updates.
Java training in Pune

Java interview questions and answers

Java training in Chennai | Java training institute in Chennai | Java course in Chennai

Java training in Bangalore | Java training institute in Bangalore | Java course in Bangalore

john jersy said...

This is most informative and also this post most user friendly and super navigation to all posts... Thank you so much for giving this information to me.. 
python course in pune
python course in chennai
python course in Bangalore

Nila shri said...

Thanks for such a great article here. I was searching for something like this for quite a long time and at last I’ve found it on your blog. It was definitely interesting for me to read  about their market situation nowadays.
Data Science course in Chennai | Best Data Science course in Chennai
Data science course in bangalore | Best Data Science course in Bangalore
Data science course in pune | Data Science Course institute in Pune
Data science online course | Online Data Science certification course-Gangboard
Data Science Interview questions and answers

ais a said...

Nice tutorial. Thanks for sharing the valuable information. it’s really helpful. Who want to learn this blog most helpful. Keep sharing on updated tutorials…
Best Devops training in sholinganallur
Devops training in velachery
Devops training in annanagar
Devops training in tambaram

kevin antony said...

I appreciate your efforts because it conveys the message of what you are trying to say. It's a great skill to make even the person who doesn't know about the subject could able to understand the subject . Your blogs are understandable and also elaborately described. I hope to read more and more interesting articles from your blog. All the best.

rpa training in Chennai | rpa training in bangalore | best rpa training in bangalore | rpa course in bangalore | rpa training institute in bangalore | rpa training in bangalore | rpa online training

genga g said...

Great post! I am actually getting ready to across this information, It’s very helpful for this blog.Also great with all of the valuable information you have Keep up the good work you are doing well.
angularjs interview questions and answers

angularjs Training in bangalore

angularjs Training in bangalore

angularjs interview questions and answers

angularjs Training in marathahalli

angularjs interview questions and answers