在 ASP.NET Core 中验证 Entra AD 应用注册颁发的 JWT 令牌

问题描述 投票:0回答:1

我现在有一个 Entra/Azure AD 应用程序注册,小型 Angular SPA 正在使用它来向 ASP.NET Core Web API 进行身份验证。

当我解码 JWT 时,

iss
(发行者)是:

 "iss": "https://sts.windows.net/64..............a21/",

在我的

Program.cs
中,我有:

builder.Services.AddAuthentication(options =>
    {
    options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
    options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
    options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(o =>
{
    o.TokenValidationParameters = new TokenValidationParameters
    {
        ValidIssuer = builder.Configuration["https://login.microsoftonline.com/64..............a21"],
        ValidAudience = builder.Configuration["http://localhost:5036/"],
        IssuerSigningKey = new SymmetricSecurityKey
        (Encoding.UTF8.GetBytes(builder.Configuration["?????"])), //this is what i feel like I am missing
        ValidateIssuer = true,
        ValidateAudience = true,
        ValidateLifetime = false,
        ValidateIssuerSigningKey = true
    };
});

在本地开发模式下运行我的 Angular 应用程序以及本地 API 时,我收到 http 错误 401。我按照这个指南进行应用程序注册。

所以我的问题是 - 如何获取签名密钥,以便我的应用程序可以验证来自 Azure 的 JWT 令牌。

c# authentication jwt asp.net-core-webapi
1个回答
0
投票

您应该只需要使用这样的代码:

.AddJwtBearer(options =>
{
    options.Authority = ...;
    options.Audience = ...;
}

您更广泛的问题似乎是您收到了错误类型的 JWT 访问令牌,JWT 标头中带有

nonce
。要修复它,您需要
expose an API scope
。我的博客文章的第 4 步有更多详细信息。

© www.soinside.com 2019 - 2024. All rights reserved.