title: C# JWT token persist claims after update?
body: <p>I want to update the user's claims using <code>HttpContext.User</code> instance, but after updating the claims they only stay within the scope of the current request.  I need to make it persist for the upcoming requests as well, please help me out with this.</p>
  
  <p>Please find my code below.  In the <code>POST</code> method I update the claim and next time when the <code>GET</code> method is hit, I am trying to get the updated value but I get the old value.</p>
  
  <pre><code>[Route("login")]
  public class LoginController : Controller
  {
  
      private readonly IList&lt;User&gt; users = new List&lt;User&gt;
      {
          new User { UserName = "admin", Password = "1234", Role="Administrator"},
          new User { UserName = "user", Password ="1234", Role="User"}
      };
      private IConfiguration _config;
  
      public LoginController(IConfiguration config)
      {
          this._config = config;
      }
  
      [HttpGet("Enter")]
      public IActionResult Login([FromQuery]string username, [FromQuery]string password)
      {
          User login = new User();
          login.UserName = username;
          login.Password = password;
          IActionResult response = Unauthorized();
  
          var user = AuthenticateUser(login);
          if(user != null)
          {
              var tokenStr = GenerateJSONWebToken(user);
              response = Ok(new { token = tokenStr });
          }
          return response;
      }
  
      private string GenerateJSONWebToken(User userinfo)
      {
          var securityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_config["Jwt:Key"]));
          var credentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256);
  
  
          var claims = new[]
          {
              new Claim("username", userinfo.UserName),
              new Claim(ClaimTypes.Role, userinfo.Role),
              new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString())
          };
  
          var token = new JwtSecurityToken(
          issuer: _config["Jwt:Issuer"],
          audience: _config["Jwt:Issuer"],
          claims,
          expires: DateTime.Now.AddMinutes(120),
          signingCredentials: credentials
              );
  
          var encodettoken = new JwtSecurityTokenHandler().WriteToken(token);
          return encodettoken;
  
      }
  
      [Authorize(Roles = "Administrator")]
      [Authorize]
      [HttpPost("Post")]
      public string Post()
      {
          var identity = HttpContext.User.Identity as ClaimsIdentity;
          IList&lt;Claim&gt; claim = identity.Claims.ToList();
          var username = claim[0].Value;
          return "Welcome To " + username;
  
        // i update claim here
            var identityClaims = (ClaimsIdentity)User.Identity;
              var username = identityClaims.FindFirst("username");
              if (username != null)
                  identityClaims.RemoveClaim(username);
              identityClaims.AddClaim(new Claim("username", "sample username"));
      }
  
      [Authorize(Roles = "Administrator, User")]
      [HttpGet("GetValue")]
      public ActionResult&lt;IEnumerable&lt;string&gt;&gt; Get()
      {
          // in the next get request i try to access the claim, but it does not have the updated value
          // instead it has the old value
          // here i have to persist the value
            var identityClaims = (ClaimsIdentity)User.Identity;
              var username = identityClaims.FindFirst("username");
          return new string[] { "Value1", "Value2", "Value3" };
      }
  
      private User AuthenticateUser(User login)
      {
          User entity = null;
          if (users.Where(x=&gt;x.UserName == login.UserName &amp;&amp; x.Password == login.Password).ToList().Count() &gt; 0)
          {
              entity = users.Where(x =&gt; x.UserName == login.UserName &amp;&amp; x.Password == login.Password).FirstOrDefault();
          }
          return entity;
      }
  }
  </code></pre>