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

How to block / prevent user to go back to previous page after logout in ASP.NET

本文关键字:注销 返回上一页 ASP NET 用户 何阻止      更新时间:2023-09-26

我正在用 ASP.NET(框架4.0(创建一个网站。我一直在这个网站上保持会话。我已经编写了用于注销的代码,其中表单身份验证,会话值为放弃。 我的代码如下,用于注销按钮单击。

 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。如果您使用的是母版页,请在 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();

您可以使用这个,但我认为它可以在某些浏览器上运行。在此母版 (page_load( 上使用。

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

点击此链接了解更多详情。