使用浏览器后退按钮注销问题

我使用ASP.Net MVC 4创建了登录/注销function。我使用自己创建的表单来针对Active Directory对用户进行身份validation。 它的function很好。

安全问题仍然存在很大问题。 用户单击注销链接后,他/她已成功注销并重新定向到登录表单。 控制器中的代码如下所示。

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来禁用/关闭浏览器。)

更新:代码片段

  [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页面标题如下所示。

        . . . 

在global.asax页面中添加以下代码,并从logout()函数中删除前3行。

 protected void Application_BeginRequest() { Response.Cache.SetCacheability(HttpCacheability.NoCache); Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1)); Response.Cache.SetNoStore(); } 

我只使用了DateTime.Now的SetExpires,它将本地服务器时间与cookie匹配。 使用DateTime.UtcNow.Addminutes(-1)可能是真正的罪魁祸首。

此外,如果您正在使用表单身份validation,我看不到您的呼叫

 FormsAuthentication.SignOut(); 

将以下属性添加到任何返回控制器中安全页面的ActionResult方法应该有效:

 public class MyControllerForAuthorizedStuff { [OutputCache(NoStore = true, Duration = 0, Location = OutputCacheLocation.None)] public ActionResult Index() { return View(); } }