ASP.Net:返回时使页面过期

发布于 2024-08-25 11:43:24 字数 626 浏览 3 评论 0原文

基本上,当用户在浏览器中单击“后退”(或使用按键控制)时,我正在构建的该网站上的所有页面都无法访问,并且如果尝试返回历史记录,该页面应该过期。

我放入 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

人生百味 2024-09-01 11:43:24

始终如一地执行此操作的唯一方法是使用 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, use https because each request will force a reload.

温柔少女心 2024-09-01 11:43:24

经过大量时间的调查和试验,我实施了一种解决方法,并决定分享它,以防其他人发现它有用。此(最接近解决方案,但仅限 Firefox 上的例外)解决方法将导致:

  1. IE6 - 8: 出现“页面已过期”
  2. Chrome: 出现“确认表单重新提交”
  3. Firefox:会出现一个对话框,要求重新发送帖子数据

解决方法:

  1. 使用上面发布的原始问题中列出的 Response.Cache 设置
  2. ,将所有超链接更改为其他页面站点内的 LinkBut​​ton。
  3. LinkBut​​ton 中的 PostBackURL 设置为链接 href URL。这是必要的,因为只有使用 POST 以及我们设置的标头,浏览器才会使最初发布的内容过期历史页面并要求重新发送。
  4. 不要在后面的代码中使用 Response.Redirect,而是使用 Server.Transfer,因为 Response.Redirect 将使用 GET 来访问另一个页面。

该方案并不禁止用户导航回历史页面,而是确认是否再次发布数据。正如原始帖子中提到的,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:

  1. IE6 - 8: "Page expired" comes up
  2. Chrome: "Confirm Form Resubmission" comes up
  3. Firefox: A dialog box would come up asking to resend the post data

Workaround:

  1. With the Response.Cache settings listed in the original problem posted above
  2. Change all hyperlinks to a different page within the site to LinkButton.
  3. Set the PostBackURL in the LinkButton 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.
  4. Instead of using Response.Redirect in the code behind, use Server.Transfer, as Response.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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文