Cache home page for non-authenticated users in ASP.NET MVC 3
Asked Answered
W

1

4

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.

Webster answered 7/2, 2012 at 17:33 Comment(0)
D
2

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.

Duroc answered 7/2, 2012 at 21:21 Comment(3)
Hi. That is what I tried first but it didn't work. Now I am trying to figure out why it is not working, take a look on #9211081. Cheers.Webster
In your link is just using the server side caching, I am interested in HTTP caching on the client and downstream as well.Webster
You can specify proxy/client-level caching like so: context.Response.Cache.SetCacheability(HttpCacheability.Public);Unlucky

© 2022 - 2024 — McMap. All rights reserved.