【问题标题】:How to block / prevent user to go back to previous page after logout in ASP.NET如何阻止/阻止用户在 ASP.NET 中注销后返回上一页
【发布时间】:2016-05-11 16:14:39
【问题描述】:

我正在用 ASP.NET(框架 4.0)创建一个网站。我在这个网站上保持了会话。我已经编写了注销代码,其中 FormsAuthentication ,会话值为 Abandon 。我的代码如下注销按钮点击。

 protected void LinkButton1_Click(object sender, EventArgs e)
    {
        if (Request.Cookies["ASP.NET_SessionId"] != null)
        {
            Response.Cookies["ASP.NET_SessionId"].Value = string.Empty;
            Response.Cookies["ASP.NET_SessionId"].Expires = DateTime.Now.AddMonths(-20);
        }
        FormsAuthentication.SignOut();
        Session.Abandon();
        Response.Redirect(FormsAuthentication.DefaultUrl);


    }

如何在注销且没有 javascript 的情况下禁用/阻止/防止到上一页。 因为当用户注销时,他会重定向到默认页面,但用户单击浏览器后退按钮,他会重定向到上一页(即用户主页)而不是登录页面。

【问题讨论】:

    标签: javascript c# asp.net session


    【解决方案1】:

    我认为你必须使用 javascript 。如果您使用的是母版页,请在 head 部分编写此代码。

    <script type="text/javascript">
            window.history.forward(-1);
        </script>
    

    并在母版页 (Page_Load) 模式下编写此代码。

    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
    Response.Cache.SetNoStore();
    

    【讨论】:

    • 是的,您的代码很有用。但是他们有没有其他方式来编写代码而不是 javascript ?因为用户可以禁用 javascript。
    【解决方案2】:

    您可以使用它,但我认为它适用于某些浏览器。在此 Master (page_load) 上使用。

    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.Cache.SetExpires(DateTime.Now.AddSeconds(360));
    Response.Cache.SetCacheability(HttpCacheability.Private)
    Response.Cache.SetSlidingExpiration(true);
    

    关注Link了解更多详情。

    【讨论】:

    • 您的第一个代码有效,但是当用户单击浏览器后退按钮时此代码无效。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-18
    • 2010-10-04
    • 2011-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多