Can I turn off impersonation just in a couple instances

后端 未结 4 515
被撕碎了的回忆
被撕碎了的回忆 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:39

    I am not sure if this is the preferred approach but when I wanted to do this I new'd up an instance of a WindowsIdentity and called the Impersonate method. This allows subsequent code to impersonate a different Windows user. It returns a WindowsImpersonationContext that has an Undo method which reverts the impersonation context back again.

    0 讨论(0)
  • 2020-11-27 19:46

    I just ended up giving the folders write permissions to "Authenticated Users"

    0 讨论(0)
  • 2020-11-27 19:49

    You could turn off authentication for the page and then manually impersonate the authenticated user during the remainder of your code.

    http://support.microsoft.com/kb/306158

    This has a reference to that last part, but basically you impersonate User.Identity

    This will mean you will have to impersonate at the beginning of any call to the page, turn it off when you need it off, then turn it back on when you are done, but it should be a workable solution.

    0 讨论(0)
  • 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 { }
    }
    
    0 讨论(0)
提交回复
热议问题