After a little research on this problem, I came to understand and solve this problem.
The reason that the output cache does not play good results with cookies
Therefore, the reason the output cache will not cache the response with cookies is because the cookie may be user-specific (e.g. authentication, analytic tracking, etc.). If one or more cookies with the property HttpCookie.Shareable = false , then the output cache considers the answer incompatible.
Including cookies with cached response
That's where it gets complicated. The output cache caches the headers and contents of the responses together and does not provide any hooks to change them before sending them back to the user. However, I wrote the following custom output cache provider to provide the ability to change the cached response headers before they are sent back to the user (requires the Fasterflect nuget package):
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Caching; using System.Web; using System.Web.Caching; using Fasterflect; namespace CustomOutputCache {
You would connect it like this:
<system.web> <caching> <outputCache defaultProvider="HeaderModOutputCacheProvider"> <providers> <add name="HeaderModOutputCacheProvider" type="CustomOutputCache.HeaderModOutputCacheProvider"/> </providers> </outputCache> </caching> </system.web>
And you can use it to insert cookies:
HeaderModOutputCacheProvider.RequestServedFromCache += RequestServedFromCache; HeaderModOutputCacheProvider.RequestServedFromCache += (sender, e) => { e.AddCookies(new HttpCookieCollection { new HttpCookie("key", "value") }); };
Wiseguyyh
source share