通过NTLM模拟用户

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

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

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

它甚至可能吗? 谢谢。

假设您启用了表单身份validationASP.NET应用程序,登录表单为login.aspx,您的用户存储在数据库中。 现在,您希望同时支持Forms和Windows身份validation。 我就是做这个的:

对于表单身份validation我使用SQL DB,比如说Users表。 我在此表中添加了一个名为WindowsUserName的新列,其中我将以表格COMPUTER \ User保存Windows用户的名称

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

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

某处我有一个像Admin这样的链接

在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是我的方法,仅用于示例。