madskristensen / WebEssentials.AspNetCore.OutputCaching

Other
52 stars 17 forks source link

Outputcache doesn't work when a cookie is set #23

Open arnvanhoutte opened 3 years ago

arnvanhoutte commented 3 years ago

I have a custom localization middleware where I set the current culture in a cookie:

                context.Response.Cookies.Append(
                    CookieRequestCultureProvider.DefaultCookieName,
                    CookieRequestCultureProvider.MakeCookieValue(
                        new RequestCulture(culture, culture)));

If I add this code then the outputcache stops working. This was also a problem in .NET Framework's OutputCache but the workaround was making the cookie shareable https://docs.microsoft.com/en-us/dotnet/api/system.web.httpcookie.shareable?redirectedfrom=MSDN&view=netframework-4.8#System_Web_HttpCookie_Shareable

However it doesn't seem like this works here.

EDIT:

I notice that this setup works but what the reason that cookies are blocked in the first place?

        services.AddOutputCaching(options => options.DoesResponseQualify = (context) =>
        {
            if (context.Response.StatusCode != StatusCodes.Status200OK) return false;
            //if (context.Response.Headers.ContainsKey(HeaderNames.SetCookie)) return false; ENABLING COOKIES

            return true;
        });