【问题标题】:Impersonating users through NTLM通过 NTLM 模拟用户
【发布时间】:2010-11-17 05:05:13
【问题描述】:

我有一个具有两个安全级别的内部应用程序。面向客户端的应用程序的 FormsAuthentication 和管理界面的 NTLM 集成身份验证。

只需使用 FormsAuthentication 类的方法创建正确的 .ASPXAUTH cookie,我就可以轻松地模拟客户端。但是到目前为止,为 NTLM 生成 HTTP 身份验证标头超出了我的范围。

当我发现这篇文章 (http://msdn.microsoft.com/en-us/library/ms998358.aspx#paght000025_usingimpersonation) 时,我抱有希望,但后来我意识到它只会创建一个上下文来在请求期间运行代码。而且我想切换我的整个会话以使服务器认为我正在使用另一个域登录。我对我的帐户有管理权限,所以这不是为了搞砸或窃取域密码。

有可能吗?谢谢。

【问题讨论】:

    标签: c# asp.net impersonation ntlm


    【解决方案1】:

    假设您使用登录表单 login.aspx 启用了表单身份验证的 ASP.NET 应用程序,并且您的用户存储在 DB 中。现在您希望同时支持表单和 Windows 身份验证。我就是这样做的:

    对于表单身份验证,我将 SQL DB 与用户表一起使用。我在此表中添加了名为 WindowsUserName 的新列,我将在其中以 COMPUTER\User 形式保存 Windows 用户名

    在 login.aspx 表单中我添加了一个方法,它将发送一个显示登录窗口的响应:

    private void ActivateWindowsLogin()
    {
        Response.StatusCode = 401;
        Response.StatusDescription = "Unauthorized";
        Response.End();
    }
    

    在某处我有一个类似<a href="login.aspx?use=windows">Admin</a>的链接

    在 login.aspx Page_Load 我已经添加了:

    if (Request.QueryString["use"] == "windows")
    {
        var windowsuser = Request.ServerVariables["LOGON_USER"];
        if (windowsuser.Length == 0)
            ActivateWindowsLogin();
        else
        {
            // get userId from DB for Windows user that was authenticated by IIS
            // I use userId in .ASPXAUTH cookie
            var userId = GetUserIdForWindowsUser(windowsuser);
            if (userId > 0) //user found
            {
                // here we get User object to check roles or other stuff
                var user = GetApplicationUser(userId);
                // perform additional checks here and call ActivateWindowsLogin()
                // to show login again or redirect to access denied page.
                // If everythig is OK, set cookie and redirect
                FormsAuthentication.SetAuthCookie(userId.ToString(), false);
                Response.Redirect(FormsAuthentication.GetRedirectUrl(userId.ToString(), false), true);
            }
            else //user not found
                ActivateWindowsLogin();
        }
    }
    else
    {
        //your Forms auth routine
    }
    

    GetUserIdForWindowsUser 和 GetApplicationUser 是我的示例方法。

    【讨论】:

    • 问题是,两种身份验证方案管理网站的两个不同部分,我不能混合使用它们。不过,我确实计划将它们分成两个不同的应用程序。完成后 - 我可以使用 Forms AuthCookie,但仍然使用域帐户进行验证。
    猜你喜欢
    • 2020-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多