我使用 ASP.Net MVC 4 创建了登录/注销功能。我使用自己创建的表单根据 Active Directory 对用户进行身份验证。它的功能运行良好。
安全方面仍然存在一个大问题。用户单击注销链接后,他/她成功注销并再次重定向到登录表单。控制器中的代码如下所示。
public ActionResult Logout()
{
// Tried to include below 3 lines in _Layout.cshtml as well. But not identifying.
Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1));
Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);
Response.Cache.SetNoStore();
Session.Abandon();
return RedirectToAction("Login");
}
但是,一旦单击浏览器后退按钮,用户就可以返回到其他页面并浏览页面。
我尝试了几种解决方案、不同的方法,但都没有成功。看起来 MVC 方法与 ASP.NET 表单有很大不同。感谢您对此的帮助。
(我正在寻找使用 C#/ MVC 方式解决这个问题。不使用 JavaScript 在注销时禁用/关闭浏览器。)
UPDATE:代码片段
[HttpPost]
public ActionResult Login(LoginModel authUser)
{
// Call Helper to get LDAP info. Will return username with groups or null
UserModel userProfile = LdapLoginHelper.AuthenticateUser(authUser);
if (userProfile != null)
{
Session["UserName"] = userProfile.UserName;
Session["LdapGroups"] = userProfile.LdapGroups;
if (userProfile.LdapGroups.Contains("Administrators"))
{
// To be implemented
}
else
{
// To be implemented
}
// Successful login. Redirect to main page
return RedirectToAction("Home", "Home");
}
else
{
// Invalid Login. Redirect to Login page
return RedirectToAction("Login");
}
}
public ActionResult Logout()
{
// Not worked
Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1));
Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);
Response.Cache.SetNoStore();
Session.Abandon();
/// Tried this too. Not worked.
/// Session.Clear();
/// FormsAuthentication.SignOut();
//// Tried this also. Not worked.
//// WebSecurity.Logout();
return RedirectToAction("Login");
}
除了这个常见的 _Layout.cshtml 页面标题如下所示。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Expires" content="-1">
<meta http-equiv="CACHE-CONTROL" content="NO-CACHE">
.
.
.