注销后如何防止浏览器后退按钮?

时间:2014-02-21 09:37:57

标签: c# jquery asp.net

用户退出后如何停止后退按钮? (LOGOUT按钮在母版页中) 使用webforms

我有几页,最后一页是最后一页,退出后我点击后退按钮显示上一页。我如何避免这一点。请帮助我解释代码

代码只需在LOGOUT之后触发。用户必须能够返回,如果他在进入时必须进行任何更改,请查看上一页。

8 个答案:

答案 0 :(得分:20)

您应该设置正确的HTML标头。根据{{​​3}},这些是适用于所有浏览器的:

Cache-Control: no-cache, no-store, must-revalidate
Pragma: no-cache
Expires: 0

您可以这样设置:

HttpContext.Current.Response.AddHeader("Cache-Control", "no-cache, no-store, must-revalidate");
HttpContext.Current.Response.AddHeader("Pragma", "no-cache");
HttpContext.Current.Response.AddHeader("Expires", "0");

答案 1 :(得分:0)

function preventBack(){window.history.forward();}
  setTimeout("preventBack()", 0);
  window.onunload=function(){null};

答案 2 :(得分:0)

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

它可能适合你

答案 3 :(得分:0)

此方法仅适用于MVC网站

这是我使用的方法

将以下过滤器添加到FilterConfig(假设您使用的是MVC)

 filters.Add(new System.Web.Mvc.AuthorizeAttribute());

现在装饰用户可以使用的操作方法,而无需使用

登录
 [AllowAnonymous]

请务必装饰您登录和注册方法。

现在,当用户点击后(假设页面刷新),他们将被要求再次登录。 我不允许我的页面被缓存强制刷新后退按钮。

希望这对你有所帮助

答案 4 :(得分:0)

即使在浏览器中按下后退按钮(在crome中测试)也能完美地工作

我使用了会话变量,并且还禁用了缓存

第1页(default.aspx)登录表单,我收集会话变量ID

Session["id"] = TextBox1.Text;// just the username</pre>

     page 2(Default2.aspx) in page load 
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetExpires(DateTime.Now.AddDays(-1));
            Response.Cache.SetNoStore();

                if (Session["id"] == null)
                {    

                    Response.Redirect("Default.aspx");// redirects to page1(default.aspx)

                }
                else
                {
                    Label1.Text = (string)Session["id"];//just display user name in label

                }

        }

在第2页退出按钮

     protected void Button1_Click(object sender, EventArgs e)
        {
            Session["id"] = null;

            Response.Redirect("/WebSite5/Default.aspx");//redirects to page1(default.aspx)
        } 

答案 5 :(得分:0)

尝试了很多解决方案,但只有下面的解决方案得以解决。在注销页面中添加了以下java脚本。所有其他解决方案在IE以外的浏览器中都能正常工作

window.onunload = function() {
                window.location.href = "<%=request.getContextPath()%>/logout.jsp";
            };

答案 6 :(得分:0)

我知道这是一个旧线程,但有人可能会帮助这个。您可以使用javascript事件windows.hash更改来阻止后退按钮。

    <script>
        var history_api = typeof history.pushState !== 'undefined';
        if (location.hash == '#no-back') {
            if (history_api) history.pushState(null, '', '#stay')
            else location.hash = '#stay'
 
            window.onhashchange = function () {
                if (location.hash == '#no-back') {
                    alert('Action cannot be done!');
                    if (history_api) history.pushState(null, '', '#stay')
                    else location.hash = '#stay'
                }
            }
        }
</script>

您可以访问here获取完整教程

答案 7 :(得分:0)

试试这个我发现这种防止反击的最佳方法。在codeproject中得到了这个解决方案。

使用这些常用方法注销时清理会话值。

Session.Abandon();
Session.Clear();
Session.RemoveAll();
System.Web.Security.FormsAuthentication.SignOut();
Response.Redirect("frmLogin.aspx", false);

在ASPX页面上,我使用带有JSON的Jquery并使用LogoutCheck()WebMethod检查Session值。

<script type="text/javascript">
    $(document).ready(function () {
        CheckingSeassion();
    });
    function CheckingSeassion() {
        $.ajax({
            type: "POST",
            url: "frmLogout.aspx/LogoutCheck",
            data: "{}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (response) {
                if (response.d == 0) {
                    window.location = '<%= BaseURL %>' + "frmLogin.aspx";
                }
            },
            failure: function (msg) {
                alert(msg);
            }
        });
    }

LogoutCheck()WebMethod在客户端加载时检查应用服务器的会话值。

我在frmLogout.aspx页面上创建了这个方法,如下所示:

[WebMethod]
public static int LogoutCheck()
{
   if (HttpContext.Current.Session["user"] == null)
   {
      return 0;
   }
   return 1;
}

现在,当用户注销页面时,它会重定向到注销页面并清除并放弃会话值。现在,当用户单击浏览器的后退按钮时,客户端仅加载,在此期间,CheckSession()WebMethod在JQuery中触发,并检查会话值LogoutCheck()WebMethod。当会话为null时,该方法返回零,页面在登录页面中再次重定向。因此,我不必清除缓存或清除用户浏览器的任何历史记录。

相关问题