使用Windows身份验证在MVC应用程序中模拟Active Directory用户

时间:2014-03-14 13:42:59

标签: asp.net-mvc asp.net-mvc-4 iis windows-authentication impersonation

我正在为Intranet MVC应用程序构建管理模块。此应用程序实现Windows身份验证(用户自动登录)。

目前,我基于他们的HttpContext.User.Identity数据建立了所有用户体验。

我需要做的是能够冒充用户,这样我就可以在遇到问题时复制他们的经验。

使用表单身份验证,这非常简单......

我尝试替换HttpContext中的IPrincipal.User对象,但这只有一个getter而不是setter。

任何指针都会非常感激。

感谢。

1 个答案:

答案 0 :(得分:2)

 using (new Impersonation()){
  // now working in context of whatever user you want
 }

这是班级

 [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
    public class Impersonation : IDisposable
    {
    private readonly SafeTokenHandle _handle;
    private readonly WindowsImpersonationContext _context;

    //const int Logon32LogonNewCredentials = 9; 
    private const int Logon32LogonInteractive = 2;

    public Impersonation()
    {
        var domain = "your domain;
        var username = "the user";
        var password = "their password";
        var ok = LogonUser(username, domain, password, Logon32LogonInteractive, 0, out _handle);
        if (!ok)
        {
            var errorCode = Marshal.GetLastWin32Error();
            throw new ApplicationException(string.Format("Could not impersonate the elevated user.  LogonUser returned error code {0}.", errorCode));
        }
        _context = WindowsIdentity.Impersonate(_handle.DangerousGetHandle());
    }

    public void Dispose()
    {
        _context.Dispose();
        _handle.Dispose();
    }

    [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
    private static extern bool LogonUser(String lpszUsername, String lpszDomain, String lpszPassword, int dwLogonType, int dwLogonProvider, out SafeTokenHandle phToken);

    public sealed class SafeTokenHandle : SafeHandleZeroOrMinusOneIsInvalid
    {
        private SafeTokenHandle()
            : base(true) { }

        [DllImport("kernel32.dll")]
        [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
        [SuppressUnmanagedCodeSecurity]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool CloseHandle(IntPtr handle);

        protected override bool ReleaseHandle()
        {
            return CloseHandle(handle);
        }
    }
}