当用户在asp.net c#中注销时如何禁用浏览器中的后退按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/589285/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 09:23:28  来源:igfitidea点击:

How to disable the back button in browser when user logout in asp.net c#

c#asp.net

提问by

Our problem is we are able to clear session on logout.

我们的问题是我们能够在注销时清除会话。

But if a user clicks the back button then he/she can go through all previous screens.

但是,如果用户单击后退按钮,则他/她可以浏览所有以前的屏幕。

But the advantage is that on a single click on any of of such previously surf page bring user to login page back ,we had done that. But our requirement is we should no allow user to go through the previously surf page.

但优点是,在任何以前的冲浪页面上单击任何一个,都会将用户带回登录页面,我们已经做到了。但是我们的要求是我们不应该允许用户浏览之前的冲浪页面。

回答by Brandon

You could go Outlook Web Access style, and simply have JavaScript close the current window/tab.

您可以采用 Outlook Web Access 风格,只需让 JavaScript 关闭当前窗口/选项卡即可。

Also, you can make sure that your "logout" page is a postback. That will force the user on a Back button in most browsers to retry the postback, at which point you can detect that they are no longer logged in and can redirect them back to the login page.

此外,您可以确保您的“注销”页面是回发。这将强制用户在大多数浏览器中使用“后退”按钮重试回发,此时您可以检测到他们不再登录并可以将他们重定向回登录页面。

Edit:Someone else mentioned a Response.Redirect. You could actually make your "logout" link go to a page that does a redirect, and ALWAYS redirect to a second "landing page". If the user clicks "Back", they will land on the redirect again and put them back where they started.

编辑:其他人提到了 Response.Redirect。您实际上可以将“注销”链接转到执行重定向的页面,并始终重定向到第二个“登录页面”。如果用户点击“返回”,他们将再次登陆重定向并将他们放回他们开始的地方。

There's no way to prevent browser history so it's important to use a couple of methods together and don't plan on a user "not going backwards" to ensure your application security.

没有办法阻止浏览器历史记录,因此将几种方法结合使用很重要,不要计划用户“不退缩”以确保您的应用程序安全。

回答by RSolberg

You need to force the cache to expire for this to work. I'm looking for the code sample for you.

您需要强制缓存过期才能使其工作。我正在为您寻找代码示例。

EDIT
Found this for you, its already been addressed here on SO.

编辑
为你找到了这个,它已经在这里解决了。

Page.Response.Cache.SetCacheability(HttpCacheability.NoCache)

Here...

这里...

回答by ahsteele

For ASP.NET pages you can use Response.CacheControlto control how a page is stored in a users cache. Other web development languages will utilize something similar.

对于 ASP.NET 页面,您可以使用Response.CacheControl来控制页面在用户缓存中的存储方式。其他 Web 开发语言将使用类似的东西。

回答by Erik Funkenbusch

You can't "disable" the back button. There are numerous "tricks" that i've seen that can clear out the back history, but these are unreliable and they don't work from browser to browser, or even version of browser to version of browser.

您不能“禁用”后退按钮。我见过许多“技巧”可以清除过去的历史记录,但这些都是不可靠的,并且它们在浏览器之间,甚至浏览器版本之间都不起作用。

As others have said, the correct method is invalidate the cache, along with server side validation that the session is no longer valid if they try to resend data. Also, Response.Redirect works better than a postback, since that causes a get rather than a post.

正如其他人所说,正确的方法是使缓存无效,以及服务器端验证如果他们尝试重新发送数据会话不再有效。此外,Response.Redirect 比回发效果更好,因为这会导致获取而不是发布。

回答by Ritesh Shah

write this code in master pagein page load event

在页面加载事件的母版页中编写此代码

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

and write this code in Login pagein head section

并在头部部分的登录页面中编写此代码

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

回答by Alejandro

This is the solution I found on Coding Solutions

这是我在Coding Solutions 上找到的解决方案

in the master page

在母版页

    protected void Page_Load(object sender, EventArgs e)
    {
        Response.ClearHeaders();
        Response.AppendHeader("Cache-Control", "no-cache"); //HTTP 1.1
        Response.AppendHeader("Cache-Control", "private"); // HTTP 1.1
        Response.AppendHeader("Cache-Control", "no-store"); // HTTP 1.1
        Response.AppendHeader("Cache-Control", "must-revalidate"); // HTTP 1.1
        Response.AppendHeader("Cache-Control", "max-stale=0"); // HTTP 1.1
        Response.AppendHeader("Cache-Control", "post-check=0"); // HTTP 1.1
        Response.AppendHeader("Cache-Control", "pre-check=0"); // HTTP 1.1
        Response.AppendHeader("Pragma", "no-cache"); // HTTP 1.1
        Response.AppendHeader("Keep-Alive", "timeout=3, max=993"); // HTTP 1.1
        Response.AppendHeader("Expires", "Mon, 26 Jul 1997 05:00:00 GMT"); // HTTP 1.1
    }

Control LoginStatus

控制登录状态

    protected void LoginStatusUser_LoggedOut(object sender, EventArgs e)
    {
        Session.Abandon();
        FormsAuthentication.SignOut();
    }