How to force the web app goes to user/logout when session expired?

Dondon510 221 Reputation points
2022-09-20T03:18:34.46+00:00

How to force the web app goes to user/logout when session expired?
my C# codes below only goes to /user/logout when the session was expired if the user triggering something (ie. click a menu or hit refresh)

builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)  
.AddCookie(options =>  
{  
    options.LoginPath = "/User/Login";  
    options.AccessDeniedPath = "/User/Login";  
    options.LogoutPath = "/User/Logout";  
    options.SlidingExpiration = true;  
    options.ExpireTimeSpan = TimeSpan.FromMinutes(PrimeIoT.SIM.Models.AppSettings.Application.SESSION_TIMEOUT);  
});  
  
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,140 questions
{count} votes

2 additional answers

Sort by: Most helpful
  1. Sreeju Nair 11,606 Reputation points
    2022-09-20T04:48:53.71+00:00

    Hi @Dondon510 , when the user gets logged out, your browser do not have any clue. One option you have is to use a Javascript timer that runs in the browser. The purpose of the timer is to issue a request and check whether the user is still logged in, you may use the response status code or see the cookie in the response. (Make sure you set SlidingExpiration to false).

    Now in the javascript timer method, when you detect the cookie expired, you can redirect the user to the login page.

    Hope this helps


  2. Dondon510 221 Reputation points
    2022-09-21T00:29:32.693+00:00

    BTW,

    still related to session timeout, is it possible to set no session timeout?, currently I set to long expiry timeout like 999999 but I thought there is a better way to disable the session timeout, please CMIIW