Impersonate Domain User with Integrated Pipeline

前端 未结 2 486
隐瞒了意图╮
隐瞒了意图╮ 2020-12-04 12:35

In an local Intranet environment, are we doomed to use \"Classic\" pipeline mode in our App Pool if we want to use Impersonate our Windows domain users, or is there a new wa

2条回答
  •  无人及你
    2020-12-04 13:21

    No, but "Integrated" pipeline requires you manually impersonate the Windows Authenticated user. At least in IIS8.5, that is.

    Why? Classic impersonation break .NET's async features. Specifically, it is hard to manage the WindowsIdentity of a thread when it is being used by multiple users at the same time.

    How? Use a WindowsImpersonationContext e.g.

    // Start with identity assigned by IIS Application Pool
    var current = System.Security.Principal.WindowsIdentity.GetCurrent();
    
    // Enable Windows Authentication in ASP.NET *and* IIS, which ensures 
    // User.Identity is a WindowsIdentity
    WindowsIdentity clientId = (WindowsIdentity)User.Identity;
    
    // When 'using' block ends, the thread reverts back to previous Windows identity,
    // because under the hood WindowsImpersonationContext.Undo() is called by Dispose()
    using (WindowsImpersonationContext wic = clientId.Impersonate())
    {
        // WindowsIdentity will have changed to match clientId
        current = System.Security.Principal.WindowsIdentity.GetCurrent();
    }
    // Back to the original identity
    current = System.Security.Principal.WindowsIdentity.GetCurrent();
    

    Problems? Sometimes you need to use delegation instead of impersonation.

提交回复
热议问题