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

68 comments:

  1. 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

    ReplyDelete
  2. 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

    ReplyDelete

  3. 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

    ReplyDelete
  4. 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

    ReplyDelete
  5. 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

    ReplyDelete
  6. 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

    ReplyDelete
  7. 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

    ReplyDelete


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

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

    ReplyDelete
  9. 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

    ReplyDelete
  10. 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.

    ReplyDelete
  11. 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

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

    ReplyDelete
  13. This comment has been removed by the author.

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

    ReplyDelete
  15. 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

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

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

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

    ReplyDelete

  19. 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

    ReplyDelete
  20. 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

    ReplyDelete
  21. 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

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

    ReplyDelete
  23. 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

    ReplyDelete
  24. 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

    ReplyDelete
  25. 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

    ReplyDelete

  26. 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

    ReplyDelete
  27. 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>

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

    ReplyDelete


  29. 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

    ReplyDelete
  30. 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

    ReplyDelete
  31. 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

    ReplyDelete
  32. This comment has been removed by the author.

    ReplyDelete
  33. 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

    ReplyDelete
  34. 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

    ReplyDelete
  35. 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

    ReplyDelete
  36. 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.

    ReplyDelete
  37. 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

    ReplyDelete
  38. 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

    ReplyDelete
  39. 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/

    ReplyDelete
  40. 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

    ReplyDelete
  41. 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.

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

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

    ReplyDelete

  44. 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

    ReplyDelete
  45. 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.

    ReplyDelete
  46. we must say, your blog never fails to impress us. keep sharing.

    Join Maven Technology to excel in this field by leveraging quality app development skills and experience to deliver top-notch Android applications that drive business success. Let's rock the market by shaping the future of Mobile App Development.

    Contact us and set a new benchmark for tech footprints today.

    Call us : +91 8851223376
    Email us : contact@maventechie.com
    Website : maventechie.com.

    ReplyDelete
  47. Great explanation about impersonation of web users in ASP.NET and SharePoint. The concept of executing code under another user’s identity is very useful for secure resource access in enterprise applications.

    Learning such backend concepts helps developers build secure systems. Platforms like Login360 help learners gain practical knowledge in Data Science and Full Stack Development

    ReplyDelete
  48. For developers building programming skills, Login360’s Java Course and Python Course also provide hands-on learning

    ReplyDelete
  49. For those who are also interested in strengthening their technical skills alongside blogging, this might be useful:
    Java Course for Beginners

    Thanks for providing such an easy-to-use platform for content creators!

    ReplyDelete
  50. Thanks for the valuable information! You can also visit: Digital Marketing Training in Chennai

    ReplyDelete
  51. Loved the clarity in your explanation, very easy to follow.
    It’s always great to find blogs like this.
    If anyone is interested in learning more, this helped me:
    Data Science Course with Placement
    Plus another great option here:
    Best IT Training Institute in Coimbatore

    ReplyDelete
  52. For those starting from scratch, I suggest looking for AWS Cloud Practitioner Training Electronic City to build a strong foundation.

    ReplyDelete
  53. I enjoyed reading this informative blog.
    Helpful course link: Data Science Course in Coimbatore

    ReplyDelete