简体   繁体   English

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

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

Our problem is we are able to clear session on logout.我们的问题是我们能够在注销时清除 session。

But if a user clicks the back button then he/she can go through all previous screens.但是,如果用户单击后退按钮,则他/她可以通过所有先前的屏幕 go。

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.但我们的要求是不允许用户通过之前的浏览页面访问 go。

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... 这里...

write this code in master page in page load event在页面加载事件的母版页中编写此代码

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

and write this code in Login page in head section并在头部的登录页面中编写此代码

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

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.正如其他人所说,正确的方法是使缓存无效,以及服务器端验证 session 如果他们尝试重新发送数据不再有效。 Also, Response.Redirect works better than a postback, since that causes a get rather than a post.此外,Response.Redirect 比回发更好,因为这会导致获取而不是发布。

For ASP.NET pages you can use Response.CacheControl to control how a page is stored in a users cache.对于 ASP.NET 页面,您可以使用Response.CacheControl来控制页面在用户缓存中的存储方式。 Other web development languages will utilize something similar.其他 web 开发语言将使用类似的东西。

You could go Outlook Web Access style, and simply have JavaScript close the current window/tab.您可以 go Outlook Web 访问样式,只需关闭当前窗口 Z686155AF75A60A0F6EZD8。

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.编辑:其他人提到了 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".您实际上可以将“注销”链接 go 链接到执行重定向的页面,并始终重定向到第二个“登录页面”。 If the user clicks "Back", they will land on the redirect again and put them back where they started.如果用户点击“返回”,他们将再次登陆重定向并将他们放回他们开始的地方。

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.没有办法阻止浏览器历史记录,因此一起使用几种方法并且不要计划用户“不倒退”以确保您的应用程序安全性非常重要。

This is the solution I found on 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();
    }

暂无
暂无

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

相关问题 注销后,如何在ASP.NET MVC3中单击浏览器的后退按钮时将用户重定向到登录页面 - After logout,how to redirect user to login page when he/she will click back button of browser in ASP.NET MVC3 登录C#asp.net后禁用Web浏览器后退按钮 - Disable web browser back button after login in c# asp.net 用户单击浏览器停止按钮时如何停止服务器端处理(ASP.net C#) - How to stop server side processing when user clicks browser stop button (ASP.net C#) 如何在asp.net c#中确定用户注销? - How to determine a user logout in asp.net c#? asp.net注销中浏览器问题的后退按钮 - back button of browser issue in asp.net logout 当用户再次使用C#在asp.net中单击按钮时,如何将图像按钮更改回前一个图像 - How to change the image button back to the previous image when user click the button again in asp.net using C# 在注销后如何通过在asp.net中的浏览器中键入后退按钮或URL拒绝访问? - How to deny access after logout by means of back button or url typed in browser in asp.net? 注销后C#ASP.NET MVC后退按钮问题 - C# ASP.NET MVC Back button issue after logout 网页URL带来已经注销的页面:asp.net c#(不是返回按钮,而是页面url) - webpage URL bringing the page which is already logout :asp.net c# (Not back button, but the page url) 当用户单击asp.net中的浏览器后退按钮时,如何快速重定向页面 - How to redirect the page quickly when the user clicks the browser back button in asp.net
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM