Impossible to do this. If the cookie has expired, it is not sent to the server to retrieve any information. With ASP.Net Core Identity, you don't have much control over this. This gives you the ability to use cookie middleware.
This gives the user a normal redirect when the cookie expires:
public void ConfigureServices(IServiceCollection services) { services.Configure<CookieAuthenticationOptions>(options => { options.LoginPath = new PathString("/Home/Index"); }); }
The best way to achieve what you are looking for is to set the cookie expiration much later than the expiration of the authentic user session, and then run the server expiration session command and redirect the user at that point. Although this is not ideal, you have no other options when the cookie has expired.
public void ConfigureServices(IServiceCollection services) { app.UseCookieAuthentication(new CookieAuthenticationOptions() { AuthenticationScheme = "MyCookieMiddlewareInstance", // Redirect when cookie expired or not present LoginPath = new PathString("/Account/Unauthorized/"), AutomaticAuthenticate = true, // never expire cookie ExpireTimeSpan = TimeSpan.MaxValue, Events = new CookieAuthenticationEvents() { // in custom function set the session expiration // via the DB and reset it everytime this is called // if the session is still active // otherwise, you can redirect if it invalid OnValidatePrincipal = <custom function here> } }); }
Ashley lee
source share