ASP.NET MVC 3 中未经身份验证用户的缓存主页

发布于 2025-01-02 22:36:18 字数 447 浏览 3 评论 0原文

我的网站有一个主页,我想为匿名用户缓存该页面,并将其设置为经过身份验证的用户的“私有”(以便他们将其保存在自己的计算机上,而不是其他地方)。

因此,如果用户是匿名的,我想将页面保存在服务器缓存中,并使用 Cache-control:public, max-age=60Vary:Cookie 将页面保存在浏览器缓存中,因此如果浏览器通过身份验证并发送 cookie,浏览器将不会重用以前存储的页面。

如果用户已通过身份验证,那么我不希望将页面存储在服务器中,但我会使用 Cache-control:private, max-age=60 在客户浏览器上存储页面。

我一直在尝试使用 OutputCacheAttribute 和 Response.Cache 的几种组合,但我无法正确使用。

最好的方法是什么?

问候。

My website have a home page, and I would like to cache that page for anonymous users, and set it as "private" for authenticated users (so they save it on their computers, nowhere else).

So, if the user is anonymous I want to save the page in the server cache, and also in the browser cache using Cache-control:public, max-age=60 and Vary:Cookie, so if the browser gets authenticated and send a cookie, the browser won't reuse the former stored page.

If the user is authenticated, then I do not want the page be stored in the server, but I do on the customer browser using Cache-control:private, max-age=60.

I have been trying with several combinations with OutputCacheAttribute and Response.Cache but I cannot get it right.

What would be the best way of doing it?

Regards.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

淡淡绿茶香 2025-01-09 22:36:18

您可以尝试实施 VaryByCustom 规则来区分匿名用户和经过身份验证的用户。这是 一个示例应该会让您走上正轨。

You may try implementing a VaryByCustom rule that will distinguish between anonymous and authenticated users. Here's an example that should put you on the right track.

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