Vas*_*dov 8 c# asp.net impersonation ntlm
我有一个内部应用程序,它有两个安全级别.FormsAuthentication用于面向客户端的应用程序和NTLM集成身份验证用于管理界面.
我可以通过使用FormsAuthentication类的方法创建正确的.ASPXAUTH cookie来轻松模拟客户端.但是,到目前为止,为NTLM生成HTTP身份验证标头已超出我的范围.
当我发现这篇文章(http://msdn.microsoft.com/en-us/library/ms998358.aspx#paght000025_usingimpersonation)时,我有了希望,但后来我意识到它只创建一个上下文来运行代码一段时间请求.我想切换整个会话,让服务器认为我正在使用另一个域登录.我对我的帐户拥有管理权限,因此不是为了搞砸或窃取域密码.
它甚至可能吗?谢谢.
假设您启用了表单身份验证ASP.NET应用程序,登录表单为login.aspx,您的用户存储在数据库中.现在,您希望同时支持Forms和Windows身份验证.我就是做这个的:
对于表单身份验证,我使用SQL DB,比如说Users表.我在此表中添加了一个名为WindowsUserName的新列,其中我将以表格COMPUTER\User保存Windows用户的名称
在login.aspx表单中,我添加了一个方法,该方法将发送一个显示登录窗口的响应:
private void ActivateWindowsLogin()
{
Response.StatusCode = 401;
Response.StatusDescription = "Unauthorized";
Response.End();
}
Run Code Online (Sandbox Code Playgroud)
在某个地方我有一个类似的链接 <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
}
Run Code Online (Sandbox Code Playgroud)
GetUserIdForWindowsUser和GetApplicationUser是我的方法,仅用于示例.
| 归档时间: |
|
| 查看次数: |
9162 次 |
| 最近记录: |