简体   繁体   中英

Why i'm getting "Bearer error="invalid_token"" in asp.net webapi?

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? 在此处输入图像描述

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);
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM