title: Configure JWT Bearer token validation using the public security key in .NET Core
body: <p>My web application is a kind of wrapper for some 3rd party service. This 3rd party service uses the JWT Bearer authentication to access its WebAPI endpoints. The tokens are encrypted with RS256 algorithm (asymmetric).</p>
  
  <p>I have a Public Key to validate tokens signature on my side. It is easy to validate signature on jwt.io site (just paste the token and public key to the text boxes). But how do I configure <strong>TokenValidationParameters</strong> to have tokens validated automatically using specified Public Key?</p>
  
  <p>AddAuthentication code snippet:</p>
  
  <pre><code>services.AddAuthentication(options =&gt;
  {
      options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
      options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
  })
  .AddJwtBearer(options =&gt;
  {
      options.TokenValidationParameters.ValidateIssuer = true;
      options.TokenValidationParameters.ValidIssuer = "iss";
      options.TokenValidationParameters.ValidateIssuerSigningKey = true;
      options.TokenValidationParameters.IssuerSigningKey = SomeCodeToGenerateSecurityKeyUsingPublicKeyOnly("-----BEGIN PUBLIC KEY-----...-----END PUBLIC KEY-----");
      options.TokenValidationParameters.ValidateAudience = false;
      options.TokenValidationParameters.ValidateLifetime = true;
      options.TokenValidationParameters.ClockSkew = TimeSpan.Zero;
  });
  
  services.AddAuthorization(options =&gt;
  {
      options.AddPolicy("Bearer",
          new AuthorizationPolicyBuilder(new string[] { JwtBearerDefaults.AuthenticationScheme })
              .RequireAuthenticatedUser()
              .Build()
      );
  });
  </code></pre>
  
  <p>I can't just use SymmetricSecurityKey class like this:</p>
  
  <pre><code>options.TokenValidationParameters.IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("..."));
  </code></pre>
  
  <p>because of asymmetric encryption. In this case an exception occurs:</p>
  
  <pre><code>IDX10503: Signature validation failed. Keys tried: 'Microsoft.IdentityModel.Tokens.SymmetricSecurityKey , KeyId: 
  '.
  Exceptions caught:
   ''.
  token: '{"alg":"RS256","typ":"JWT"}....
  </code></pre>