如何避免历史回退到登录页面

我搞出来的网站,在浏览器中按回退,不小心就退到登录页面,是可忍,孰不可忍。

怎么搞定这个问题?

试来试去,什么禁止缓存,meta里面no-cache啦,什么后台代码里

Response.Buffer=true;
Response.ExpiresAbsolute=DateTime.Now.AddSeconds(-1);
Response.Expires=0;
Response.CacheControl="no-cache";

啦,言之凿凿,告诉你,朕试过了,都没有什么卵用。

想想也是哈,这些语句,都是在告诉浏览器:嘿,兄弟,你可别缓存哟,这个一定要马上过期哟。我靠,将希望寄托在浏览器身上,浏览器如果不听你的呢?想想都不靠谱。

真正有用的,还是我大脚本。
我用ASP.NET MVC为例

控制器:

[HttpPost]
[AuthorizeIgnore]
public ActionResult Login()
{
    if(登录成功)
    {
        ViewBag.Redirect = tourl;
    }
    return View();
}

视图View:

<!DOCTYPE html>
@if (ViewBag.Redirect != null)
{//登录成功后返回
    <html lang="en">
    <head>
        <meta charset="utf-8" />
        <title>用户登录</title>
        <script src='@Url.StaticFile("media/js/jquery-1.10.1.min.js")' type="text/javascript"></script>
        <script src='@Url.StaticFile("~/Content/Scripts/wait.js")' type="text/javascript"></script>
    </head>
    <body>
        <script type="text/javascript">
            waitIcon.wait("正在登录。。。");
            $(function () {
                var tourl = "登录后页面地址";
                window.location.replace(tourl);//登录成功后,先回到这里,然后再从这里跳转。注意跳转用的是window.location.replace
            });
        </script>
    </body>
</html>
}
else
{//首次输出登录页
<html lang="en">
<head>
</head>
<body>

<!-- 这里是登录页面 -->
。。。。。省略一千字

</body>
</html>

<script type="text/javascript">

    jQuery(document).ready(function () {
        window.history.replaceState(null, "网站标题", '网站的默认页地址,不是这个登录页面地址');//登录页一加载,马上用默认页地址置换了登录页地址。放心,不会引起跳转
    });

</script>
}

posted on 2016-08-26 21:23  左直拳  阅读(648)  评论(0)    收藏  举报

导航