我可以在几个实例中关闭模拟

nas*_*ski 17 c# asp.net impersonation

我有一个在整个过程中使用模拟的应用程序.但是当用户以管理员身份登录时,一些操作要求他们写入服务器本身.现在,如果这些用户对实际服务器没有权限(有些用户没有),则不会让他们写.

我想要做的就是关闭几个命令的模拟.

有没有办法做这样的事情?

using(HostingEnvironment.Impersonate.Off())
  //I know this isn't a command, but you get the idea?
Run Code Online (Sandbox Code Playgroud)

谢谢.

Max*_*ler 21

确保应用程序池确实具有您需要的适当权限.

然后,当您想要恢复到应用程序池标识时...运行以下命令:

private WindowsImpersonationContext context = null;
public void RevertToAppPool()
{
    try
    {
        if (!WindowsIdentity.GetCurrent().IsSystem)
        {
            context = WindowsIdentity.Impersonate(System.IntPtr.Zero);
        }
    }
    catch { }
}
public void UndoImpersonation()
{
    try
    {
        if (context != null)
        {
            context.Undo();
        }
    }
    catch { }
}
Run Code Online (Sandbox Code Playgroud)