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();
  }
 }
}

61 comments:

Unknown 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

Unknown 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

Unknown 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

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

nilashri 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

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

ganga pragya 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

tamilsasi said...

Inspiring writings and I greatly admired what you have to say , I hope you continue to provide new ideas for us all and greetings success always for you.
Keep update more information..


Selenium training in bangalore
Selenium training in Chennai
Selenium training in Bangalore
Selenium training in Pune
Selenium Online training
Selenium interview questions and answers

cdcdcdcd said...



Thanks for sharing this quality information with us. I really enjoyed reading.

Deer Hunting Tips Camping Trips Guide DEER HUNTING TIPS travel touring tips

cdcdcdcd said...



thanks for posting this blog. its really very helpful for us.

Still Hunting Method
Hunting psych tips Survival Tips Travel Touring Tips

cdcdcdcd said...


Well thanks for the information, do not forget to visit my blog too.

travel trekking tips
see the link Tent Camping 101 Exploring Smithriver

jefrin said...

Your very own commitment to getting the message throughout came to be rather powerful and have consistently enabled employees just like me to arrive at their desired goals.
Data science Course Training in Chennai | Data Science Training in Chennai
RPA Course Training in Chennai | RPA Training in Chennai
AWS Course Training in Chennai | AWS Training in Chennai

Training for IT and Software Courses said...

This is the exact information I am been searching for, Thanks for sharing the required infos with the clear update and required points. To appreciate this I like to share some useful information.

perl training institutes in bangalore

perl training in bangalore

best perl training institutes in bangalore

perl training course content

perl training interview questions

perl training & placement in bangalore

perl training center in bangalore

Ecare Technologies said...

Thanks for sharing this blog, this blog is very helpful information for every one...
RPA Training in Bangalore - Marathahalli | RPA Training Institutes | RPA Course Fees and Content | RPA Interview Questions
- ECare Technologies located in Marathahalli - Bangalore, is one of the best RPA Training institutes with 100% Placement support. RPA Training in Bangalore provided by RPA Certified Experts and real-time Working Professionals with handful years of experience in real-time RPA Projects.

Priyanka said...

Attend The Data Analytics Courses From ExcelR. Practical Data Analytics Courses Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Data Analytics Courses.
ExcelR Data Analytics Courses
Data Science Interview Questions

IT Software Training Institute said...

Internet of things iot Course in Bangalore with 100% placement. We are the Best Internet of things iot Course Institute in Bangalore. Our Agile testing course and Certification courses are taught by working professionals who are experts in Internet of things iot.

Internet of things iot Training in Bangalore

Internet of things iot course in bangalore

Internet of things iot in bangalore

Internet of things iot classes in bangalore

Internet of things iot course institute in bangalore

Agile testing course and Certification course syllabus

best Internet of things iot course

Internet of things iot course centers

IT Software Training Institute said...


Being new to the blogging world I feel like there is still so much to learn. Your tips helped to clarify a few things for me as well as giving.

Italian Language Training In Bangalore

Italian Training Institute in Bangalore

Italian Institute in Bangalore

Italian Language Institute

Indian Institute of Italian & Italian Language

Italian Learning

Italian Language Institute in Bangalore

Italian Courses

Italian language course in Bangalore

learn Italian language in Bangalore

Italian language basics learning in Bangalore

learn Italian grammar in Bangalore

Italian learning centres contact addresses in Bangalore

Italian grammer tuition phone numbers in Bangalore

reviews of Italian language learning centres in Bangalore

ratings of Italian spoken classes in Bangalore

Funclub casino online review said...

The Silveredge Casino perform extremely well with its regular promotions and a wide range of silveredge casino games online .

Data Science pune said...
This comment has been removed by the author.
tejaswini said...

I would like to say that this blog really convinced me to do it! Thanks, very good post.
data science course

Training for IT and Software Courses said...

Wow it is really wonderful and awesome thus it is very much useful for me to understand many concepts and helped me a lot.

SAP BASIS Online Training

SAP BASIS Classes Online

SAP BASIS Training Online

Online SAP BASIS Course

SAP BASIS Course Online

Training for IT and Software Courses said...

This is excellent information. It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful to me.

ServiceNow training in bangalore

Best ServiceNow Training Institutes in Bangalore

devi said...

This is a wonderful article, Given so much info in it, These type of articles keeps the users interest in the website, and keep on sharing more ... good luck Data Science Training In Chennai | Certification | Data Science Courses in Chennai | Data Science Training In Bangalore | Certification | Data Science Courses in Bangalore | Data Science Training In Hyderabad | Certification | Data Science Courses in hyderabad | Data Science Training In Coimbatore | Certification | Data Science Courses in Coimbatore | Data Science Training | Certification | Data Science Online Training Course

Data Science said...

Awesome blog with unique information and valuable content thanks for sharing.
Data Analytics Course Online

Ethical Hacking Course said...

Awesome blog with interesting topic and information provided was very valuable thank you.
Ethical Hacking Course in Bangalore

Data Science said...

Awesome article with valuable information and excellent writing skills found very useful thanks for Sharing you.
typeerror nonetype object is not subscriptable

Data Science Institute In Banglore said...


I'm really thankful that I read this. It's extremely valuable and quite informative and I truly learned a great deal from it.
Data Science Training Institute in Bangalore

Artificial Intelligence Course said...

You have completed certain reliable points there. I did some research on the subject and found that almost everyone will agree with your blog.

Artificial Intelligence Course in Bangalore

EXCELR said...

Im really impressed by it.I am happy for sharing on this blog its awesome blog I really impressed. thanks for sharing. Great efforts. data science training

360digiTMG Training said...

This post is very simple to read and appreciate without leaving any details out. Great work!
Best Institutes For Digital Marketing in Hyderabad

360digitmg said...

Such a very useful article. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article.
360DigiTMG data science course in ECIL

Data Science Course in Chandigarh said...

Truly, this article is really one of the very best in the history of articles. I am a antique ’Article’ collector and I sometimes read some new articles if I find them interesting. And I found this one pretty fascinating and it should go into my collection. Very good work!

Data Science Course in Mangalore

Big Data 360 said...

I see some amazingly important and kept up to length of your strength searching for in your on the site

Data Science Course in Trichy

360digiTMG Training said...


I see some amazingly important and kept up to length of your strength searching for in your on the site
Digital Marketing Training Institutes in Hyderabad

AI Expert said...

I just got to this amazing site not long ago. I was actually captured with the piece of resources you have got here. Big thumbs up for making such wonderful blog page!
<a href="https://360digitmg.com/india/artificial-intelligence-ai-and-deep-learning-in-pune
>artificial intelligence course in pune</a>

360digiTMG Training said...

This is a really very nice post you shared, I like the post, thanks for sharing..
Best Digital Marketing Courses in Hyderabad

traininginstitute said...



I think I have never seen such blogs before that have completed things with all the details which I want. So kindly update this ever for us.

business analytics course

Anonymous said...

Very much impressive blog post,
Digital Marketing Course with Internship

Appslure said...

Hello, This is very Helpful Content. this article really getting me an aspiration to do the same as you
We are Mobile App Development Company

traininginstitute said...

Really nice and interesting post. I was looking for this kind of information and enjoyed reading this one. Keep posting. Thanks for sharing.


best data science institute in hyderabad

webtrills said...
This comment has been removed by the author.
webtrills said...

App Development Services

traininginstitute said...

This is an excellent post . thanks for sharing it. It is really what I wanted to see. I hope in the future you will continue to share such an excellent post.
business analytics course

traininginstitute said...

This is an excellent post . thanks for sharing it. It is really what I wanted to see. I hope in the future you will continue to share such an excellent post.
business analytics course

traininginstitute said...

It's really nice and meaningful. it's a really cool blog.you have really helped lots of people who visit blogs and provide them useful information
digital marketing courses in hyderabad with placement

ram said...

Infycle Technologies offers the Best Data training in chennai and is widely known for its excellence in giving the best Data Science Certification course in Chennai. Providing quality software programming training with 100% placement & to build a solid career for every young professional in the software industry is the ultimate aim of Infycle Technologies. Apart from all, the students love the 100% practical training,
which is the specialty of Infycle Technologies. To proceed with your career with a solid base, reach Infycle Technologies through 7502633633.

Maneesha said...

The writer is enthusiastic about purchasing wooden furniture on the web and his exploration about best wooden furniture has brought about the arrangement of this article.
data scientist course in hyderabad

Professional Course said...

A good blog always contains new and exciting information, and reading it I feel like this blog really has all of these qualities that make it a blog.

AI Training in Bangalore

SREEJITHA said...

Clinician is the future of modern medicine. It enables physicians, consumers, and healthcare companies to use a basic yet powerful technical instrument, namely a smartphone.
https://clinicianapp.in/

Maneesha said...

Thanks for sharing this information. I really like your blog post very much. You have really shared an informative and interesting blog post with people..
data science training in hyderabad

appconsultio said...

It was nice reading this post. I will suggest to write more post  like this, Now I would like to highlight our services. Please visit our site for more details. Appconsultion is the best mobile app development company in Mumbai. We develop all kinds of android mobile apps, iOS mobile apps or cross-platform mobile apps at a competitive rate.
website design service in Pune, digital marketing company in Mumbai
top mobile app development company
mobile app development company in Delhi
mobile app development company in Patna
mobile app development company in Mumbai
mobile app development company in Pune
Appconsultio Websolutions is the leading best mobile app development company in India.

Unknown said...

Truly, this article is really one of the very best in the history of articles. I am a antique ’Article’ collector and I sometimes read some new articles if I find them interesting. And I found this one pretty fascinating and it should go into my collection. Very good work! data analytics course in mysore

Unknown said...

Very nice blog and articles. I am realy very happy to visit your blog. Now I am found which I actually want. I check your blog everyday and try to learn something from your blog. Thank you and waiting for your new post. business analytics course in surat

Unknown said...

I finally found great post here.I will get back here. I just added your blog to my bookmark sites. thanks.Quality posts is the crucial to invite the visitors to visit the web page, that's what this web page is providing.data science course data science course in kanpur

360DigiTMG said...

Thanks for such a great post and the review, I am totally impressed! Keep stuff like this coming.
business analytics course in hyderabad

Unknown said...

Incredibly in general very entrancing post. I was looking for such an information and savored the experience of scrutinizing this one.business analytics course in ghaziabad

Unknown said...

This is my first time i visit here and I found so many interesting stuff in your blog especially it's discussion, thank you. business analytics course in surat

Ramesh Sampangi said...

This blog is awesome and information. Keep sharing more blogs with us. Thank you.
Data Science Course in Hyderabad

corewave said...

Helpful post Mobile App Development Company in Delhi

traininginstitute said...


As someone who is fascinated by data science, this post had my complete attention right from the word go. Every line has been written so perfectly, and I wonder why this writer is not publishing posts daily. The writing style is simple but elegant, and makes readers wanting for more, every single time.
mlops training

Website Development Services Noida said...

Are you searching for the best flutter app development company in Noida? If yes, you are at the right place to gain information about it. Recently, Google improved the assistance and increased the number of cross-platform apps that also provide the native user experience as the native android apps provide.