1

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 "Bearer error="invalid_token". What i'm doing wrong? enter image description here

Program.cs code:

var builder = WebApplication.CreateBuilder(args);

if (!builder.Environment.IsDevelopment())
{
    //Set appsettings dir to root/data
    var path = Path.Combine(builder.Environment.ContentRootPath, $"data/appsettings.{builder.Environment.EnvironmentName}.json");
    builder.Configuration.AddJsonFile(path, optional: false);
}
ConfigureAuthServices(builder.Services, builder.Configuration);
ConfigureCompressionService(builder.Services);
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddDbContext<PostgresContext>(options => options.UseNpgsql(builder.Configuration.GetConnectionString("dbconnection")));
builder.Services.AddSwaggerGen(options =>
   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);
}

1 Answers1

1

Code is fine, i was wrong at grabbing whole data after '?access_token=.....' in OAuth.../Authorize endpoint. There are several fields and i only needed part of it