title: Why i'm getting "Bearer error="invalid_token"" in asp.net webapi?
body: <p>I'm trying to make webapi which would use AAD SSO as auth provider. Microsoft OAuth endpoint generates right bearer ( tested at jwt.io ). But when i'm trying to access webapi endpoint with one i get HTTP 401 error with message &quot;Bearer error=&quot;invalid_token&quot;. What i'm doing wrong?
  <a href="https://i.stack.imgur.com/3W05P.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/3W05P.png" alt="enter image description here" /></a></p>
  <p><strong>Program.cs code:</strong></p>
  <pre><code>var builder = WebApplication.CreateBuilder(args);
  
  if (!builder.Environment.IsDevelopment())
  {
      //Set appsettings dir to root/data
      var path = Path.Combine(builder.Environment.ContentRootPath, $&quot;data/appsettings.{builder.Environment.EnvironmentName}.json&quot;);
      builder.Configuration.AddJsonFile(path, optional: false);
  }
  ConfigureAuthServices(builder.Services, builder.Configuration);
  ConfigureCompressionService(builder.Services);
  builder.Services.AddControllers();
  builder.Services.AddEndpointsApiExplorer();
  builder.Services.AddDbContext&lt;PostgresContext&gt;(options =&gt; options.UseNpgsql(builder.Configuration.GetConnectionString(&quot;dbconnection&quot;)));
  builder.Services.AddSwaggerGen(options =&gt;
     options.SupportNonNullableReferenceTypes()
  );
  
  builder.Logging.ClearProviders();
  builder.Logging.AddConsole();
  
  var app = builder.Build();
  
  // Configure the HTTP request pipeline.
  if (app.Environment.IsDevelopment())
  {
      app.UseSwagger();
      app.UseSwaggerUI();
      app.UseDeveloperExceptionPage();
  }
  app.UseResponseCompression();
  app.UseAuthentication();
  app.UseAuthorization();
  app.MapControllers();
  app.Run();
  
  
  void ConfigureAuthServices(IServiceCollection services, ConfigurationManager Configuration)
  {
      services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
          .AddMicrosoftIdentityWebApi(Configuration);
  }
  </code></pre>