Ryan Michela
Ryan Michela

Reputation: 8374

Elevate impersonated credentials to app pool credentials in IIS

I am using impersonation in an application in IIS. I need to write a file to the file system, but don't want to use the impersonated user's credentials in this one case. I need to use the credentials of the app pool.

How do I temporarially elevate a worker process' credentials from impersonated credentials to the app pool's credentials?

Upvotes: 4

Views: 2163

Answers (1)

Ryan Michela
Ryan Michela

Reputation: 8374

Found the answer after much Googling.

using System.Security.Principal;
WindowsImpersonationContext ctx = 
      WindowsIdentity.Impersonate(System.IntPtr.Zero);
//do stuff in app pool's security context
ctx.Undo();

http://www.mindsharpblogs.com/todd/archive/2005/05/03/467.aspx

Upvotes: 5

Related Questions