Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy
Asp.Net Core

Asp.Net Core: Fix Output Caching Serving a Stale Authorized Page

- 19.11.26 - ErcanOPAK

⚙️ Fix Output Caching Serving a Stale Authorized Page

The Output Caching middleware caches a response keyed, by default, on the request path and query string only — it does not automatically vary that key by the authenticated user unless a policy explicitly tells it to. If `[OutputCache]` or a matching cache policy is applied to a route that renders user-specific, authorized content, the FIRST user to hit that route after the cache is empty gets their personalized response cached under that shared path+query key, and every DIFFERENT user hitting the same URL within the cache’s expiration window is served that first user’s page verbatim.

🔎 The Problem

builder.Services.AddOutputCache(options =>
{
    options.AddPolicy("PerUser", policy =>
        policy.SetVaryByHeader("Cookie")
              .Expire(TimeSpan.FromSeconds(30)));
});

// ...

app.MapGet("/dashboard", DashboardHandler)
   .CacheOutput("PerUser"); // without a vary rule, every user shares one entry

✅ Fix: Vary the Cache Key by Who’s Asking

  • Never apply a blanket `[OutputCache]` attribute or a catch-all cache policy to a route group without checking whether every route in that group is actually anonymous, static content — a single authorized endpoint swept in by a broad policy is enough to leak another user’s page.
  • For any endpoint that legitimately needs caching AND varies by user, add an explicit `VaryByValue` keyed on the user’s id or claims, rather than `VaryByHeader(“Cookie”)` alone, which varies correctly but can fragment the cache badly if session cookies are unique per request.
  • Add an integration test that hits a cached, authorized endpoint as two different authenticated users in sequence and asserts the second response actually contains the second user’s data — this is the one test that catches exactly this bug.

🚨 Why This Is Dangerous

  • A personalized page served to the wrong logged-in user isn’t a cosmetic glitch — depending on what the page shows, it can be an active information-disclosure incident the moment a second user loads that URL, with nothing in a typical error log to flag it.

A cache key that ignores who’s asking will happily hand your page to whoever asks next.

— Backend Architect

Related posts:

ASP.NET Core: Fix an Authorize Policy That Silently Never Runs Because It Was Applied to a Minimal A...

ASP.NET Core: Fix an Authorize Policy That Silently Never Runs Because It Was Applied to a Minimal A...

ASP.NET Core: Fix Localization Falling Back to the Default Culture Because the Accept-Language Heade...

ASP.NET Core: Fix Localization Falling Back to the Default Culture Because the Accept-Language Heade...

ASP.NET Core: Fix Localized Resource Strings Falling Back to the Default Culture Because a Satellite...

ASP.NET Core: Fix Localized Resource Strings Falling Back to the Default Culture Because a Satellite...

Views: 3

Post navigation

SQL Server: Fix a Trigger Firing Twice on One Merge Statement
Docker: Fix a Container Losing Logs After a Restart Policy

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

November 2026
M T W T F S S
 1
2345678
9101112131415
16171819202122
23242526272829
30  
« Oct    

Most Viewed Posts

  • How to make theater mode the default for Youtube (1,059)
  • Get the User Name and Domain Name from an Email Address in SQL (983)
  • How to add default value for Entity Framework migrations for DateTime and Bool (981)
  • Get the First and Last Word from a String or Sentence in SQL (861)
  • How to select distinct rows in a datatable in C# (856)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (658)
  • Add Constraint to SQL Table to ensure email contains @ (613)
  • Average of all values in a column that are not zero in SQL (571)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (549)
  • Find numbers with more than two decimal places in SQL (477)

Recent Posts

  • Visual Studio: Fix Quick Actions Hiding Behind a Stale Squiggle
  • Photoshop: Fix a Smart Filter Resetting After You Relink It
  • Ajax: Fix a Cancelled Request Still Updating Stale UI State
  • C#: Fix a Span Captured by a Lambda Throwing at Runtime
  • The AI Prompt That Converts a REST Endpoint Into a Typed SDK
  • Git: Fix a Worktree Locking a Branch Needed Somewhere Else
  • Asp.Net Core: Fix a Background Service Leaking Scoped Dependencies
  • SQL Server: Fix a Columnstore Index Fragmenting After Bulk Deletes
  • Docker: Fix a Container Losing Logs After a Restart Policy
  • Asp.Net Core: Fix Output Caching Serving a Stale Authorized Page

Most Viewed Posts

  • How to make theater mode the default for Youtube (1,059)
  • Get the User Name and Domain Name from an Email Address in SQL (983)
  • How to add default value for Entity Framework migrations for DateTime and Bool (981)
  • Get the First and Last Word from a String or Sentence in SQL (861)
  • How to select distinct rows in a datatable in C# (856)

Recent Posts

  • Visual Studio: Fix Quick Actions Hiding Behind a Stale Squiggle
  • Photoshop: Fix a Smart Filter Resetting After You Relink It
  • Ajax: Fix a Cancelled Request Still Updating Stale UI State
  • C#: Fix a Span Captured by a Lambda Throwing at Runtime
  • The AI Prompt That Converts a REST Endpoint Into a Typed SDK

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com