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.

vtortola
  • 34,709
  • 29
  • 161
  • 263

1 Answers1

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.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 1
    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 http://stackoverflow.com/questions/9210581/outputcache-is-sending-wrong-vary-header-when-the-call-hits-the-cache. Cheers. – vtortola Feb 09 '12 at 12:13
  • In your link is just using the server side caching, I am interested in HTTP caching on the client and downstream as well. – vtortola Feb 09 '12 at 14:16
  • You can specify proxy/client-level caching like so: context.Response.Cache.SetCacheability(HttpCacheability.Public); – Dusda Jun 12 '12 at 19:17