title: .NET Core How to refresh identity user claims for every request?
body: <p>We are storing the roles in the identity claims of the authenticated user. So if back end database records change relating to roles, we would want the cookie claims to update with that information without the user being forced to log out and in again. The only way I can think of doing this is with a ActionFilter that re-generates those roles:</p>
  
  <pre><code>public class PageAccessFilter : IActionFilter
  {
  
      public void OnActionExecuting(ActionExecutingContext context)
      {
          IAuthenticationService authenticationService = (IAuthenticationService) context.HttpContext.RequestServices.GetService(typeof(IAuthenticationService));
          if (authenticationService != null)
          {
              if (context.HttpContext.User != null)
              {
                  var user = authenticationService.GetCurrentUser();
                  authenticationService.RefreshUser(user); //claims, identity regeneration, and signout/signin occur here
              }
          }
      }
  
      public void OnActionExecuted(ActionExecutedContext context)
      {
  
      }
  }
  </code></pre>
  
  <p>However, this seems expensive to me. This means the cookie is getting rewritten every request, isn't that resource intensive? Is there a better way to do this? </p>