2009-08-06 39 views
8

我有一个内部应用程序,它有两个级别的安全性。用于面向客户端的应用程序的FormsAuthentication和用于管理界面的NTLM集成身份验证。通过NTLM模仿用户

我可以通过使用FormsAuthentication类的方法创建合适的.ASPXAUTH cookie来轻松地模拟客户端。但是,到目前为止,为NTLM生成HTTP身份验证标头已经超出了我的范围

当我发现这篇文章(http://msdn.microsoft.com/en-us/library/ms998358.aspx#paght000025_usingimpersonation)时,我对自己的希望很高兴,但后来我意识到它只创建一个上下文来在请求的持续时间内运行代码。我想切换整个会话,让服务器认为我在使用其他域登录。我对我的帐户拥有管理权限,因此不是为了盗用或窃取域密码。

它甚至有可能?谢谢。

回答

7

假设您已经启用了Forms身份验证ASP.NET应用程序,其登录表单login.aspx并且您的用户存储在数据库中。现在您想要支持Forms和Windows身份验证。这就是我所做的:

对于窗体auth我使用SQL DB,让说,用户表。我添加到名为WindowsUserName此表的新列中,我将在形式的计算机\用户

在login.aspx的形式我添加了一个方法,这将发送会显示登录窗口的响应保存Windows用户名:

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 
} 

GetUserIdForWindowsU ser和GetApplicationUser是我的方法,仅用于示例。

+0

事情是,两个身份验证方案管理网站的两个不同部分,我不能混用它们。我打算将它们分成两个不同的应用程序。完成后 - 我可以使用Forms AuthCookie,但仍然使用域帐户进行验证。 – 2009-08-18 19:55:27