How to handle cookie expiration in asp.net core

后端 未结 3 1451
青春惊慌失措
青春惊慌失措 2021-02-10 02:47

I would like to know how to properly handle the fact that the cookie expired? Is it possible to execute a custom action ?

What I would like to achieve is that when the c

3条回答
  •  逝去的感伤
    2021-02-10 03:23

    It seems there is no event for your case but you can use OnRedirectToLogin to change redirect uri. Here is an example:

    OnRedirectToLogin = async (context) =>
    {
          var binding = context.HttpContext.Features.Get()?.GetProvidedTokenBindingId();
          var tlsTokenBinding = binding == null ? null : Convert.ToBase64String(binding);
          var cookie = context.Options.CookieManager.GetRequestCookie(context.HttpContext, context.Options.CookieName);
          if (cookie != null)
          {
                var ticket = context.Options.TicketDataFormat.Unprotect(cookie, tlsTokenBinding);
    
                var expiresUtc = ticket.Properties.ExpiresUtc;
                var currentUtc = context.Options.SystemClock.UtcNow;
                if (expiresUtc != null && expiresUtc.Value < currentUtc)
                {
                      context.RedirectUri += "&p1=yourparameter";
                }
           }
           context.HttpContext.Response.Redirect(context.RedirectUri);
    
    }
    

提交回复
热议问题