ASP.Net:返回时使页面过期
基本上,当用户在浏览器中单击“后退”(或使用按键控制)时,我正在构建的该网站上的所有页面都无法访问,并且如果尝试返回历史记录,该页面应该过期。
我放入 Global.asax::Application_BeginRequest
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1))
Response.Cache.SetValidUntilExpires(False)
Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches)
Response.Cache.SetNoStore()
这将清除缓存并禁止在用户注销时返回到任何页面,但在用户登录时不会执行此操作。
我看到人们建议使用的帖子javascript 方法,通过
History.Forward(1)
在页面上调用。但我不想这样做,因为它需要启用 JavaScript 才能工作(用户可以禁用)。
感谢任何建议。
Basically all pages on this site I am building cannot be accessed when the user clicks on "Back" (or with key control) in the browser, and the page should expire if one is trying to navigate back in history.
I put into Global.asax::Application_BeginRequest
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1))
Response.Cache.SetValidUntilExpires(False)
Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches)
Response.Cache.SetNoStore()
This would clear out the cache and disallow going back to any pages when the user is logged out, but doesn't do the job while the user is logged in.
I saw posts where people suggested using a javascript approach, by calling
History.Forward(1)
on the page. But I wouldn't like to do this, as it will require javascript enabled to work (which user can disable).
Appreciate any suggestions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
始终如一地执行此操作的唯一方法是使用
https
。如果不是,您将无法强制浏览器不使用缓存页面。您提到了一些黑客行为,但它们并不能完全证明。如果它确实很重要,请使用https
因为每个请求都会强制重新加载。The only way you can consistently do this is if you are using
https
. If not you have no way to enforce the browser to not use a cached page. There are the hacks you mentioned about but they are not full proof. If it is really important, usehttps
because each request will force a reload.经过大量时间的调查和试验,我实施了一种解决方法,并决定分享它,以防其他人发现它有用。此(最接近解决方案,但仅限 Firefox 上的例外)解决方法将导致:
解决方法:
LinkButton
中的PostBackURL
设置为链接 href URL。这是必要的,因为只有使用 POST 以及我们设置的标头,浏览器才会使最初发布的内容过期历史页面并要求重新发送。该方案并不禁止用户导航回历史页面,而是确认是否再次发布数据。正如原始帖子中提到的,
History.Forward(1)
工作得不太好,而且我还认识到自动重新发布到页面而没有任何警告的可能性,本质上会导致提交多个请求到服务器。主要思想是 POST 到每个页面而不是 GET,因此网站中访问的每个页面在返回时都会产生“页面过期”页面。现在用户可以随时刷新以重新发布数据。
After much time spent on investigations and trials, I have implemented a workaround and decided to share it in case someone else might find it useful. This (closest to solution but only with the exception on Firefox) workaround will cause:
Workaround:
PostBackURL
in theLinkButton
to the link href URL.This is necessary, because only with POST, along with the header we set, that the browser would expire the originally posted page in history and ask for a resend.Server.Transfer
, asResponse.Redirect
will use a GET to another page.This solution does not prohibit the user to navigate back to a page in history, but whether confirm whether to post data once again. The
History.Forward(1)
doesn't work quite well, as mentioned in the original post, and I also recognize the possibility of automatically reposting to a page without any warnings, essentially resulting with multiple requests submitted to the server.The main idea is to POST to every page instead of GET, and so every page visited in the site would yield to the Page Expired page when navigating back. Now the user can always refresh to repost the data.