Can I turn off impersonation just in a couple instances

后端 未结 4 531
被撕碎了的回忆
被撕碎了的回忆 2020-11-27 18:53

I have an app that has impersonation used throughout. But when a user is logged in as an admin, a few operation require them to write to the server itself. Now if these us

4条回答
  •  迷失自我
    2020-11-27 19:58

    Make sure the Application Pool do have the proper rights that you need.

    Then, when you want to revert to the application pool identity... run the following:

    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 { }
    }
    

提交回复
热议问题