简体   繁体   English

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

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

I'm creating a website in ASP.NET (Framework 4.0). 我正在ASP.NET(Framework 4.0)中创建一个网站。 I have maintained session in this website. 我在该网站上保持了会话。 I have written code for logout where FormsAuthentication , session value is Abandon . 我已经编写了注销代码,其中FormsAuthentication的会话值为Abandon。 My Code as follow for logout button click . 我的代码如下,用于注销按钮单击。

 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);


    }

How to disable / block /prevent to previous page when logged out & without javascript. 注销时如何禁用/阻止/阻止到上一页/不使用JavaScript。 Because when user gets logout ,he redirect to default page, but users click browser back button he redirects to previous page (ie users home page ) instead of login page. 因为当用户注销时,他将重定向到默认页面,但是用户单击浏览器后退按钮,他将重定向到上一页(即用户的主页),而不是登录页面。

I think you have to use javascript . 我认为您必须使用javascript。 If you are using master page , then write this code in head section. 如果您使用的是母版页,则在头部分中编写此代码。

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

And in Master page (Page_Load) mode write this code. 并在母版页(Page_Load)模式下编写此代码。

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

You can use this , but i think it will work on some browser. 您可以使用它,但我认为它将在某些浏览器上运行。 Use on this Master (page_load). 在此Master(page_load)上使用。

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

Follow this Link for more details. 请点击此链接以获取更多详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM