title: Security token from TokenValidatedContext from the OnTokenValidated event listener is missing last string segment
body: <p>I'm using the <strong>Microsoft.AspNetCore.Authentication.JwtBearer</strong> and <strong>System.IdentityModel.Tokens.Jwt</strong> for my .NET Core project.</p>
  <p>Whenever I generate a new token I store that to the database. First of all this is how I generate a new token</p>
  <pre><code>    public string GenerateToken(Dictionary&lt;string, object&gt; payload)
      {
          DateTime tokenExpiresAt = DateTime.Now.AddMilliseconds(1); // from config
          byte[] symmetricKey = Convert.FromBase64String(&quot;secret&quot;); // from config
          SymmetricSecurityKey symmetricSecurityKey = new SymmetricSecurityKey(symmetricKey);
      
          SecurityTokenDescriptor tokenDescriptor = new SecurityTokenDescriptor
          {
              Claims = payload,
              Expires = tokenExpiresAt,
              SigningCredentials = new SigningCredentials(symmetricSecurityKey, 
                  SecurityAlgorithms.HmacSha256Signature)
          };
      
          JwtSecurityTokenHandler tokenHandler = new JwtSecurityTokenHandler();
          SecurityToken securityToken = tokenHandler.CreateToken(tokenDescriptor);
          string token = tokenHandler.WriteToken(securityToken);
  
          return token;
      }
  </code></pre>
  <p>The generated sample token is</p>
  <blockquote>
  <p>eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6InVzZXIxIiwibmJmIjoxNTk1NDQ1NjMxLCJleHAiOjE1OTU0NDU2OTEsImlhdCI6MTU5NTQ0NTYzMX0.cWvSpKC_yYao2_ziW_ahjjHpUl2SgUZvCmsjXntxCOI</p>
  </blockquote>
  <p>If a client tries to access a protected endpoint the default configuration will handle the basic validation (configured in the DI setup in the Startup file)</p>
  <pre><code>        services
              .AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
              .AddJwtBearer(jwtBearerOptions =&gt;
              {
                  byte[] symmetricKey = Convert.FromBase64String(&quot;secret&quot;); // from config
                  SymmetricSecurityKey symmetricSecurityKey = new SymmetricSecurityKey(symmetricKey);
                  
                  jwtBearerOptions.TokenValidationParameters = new TokenValidationParameters()
                  {
                      ValidateIssuerSigningKey = true,
                      ValidateLifetime = true,
                      ValidateIssuer = false,
                      ValidateAudience = false,
                      IssuerSigningKey = symmetricSecurityKey,
                  };
  
                  jwtBearerOptions.Events = new JwtBearerEvents()
                  {
                      OnTokenValidated = ProcessAfterTokenValidation
                  };
              });
  </code></pre>
  <p>As you can see I added a method to the <code>OnTokenValidated</code> event listener. This method should check, if the token exists in the database.</p>
  <pre><code>    public async Task ProcessAfterTokenValidation(TokenValidatedContext tokenValidatedContext)
      {
          JwtSecurityTokenHandler jwtSecurityTokenHandler = new JwtSecurityTokenHandler();
          string token = jwtSecurityTokenHandler.WriteToken(tokenValidatedContext.SecurityToken);
          
          // ... check if token exists in db ...
      }
  </code></pre>
  <p>The problem with that method is that the generated token is not the exact token as it is stored in the database. There is missing a last segment. The token variable holds this</p>
  <blockquote>
  <p>eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6InVzZXIxIiwibmJmIjoxNTk1NDQ1NjMxLCJleHAiOjE1OTU0NDU2OTEsImlhdCI6MTU5NTQ0NTYzMX0.</p>
  </blockquote>
  <p>and the missing last part would be</p>
  <blockquote>
  <p>cWvSpKC_yYao2_ziW_ahjjHpUl2SgUZvCmsjXntxCOI</p>
  </blockquote>
  <p>Does someone know why the last part of the token is missing?</p>
  <p>Thanks for help!</p>