I would like to temporarily impersonate a domain user account to read in a file on a network drive from an ASP.NET site.
I would rather not set up impersonation for the entire site or set up a mapped drive on the server.
I would like to temporarily impersonate a domain user account to read in a file on a network drive from an ASP.NET site.
I would rather not set up impersonation for the entire site or set up a mapped drive on the server.
I ended up using code from Michiel van Otegem: WindowsImpersonationContext made easy and added an implementation of IDisposable. I found this in another question about impersonation in ASP.NET.
Usage:
Code:
Actually, the process is quite easy, you can use code like this;
Your code goes in the "Now impersonating" section. the KEY is the finally block, this is VERY important. You can view this MSDN article for full details on how this works.