c# - 通过 NTLM 模拟用户

标签 c# asp.net impersonation ntlm

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

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

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

有可能吗?谢谢。

最佳答案

假设您有启用了表单例份验证的 ASP.NET 应用程序,其中包含登录表单 login.aspx,并且您的用户存储在数据库中。现在您希望同时支持 Forms 和 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 是我的方法,仅供示例使用。

关于c# - 通过 NTLM 模拟用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1236452/

相关文章:

c# - 将 C# Console.Write* 与 AWS Lambda 结合使用

c# - Roslyn Analyzer - 查找接口(interface)方法调用的当前具体实现

asp.net - 如何打开 aspx 页面作为模态弹出窗口

c# - 在 ASP.NET 回发期间如何安全地保留密码字段的值?

c# - 使用 asp.net 表单例份验证的用户模拟

java - 来自 Java(Jetty 下的 Spring MVC)应用程序的 ADFS 身份验证和模拟

c# - ClickOnce 部署需要 Framework 4.5 安装 4.5.2 时需要

c# - 如何将 UserControl 中的嵌套控件完全暴露给外部?

c# - 在没有当前 HttpContext 可用的情况下访问 HttpApplicationState

iis-7 - IIS7 Windows 2008 服务器上的经典 ASP 模拟问题