title: IDX10503: Signature validation failed
body: <p>I getting the following error with a valid token after the application re-start or publish </p>
  
  <pre><code>IDX10503: Signature validation failed. Keys tried: 'System.IdentityModel.Tokens.RsaSecurityKey
  Exceptions caught:
  token: '{"typ":"JWT","alg":"RS256","kid":null}.{"unique_name":"test@test.com","iss":"XXXXXX","aud":"XXXXX","exp":1444876186}'
  </code></pre>
  
  <p>This is the function that generate the KEY </p>
  
  <pre><code>private void generateRsaKeys()
  {
      using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(2048))
      {
  
          key = new RsaSecurityKey(rsa.ExportParameters(true));
          credentials = new SigningCredentials (key,SecurityAlgorithms.RsaSha256Signature, SecurityAlgorithms.Sha256Digest);
          rsa.PersistKeyInCsp = true;
      }
  }
  </code></pre>
  
  <p>This is how the configuration is done</p>
  
  <pre><code>services.ConfigureOAuthBearerAuthentication(options =&gt;
  {
      options.AutomaticAuthentication = true;
      options.TokenValidationParameters.IssuerSigningKey = generateRsaKeys();
      options.TokenValidationParameters.ValidAudience = audience;
      options.TokenValidationParameters.ValidIssuer = issuer;
  
  });
  
  app.UseStaticFiles();
  app.UseOAuthBearerAuthentication();
  
  // Add MVC to the request pipeline.
  app.UseMvc();
  </code></pre>
  
  <p>and this is the action on my controller</p>
  
  <pre><code>// POST: /token
  [HttpPost()]
  public async Task&lt;IActionResult&gt; Token([FromBody] LoginModel model)
  {
      if (!ModelState.IsValid)
          return HttpBadRequest();
  
      JwtSecurityTokenHandler handler = _bearerOptions.SecurityTokenValidators.OfType&lt;JwtSecurityTokenHandler&gt;().First();
  
      try
      {
          var user = await _Repo.GetDetailAsync(model.Email);
          if (!model.Password.Equals(user.Password))
              return HttpUnauthorized();
  
          JwtSecurityToken securityToken = handler.CreateToken
          (
              issuer: _bearerOptions.TokenValidationParameters.ValidIssuer,
              audience: _bearerOptions.TokenValidationParameters.ValidAudience,
              signingCredentials: _signingCredentials,
              subject: new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.Name, user.Email) }),
              expires: DateTime.Now.AddMinutes(2)
          );
  
          string token = handler.WriteToken(securityToken);
  
          return new ObjectResult(new TokenModel() { AccessToken = token, TokenType = "bearer" });
  
      }
      catch (Exception ex)
      {
          // TODO: add loggin logic here 
          return HttpUnauthorized();
      }
  
  }
  </code></pre>