title: "Accessing expired session" exception from DistributedSession in ASP.Net Core
body: <p>I have a .Net Core 3.1 web application and I keep getting &quot;Accessing expired session&quot; exception from &quot;Microsoft.AspNetCore.Session.DistributedSession&quot;, which prevents my website from loading.</p>
  <p>Steps to reproduce:</p>
  <ol>
  <li>Successfully navigate to the website, which caches the session cookie with the browser</li>
  <li>Restart web server, which clears the in-memory session cache</li>
  <li>Refresh browser, which tries to re-use previously cached session cookie</li>
  </ol>
  <p>At this point the exception just loads up my logs until the web server crashes.  The only way to get it to stop is to clear the session cookie in the browser, which will allow it to generate a fresh session cookie.</p>
  <p>Why does the session mechanism, which I enabled using AddDistributedMemoryCache and AddSession, not just create a fresh session when it gets this request using the old cookie?  If I could catch the exception somewhere, maybe I could deal with it in my code, but I do not even know how to trap the exception.  Can anyone offer a way to prevent this error without manually deleting the browser cookie?</p>
  <p><strong>Additional Info:</strong>
  I found the source code for this, and apparently, it is not actually an error, but yet it is preventing my page from rendering and it is crashing my webserver with an out of memory exception because it logs the message 1000's of times.  Any suggestions for how to get around this?</p>
  <p><a href="https://github.com/dotnet/aspnetcore/blob/b0a6755b5ef103b57394f115613b45a37912600a/src/Middleware/Session/src/LoggingExtensions.cs" rel="nofollow noreferrer">Source Code Logging</a></p>
  <p><a href="https://github.com/dotnet/aspnetcore/blob/b0a6755b5ef103b57394f115613b45a37912600a/src/Middleware/Session/src/DistributedSession.cs" rel="nofollow noreferrer">Source Code Handling Event</a></p>