Session has not been configured 🟪 .NET

InvalidOperationException: Session has not been configured for this application or request

Code used HttpContext.Session, but session services/middleware aren’t registered — or UseSession runs after the endpoint that needs it.

Seen on: .NET

Meaning

Sessions require AddDistributedMemoryCache (or another IDistributedCache), AddSession, and app.UseSession() before endpoints/MVC. In multi-instance deployments use a shared cache (Redis/SQL).

Common causes

  • AddSession or UseSession missing
  • UseSession placed after MapControllers/endpoints
  • No IDistributedCache registered

⚡ Quick fix

  1. Register AddDistributedMemoryCache + AddSession
  2. Call app.UseSession() before mapping endpoints
  3. Use Redis/SQL cache when scaled out

Detailed fix by platform

C#

  1. csharp
    builder.Services.AddDistributedMemoryCache();
    builder.Services.AddSession(o => o.IdleTimeout = TimeSpan.FromMinutes(30));
    var app = builder.Build();
    app.UseSession();
    app.MapControllers();

How to diagnose

  1. Registration — Services added?
  2. Order — Middleware order

🔧 Still not fixed?

Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.