title: .NET core JWT validation with Azure B2C
body: <p>I have a problem with a token validation against <code>Azure B2C</code>. The error is:</p>
  
  <blockquote>
    <p>AuthenticationFailed: IDX10205: Issuer validation failed. Issuer:
    '[PII is hidden by default. Set the 'ShowPII' flag in
    IdentityModelEventSource.cs to true to reveal it.]'. Did not match:
    validationParameters.ValidIssuer: '[PII is hidden by default. Set the
    'ShowPII' flag in IdentityModelEventSource.cs to true to reveal it.]'
    or validationParameters.ValidIssuers: '[PII is hidden by default. Set
    the 'ShowPII' flag in IdentityModelEventSource.cs to true to reveal
    it.]'.</p>
  </blockquote>
  
  <p>In my <code>.net core</code> <code>webapi</code> project, I added <strong>System.IdentityModel.Tokens.Jwt</strong> from Nuget. Then in the <em>Startup.cs</em> under <code>ConfigureServices</code> I added:</p>
  
  <pre><code>  services.AddAuthentication(options =&gt;
    { 
      options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme; 
    })
    .AddJwtBearer(jwtOptions =&gt;
    {
      jwtOptions.Authority = 
           $"https://login.microsoftonline.com/tfp/{Configuration["B2CTenantId"]}/" + 
           "{Configuration["B2CPolicy"]}/v2.0/";
      jwtOptions.Audience = Configuration["B2CClientId"];
      jwtOptions.Events = new JwtBearerEvents
      {
        OnAuthenticationFailed = AuthenticationFailed
      };
    });
  
    private Task AuthenticationFailed(AuthenticationFailedContext arg)
    {
          // For debugging purposes only!
          var s = $"AuthenticationFailed: {arg.Exception.Message}";
          arg.Response.ContentLength = s.Length;
          arg.Response.Body.Write(Encoding.UTF8.GetBytes(s), 0, s.Length);
  
          return Task.FromResult(0);
    }
  </code></pre>
  
  <p>where <code>B2CTenandId</code> is similar to <code>mytenant.onmicrosoft.com</code>, <code>B2CClientId</code> is the if from Azure Portal and <code>B2CPolicy</code> is <code>b2c_1_susi</code> (created for the login).</p>
  
  <p><a href="https://i.stack.imgur.com/4HygP.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/4HygP.png" alt="Azure B2C configuration"></a></p>