🔧 The Response Caching Middleware That Refused Every Cookie-Bearing Response
ASP.NET Core’s response caching middleware follows the HTTP caching spec closely, and one of the rules it enforces without exception is that a response setting a cookie isn’t eligible to be cached – a shared cache storing and replaying a response meant to set a unique session cookie would hand that same cookie to every subsequent caller, which the spec correctly treats as unsafe. The part that surprises most teams is that this check applies to any cookie set anywhere in the pipeline for that request, including one set by completely unrelated middleware that runs long before the endpoint whose caching behavior everyone’s actually trying to debug.
🔎 The Problem
app.UseResponseCaching();
app.MapGet("/catalog/featured", (HttpContext ctx) =>
{
// Some shared code path sets a tracking or session-affinity
// cookie on every response through this app, including this
// one - which has nothing to do with the data being returned.
ctx.Response.Cookies.Append("sid", Guid.NewGuid().ToString());
ctx.Response.GetTypedHeaders().CacheControl =
new CacheControlHeaderValue { Public = true, MaxAge = TimeSpan.FromMinutes(5) };
return Results.Ok(GetFeaturedProducts());
});
// ResponseCachingMiddleware silently skips caching ANY response that
// sets a cookie, by design (per HTTP caching semantics) - the
// Cache-Control header above is completely ignored, and every
// request re-executes GetFeaturedProducts() from scratch.
✅ Fix: Keep Cookies Off the Routes You Actually Want Cached
- Move any cookie-setting logic (session IDs, tracking cookies, anti-forgery tokens) out of the handlers for routes that are actually meant to be cached, since the middleware’s skip behavior is correct per spec but easy to trigger by accident through shared middleware earlier in the pipeline.
- Audit earlier middleware in the pipeline (authentication, anti-forgery, a custom logging or tracking component) for cookies being set globally on every response, rather than only on the routes that actually need them.
- For an endpoint that must both set a cookie and be cacheable, use output caching with explicit per-endpoint policies instead of the older response caching middleware, or cache the underlying data directly (in memory or a distributed cache) rather than relying on HTTP-level response caching at all.
⚠️ Why This Is Easy to Miss
- Nothing errors or logs a warning – the response still returns 200 with the right data and the right headers on the wire, it’s just never actually served from cache, which only becomes visible as consistently higher-than-expected load on the endpoint behind it.
- The cookie responsible is frequently set by middleware registered well before the route itself (authentication or a shared diagnostics component), so a developer reviewing just the endpoint in question often finds no cookie-setting code there at all.
Response caching middleware isn’t ignoring your Cache-Control header – it’s following a rule from the HTTP spec you never knew applied, because a cookie showed up somewhere in the pipeline before your handler even ran.




