TIP: Accessing Resources as the Web Application Pool Account Instead of the Current User

Generally custom web applications that interact with K2 impersonate the current user for security by setting this attribute in the web.config file:

  <identity impersonate="true"/>

But sometimes you want to perform some code in the security context of the account that runs the application pool.  There is a simple way to do this in code:

using (System.Web.Hosting.HostingEnvironment.Impersonate())
{
           //Code in here will execute as the application pool identity instead of the current user

}

I knew it could be done, but it took me a couple of hours of searching to find it.  Here is a great article with some ideas on the subject: 

http://httpcode.com/blogs/PermaLink,guid,961b0893-cebf-47e2-a608-9562136bcad4.aspx


Posted Fri, Aug 21 2009 9:44 AM by DavidL