簡體   English   中英

為什么我在 asp.net webapi 中收到“Bearer error="invalid_token""?

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

我正在嘗試制作將使用 AAD SSO 作為身份驗證提供程序的 webapi。 Microsoft OAuth 端點生成正確的承載(在 jwt.io 測試)。 但是,當我嘗試使用一個訪問 webapi 端點時,出現 HTTP 401 錯誤消息“Bearer error =”invalid_token”。我做錯了什么? 在此處輸入圖像描述

程序.cs代碼:

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

代碼很好,我在 OAuth.../Authorize 端點中的 '?access_token=.....' 之后抓取整個數據是錯誤的。 有幾個字段,我只需要其中的一部分

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM