2009-08-06 78 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