I have a .Net Core 3.1 web application and I keep getting "Accessing expired session" exception from "Microsoft.AspNetCore.Session.DistributedSession", which prevents my website from loading.
Steps to reproduce:
- Successfully navigate to the website, which caches the session cookie with the browser
- Restart web server, which clears the in-memory session cache
- Refresh browser, which tries to re-use previously cached session cookie
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.
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?
Additional Info: 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?