【发布时间】:2021-10-27 17:33:16
【问题描述】:
在 ASP.NET Core API 项目中,我需要验证位于不同于 Authorization 标头的标头中的另一个 JWT Bearer 令牌。例如,假设发送一个 GET 请求以将产品发送到 /api/products,并在名为 AccessToken 的标头中使用 Bearer 令牌。
curl --location --request GET 'https://localhost/api/products' \
--header 'AccessToken: <bearer_token>'
我正在引用 Microsoft.AspNetCore.Authentication.JwtBearer 包并在 API 项目中设置身份验证,如下所示:
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, options => Configuration.Bind("JwtSettings", options));
但是,我在 JwtBearerOptions Class 中找不到任何有关标题名称的信息。
如何配置 JWT 身份验证以从名为“AccessToken”的标头中读取 JWT? 甚至可以使用 Microsoft.AspNetCore.Authentication.JwtBearer 包吗?
【问题讨论】:
-
根据来源:github.com/dotnet/aspnetcore/blob/…,此中间件只会查看 auth 标头。如果您想做其他事情,则需要自己编写该中间件。
-
你必须在你的启动中编写一个自定义中间件。
标签: c# asp.net-core jwt asp.net-core-authenticationhandler