title: Invalid Audience Token Validation
body: <p>I have a token tied to each request from the header. I want to be able to retrieve that token and validate it with the public key from the cert that I have. I am trying to make it so my endpoints get validated with my public key through identity server 4 using asp.net core. I am getting this error -> </p>
  
  <blockquote>
    <p>Microsoft.IdentityModel.Tokens.SecurityTokenInvalidAudienceException: IDX10214: Audience validation failed. Audiences: '[PII is hidden by default. Set the 'ShowPII' flag in IdentityModelEventSource.cs to true to reveal it.]'. Did not match: validationParameters.ValidAudience: '[PII is hidden by default. Set the 'ShowPII' flag in IdentityModelEventSource.cs to true to reveal it.]' or validationParameters.ValidAudiences: '[PII is hidden by default. Set the 'ShowPII' flag in IdentityModelEventSource.cs to true to reveal it.]'.</p>
  </blockquote>
  
  <p>Startup.cs</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Security.Cryptography.X509Certificates;
  using System.Text;
  using System.Threading.Tasks;
  using IdentityServer4.AccessTokenValidation;
  using Microsoft.AspNetCore.Authentication.JwtBearer;
  using Microsoft.AspNetCore.Authorization;
  using Microsoft.AspNetCore.Builder;
  using Microsoft.AspNetCore.Hosting;
  using Microsoft.AspNetCore.Http;
  using Microsoft.AspNetCore.Mvc.Authorization;
  using Microsoft.AspNetCore.Mvc.Formatters;
  using Microsoft.AspNetCore.Mvc.Infrastructure;
  using Microsoft.Extensions.Configuration;
  using Microsoft.Extensions.DependencyInjection;
  using Microsoft.Extensions.Logging;
  using Microsoft.IdentityModel.Tokens;
  using Newtonsoft.Json;
  using Swashbuckle.AspNetCore.Swagger;
  
  namespace Reveal.IDP.ClientAPI
  {
      public class Startup
      {
          public static IConfigurationRoot Configuration;
          public static string ConnectionString;
  
          public static string Uri;
  
          public Startup(IHostingEnvironment env)
          {
              var builder = new ConfigurationBuilder()
                  .SetBasePath(env.ContentRootPath)
                  .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
                  .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true)
                  .AddEnvironmentVariables();
  
              Configuration = builder.Build();
  
              ConnectionString = Configuration["connectionStrings:revealUserDBConnectionString"];
              Uri = Configuration["uri"];
          }
          // This method gets called by the runtime. Use this method to add services to the container.
          // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
          public void ConfigureServices(IServiceCollection services)
          {
              var connectionString = ConnectionString;
  
  
              services.AddMvcCore()
                  .AddAuthorization()
                  .AddJsonFormatters();
  
              services.AddAuthentication("Bearer")
                  .AddIdentityServerAuthentication(options =&gt;
                  {
                      options.Authority = "http://localhost:5000";
                      options.RequireHttpsMetadata = false;
                      options.ApiName = "client";
                  });
  
  
  
              // Service DI
              services.AddScoped&lt;IUserService, UserService&gt;();
  
              // Repository DI
              services.AddScoped&lt;IUserRepository, UserRepository&gt;();
  
              services.AddCors(options =&gt;
              {
                  options.AddPolicy("AllowCors", builder =&gt; builder.AllowAnyOrigin()
                                                                   .AllowAnyMethod()
                                                                   .AllowAnyHeader()
                                                                   .WithExposedHeaders("x-pagination")
                                                                   .AllowCredentials());
              });
  
              services.AddSingleton&lt;IActionContextAccessor, ActionContextAccessor&gt;();
  
              services.AddMvc(config =&gt;
              {
                  config.RespectBrowserAcceptHeader = true;
                  config.ReturnHttpNotAcceptable = true;
                  config.OutputFormatters.Add(new XmlDataContractSerializerOutputFormatter());
              })
                  .AddJsonOptions(opt =&gt;
                  {
                      opt.SerializerSettings.DateFormatHandling = DateFormatHandling.IsoDateFormat;
                      opt.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Utc;
                  });
  
  
          }
  
          // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
          public void Configure(IApplicationBuilder app,
              IHostingEnvironment env,
              ILoggerFactory loggerFactory,
              IApplicationLifetime appLifetime)
          {
              if (env.IsDevelopment())
              {
                  app.UseDeveloperExceptionPage();
              }
  
              loggerFactory.AddConsole(Configuration.GetSection("Logging"));
              loggerFactory.AddDebug();
  
              app.UseAuthentication();
  
              app.UseMiddleware(typeof(ErrorHandlingMiddleware));
  
              app.UseCors("AllowCors");
  
              app.UseStaticFiles();
  
              app.UseMvcWithDefaultRoute();
  
              app.UseSwagger();
  
              app.UseSwaggerUI(c =&gt;
              {
                  string basePath = Environment.GetEnvironmentVariable("ASPNETCORE_APPL_PATH");
                  if (basePath == null) basePath = "/";
                  if (basePath == "/") basePath = "";
                  c.SwaggerEndpoint($"{basePath}/swagger/v1/swagger.json", "API");
              });
              app.UseMvcWithDefaultRoute();
  
          }
  
  
      }
  }
  </code></pre>