When session is expired, the server will delete all session variables set on login and there is no way to see if it has been expired from client side (one of the many reasons on why I switched from Sessions
to JsonWebTokens
).
To use session, add following code in your Program.cs
builder.Services.AddSession(options =>
{
options.IdleTimeout = TimeSpan.FromMinutes(30);
options.Cookie.HttpOnly = true;
// make the session cookie Essential
// so that session variable is not null
// check this StackOverflow answer: https://stackoverflow.com/a/64984796/19112855
options.Cookie.IsEssential = true;
});
app.UseSession(); // before app.UseAuthentication(); and after app.UseRouting();
To manually delete all session variables, call this in your controller HttpContext.Session.Clear();