如何在ASP.NET中注销后阻止/阻止用户返回上一页

时间:2016-02-02 17:56:21

标签: javascript c# asp.net session

我在ASP.NET(Framework 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。因为当用户注销时,他会重定向到默认页面,但是用户点击浏览器后退按钮,他会重定向到上一页(即用户主页),而不是登录页面。

2 个答案:

答案 0 :(得分:3)

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

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

在Master页面(Page_Load)模式下编写此代码。

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

答案 1 :(得分:1)

你可以使用它,但我认为它适用于某些浏览器。在这个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了解详情。