mvcdonutcaching icon indicating copy to clipboard operation
mvcdonutcaching copied to clipboard

Handling Cookies

Open dazbradbury opened this issue 12 years ago • 9 comments

Firstly - MVC Donut Cache is excellent, thanks for releasing.

I'm having an issue understanding how cookies are meant to be handled with respect to the output cache.

Let's say I set a cookie in the method of a cached controller:

[DonutOutputCache(Duration = 43200, VaryByParam = "*")]
public ActionResult Index()
{
...
HttpCookie cookie = new HttpCookie("cookie");
cookie.Value = "cookieValue";
cookie.Expires = DateTime.Now.AddMonths(1);
cookie.Shareable = true;
Response.Cookies.Add(cookie);
...
Return View();
}

On the first load of this page, the cookie will be set. On all subsequent loads, it is not set.

Looking around a bit my understanding was setting cookie.Shareable to true would mean the cookie would be sent with all responses - is this not the case? Using DonutOutputCache, this setting appears to make no difference.

Do you have a different way to handle cookies in the response which works for all requests?

Thanks, Daz.

dazbradbury avatar Dec 06 '13 20:12 dazbradbury

The cookie is not set for subsequent requests, this is the expected behaviour as your result will be cached for 43200 seconds upon first load. The body of your Action code won't be exectuted until expiration.

You should not set a cookie in a donut cached action (or anything that alters the response in headers terms).

moonpyk avatar Dec 06 '13 20:12 moonpyk

Thanks for the reponse - I can see that is the case, I was just hoping that: cookie.Shareable = true; Could be used to ascertain whether a cookie should be cached or not in all requests that follow.

There is another workaround, which is to respond with a redirect to the same controller (adding a paramater of cookie set = true), redirects are ignored by the Donut Output Cache, and hence, the cookie will be set and the cached page loaded from the subsequent request.

Alternatively, I guess one could use javascript to set any cookies...

Ideally however, there would be a way to cache and replay the cookie response as part of the cache. I appreciate this may be out of scope for the project!

dazbradbury avatar Dec 06 '13 20:12 dazbradbury

I was completely unaware of this property, Mvc's native OutputCache respects this property, so it's clear that MvcDonutCaching should too.

moonpyk avatar Dec 06 '13 21:12 moonpyk

I'm very unfamiliar with your codebase - is it going to be a substantial change? Or something that can be offloaded to the native OutputCache?

If you point me in the right direction I'll happily see if I can implement.

dazbradbury avatar Dec 06 '13 21:12 dazbradbury

@dazbradbury I also have this problem... I need to update a cookie, but the action is cached, so the cookie isn't changed. How did you solve this?

migig avatar May 23 '15 18:05 migig

@migig I believe that is a different question all together, if you need to update the cookie you'll need your controller code (or a higher priority filter) to be executed. Lots of options here, action filter, controller with view that loads a cached controller / view action, etc.. This however is not related to this specific bug / feature.

dazbradbury avatar May 26 '15 11:05 dazbradbury

@dazbradbury Library still doesn't support cookie.Shareable so you should'nt close this issue yet.

migig avatar Jun 02 '15 15:06 migig

Are cookies supported now?

YodasMyDad avatar Jan 06 '16 15:01 YodasMyDad

no @leen3o I'm sorry the ticket has been wrongly closed

moonpyk avatar Jan 07 '16 12:01 moonpyk