We are currently testing a new WAF for our organization. After activating it, we noticed that some requests never hit our server. Further investigation showed that the root cause was linked to our authentication cookies becoming to big. As the WAF had very strict limits configured, the request was blocked.
The cause is how cookie authentication in ASP.NET Core works by default. The complete AuthenticationTicket (the claims, the properties and the tokens) is serialized, encrypted and put in the cookie. The bigger your identity, the bigger your cookie.
Of course, we could just change the WAF configuration and increase the limits but isn't there a better way that allows us to limit the cookie size?
Let's find out together...
Keep the ticket on the server
Cookie authentication has an extension point for this: ITicketStore. When you provide an implementation through CookieAuthenticationOptions.SessionStore, the ticket is stored server-side. The cookie only contains a session key.
The interface is small:
public interface ITicketStore
{
Task<string> StoreAsync(AuthenticationTicket ticket);
Task RenewAsync(string key, AuthenticationTicket ticket);
Task<AuthenticationTicket?> RetrieveAsync(string key);
Task RemoveAsync(string key);
}
Let me walk you through the different methods:
StoreAsyncis called at sign-in and returns the key that ends up in the cookie.RenewAsyncis called when the ticket is refreshed (sliding expiration).RetrieveAsyncis called on every request to rebuild the ticket from the key.RemoveAsyncis called at sign-out.
A first implementation using IMemoryCache
We start with the simplest version:
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.Extensions.Caching.Memory;
public class MemoryCacheTicketStore : ITicketStore
{
private const string KeyPrefix = "AuthSessionStore-";
private readonly IMemoryCache _cache;
public MemoryCacheTicketStore(IMemoryCache cache)
{
_cache = cache;
}
public Task<string> StoreAsync(AuthenticationTicket ticket)
{
var key = KeyPrefix + Guid.NewGuid();
SetEntry(key, ticket);
return Task.FromResult(key);
}
public Task RenewAsync(string key, AuthenticationTicket ticket)
{
SetEntry(key, ticket);
return Task.CompletedTask;
}
public Task<AuthenticationTicket?> RetrieveAsync(string key)
{
_cache.TryGetValue(key, out AuthenticationTicket? ticket);
return Task.FromResult(ticket);
}
public Task RemoveAsync(string key)
{
_cache.Remove(key);
return Task.CompletedTask;
}
private void SetEntry(string key, AuthenticationTicket ticket)
{
var options = new MemoryCacheEntryOptions();
var expiresUtc = ticket.Properties.ExpiresUtc;
if (expiresUtc.HasValue)
{
options.SetAbsoluteExpiration(expiresUtc.Value);
}
_cache.Set(key, ticket, options);
}
}
Register it
The SessionStore property needs an instance, but our store depends on IMemoryCache. So let the options system resolve it from the container:
builder.Services.AddMemoryCache();
builder.Services.AddSingleton<ITicketStore, MemoryCacheTicketStore>();
builder.Services
.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie();
builder.Services
.AddOptions<CookieAuthenticationOptions>(CookieAuthenticationDefaults.AuthenticationScheme)
.Configure<ITicketStore>((options, store) => options.SessionStore = store);
Sign in as you always did. If you look at the cookie in your browser dev tools now, it’s only a few hundred bytes, whatever the number of claims.
Remark: The in-memory version only works for a single instance. As soon as you scale out (multiple replicas in Azure Container Apps, for example), a request that lands on another instance can’t find the ticket and the user is signed out. And every restart signs out all users.
Moving to a distributed cache
For multiple instances, store the ticket in a shared cache. IDistributedCache works with Redis, SQL Server and others. The difference is that you have to serialize the ticket yourself. ASP.NET Core has a TicketSerializer for that:
public class DistributedCacheTicketStore : ITicketStore
{
private const string KeyPrefix = "AuthSessionStore-";
private readonly IDistributedCache _cache;
public DistributedCacheTicketStore(IDistributedCache cache)
{
_cache = cache;
}
public async Task<string> StoreAsync(AuthenticationTicket ticket)
{
var key = KeyPrefix + Guid.NewGuid();
await RenewAsync(key, ticket);
return key;
}
public Task RenewAsync(string key, AuthenticationTicket ticket)
{
var options = new DistributedCacheEntryOptions();
var expiresUtc = ticket.Properties.ExpiresUtc;
if (expiresUtc.HasValue)
{
options.SetAbsoluteExpiration(expiresUtc.Value);
}
var bytes = TicketSerializer.Default.Serialize(ticket);
return _cache.SetAsync(key, bytes, options);
}
public async Task<AuthenticationTicket?> RetrieveAsync(string key)
{
var bytes = await _cache.GetAsync(key);
return bytes is null ? null : TicketSerializer.Default.Deserialize(bytes);
}
public Task RemoveAsync(string key) => _cache.RemoveAsync(key);
}
The registration stays the same, only the implementation changes.
Remark: The ticket now lives in your cache and contains the user’s claims (and tokens if you saved them). Treat that cache with the same care as a database: restrict access and encrypt the connection. You can even take it one step further and encrypt the ticket itself using the DataProtection api.
Be aware of the trade-off
Server-side storage brings something extra: you can invalidate a session. Remove the key from the store and the user is signed out on the next request. With a cookie-only ticket that’s not possible, because the cookie is valid until it expires.
The price is a lookup on every request, and a cache that is now part of your authentication path. If it’s down, nobody can sign in.
So the question becomes: do you need it? A small cookie with a handful of claims doesn’t need a ticket store. Large claim sets, saved tokens or the need to revoke sessions are good reasons to add one.
I hope that was helpful.

