将模拟凭据提升为IIS中的应用程序池凭据

Rya*_*ela 4 iis impersonation credentials

我在IIS中的应用程序中使用模拟.我需要将文件写入文件系统,但在这种情况下不想使用模拟用户的凭据.我需要使用应用程序池的凭据.

如何临时将工作进程的凭据从模拟凭据提升到应用程序池的凭据?

Rya*_*ela 5

经过谷歌搜索后找到了答案.

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

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