繁体   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