Dar*_*bio 12 asp.net-mvc http windows-authentication logout http-status-code-401
我有一个用MVC 4编写的Intranet项目,它使用Windows身份验证来授权和验证用户.
我需要添加"以其他用户身份登录"功能.
经过一些搜索,我发现这个解决方案建议返回401,并创建了以下Action(使用表单调用):
//
// POST: /Home/LogOut
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult LogOut()
{
return new HttpUnauthorizedResult();
}
Run Code Online (Sandbox Code Playgroud)
调用Action,浏览器会弹出用户名和密码窗口,但是当结果重定向回Action时,总会返回401.
一旦用户使用新凭据登录,如何将用户重定向回上一个操作?
有没有办法使服务器端的凭据无效,而不是只返回401?
Len*_*rri 24
人们反向工程\反编译来自Sharepoint的一些恰好具有此功能的代码.
我在一个ASP.NET MVC 5应用程序中测试它,它按预期工作.
该代码基于反编译具有"以不同用户身份登录"功能的Microsoft.TeamFoundation.WebAccess.
public ActionResult LogOut()
{
HttpCookie cookie = Request.Cookies["TSWA-Last-User"];
if(User.Identity.IsAuthenticated == false || cookie == null || StringComparer.OrdinalIgnoreCase.Equals(User.Identity.Name, cookie.Value))
{
string name = string.Empty;
if(Request.IsAuthenticated)
{
name = User.Identity.Name;
}
cookie = new HttpCookie("TSWA-Last-User", name);
Response.Cookies.Set(cookie);
Response.AppendHeader("Connection", "close");
Response.StatusCode = 401; // Unauthorized;
Response.Clear();
//should probably do a redirect here to the unauthorized/failed login page
//if you know how to do this, please tap it on the comments below
Response.Write("Unauthorized. Reload the page to try again...");
Response.End();
return RedirectToAction("Index");
}
cookie = new HttpCookie("TSWA-Last-User", string.Empty)
{
Expires = DateTime.Now.AddYears(-5)
};
Response.Cookies.Set(cookie);
return RedirectToAction("Index");
}
Run Code Online (Sandbox Code Playgroud)
资源:
强制在asp.net中使用Windows身份验证时以其他用户身份登录
| 归档时间: |
|
| 查看次数: |
12116 次 |
| 最近记录: |