如何在mvc3.net中注销后禁用浏览器后退按钮 [英] How Disable Browser Back Button only after Logout in mvc3.net

查看:90
本文介绍了如何在mvc3.net中注销后禁用浏览器后退按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用FormsAuthentication for userlogin。
我在用户成功注销后遇到问题后退按钮是浏览器允许用户查看页面。
我尝试使用javascript

I am using FormsAuthentication for userlogin. I am having a problem after user logs out successfuly the back button is browser allows user to view pages. I tried using javascript

 <script type = "text/javascript" >
        function preventBack() { window.history.forward(1); }
        setTimeout("preventBack()", 0);
        window.onunload = function () { null };
</script>

但是后退按钮是完全禁用的。
它工作了,我不想在用户登录时禁用后退按钮功能。我希望我的LOGGED IN用户正常使用浏览器后退按钮。但是一旦他选择退出,他就不允许通过按Back来查看任何内容。
我也尝试过使用

But back button is completly disabled. It worked bt,I dont want to disable back button functionality when user is logged in. i want my LOGGED IN user to use browser back button as normal. But once he choosed to log out, he is not allow to see any of contents by pressing Back. I also tried using

Session.Abandon();
 Response.Cache.SetCacheability(HttpCacheability.NoCache);
 Response.Cache.SetExpires(DateTime.Now);

但这也不起作用。我能解决这个问题吗?

But this is also not working.how do I fix this?

推荐答案

您可以在用户退出时清除浏览器历史记录:

You could clear the browser history when the user logs out:

var url = window.location.href;
window.history.go(-window.history.length);
window.location.href = url;

然而,这不会特别强大 - 它依赖于javascript,它不适用于多个标签和可能只会惹恼用户。 IMO最好的办法是设置适当的缓存标题,以便浏览器不会通过适当应用的NoCacheAttribute缓存任何登录页面:

However this would not be particularly robust - it relies on javascript, it would not work across multiple tabs and may only serve to annoy the user. IMO the best bet is to set appropriate caching headers such that the browser will not cache any of your 'logged in' pages via a NoCacheAttribute applied appropriately:

public class NoCacheAttribute : ActionFilterAttribute
{  
  public override void OnResultExecuting(ResultExecutingContext filterContext)
  {
      filterContext.HttpContext.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
      filterContext.HttpContext.Response.Cache.SetValidUntilExpires(false);
      filterContext.HttpContext.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
      filterContext.HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
      filterContext.HttpContext.Response.Cache.SetNoStore();

      base.OnResultExecuting(filterContext);
  }
}

这篇关于如何在mvc3.net中注销后禁用浏览器后退按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆