gpt4 book ai didi

asp.net-core - ASP.NET Core JWT 身份验证是否支持多个对称签名 key ?

转载 作者:行者123 更新时间:2023-12-02 03:01:02 25 4
gpt4 key购买 nike

一些库,例如 https://www.nuget.org/packages/JWT支持多个对称签名 key 的概念。

如果可能,我想使用 MS 提供的 Microsoft.AspNetCore.Authentication.JwtBearer 实现。该实现是否支持多个对称签名 key ?

SymmetricSecurityKey类似乎只支持一个键。

多次调用 JwtBearerExtensions.AddJwtBearer 抛出异常:InvalidOperationException: Scheme already exists: Bearer

想要支持多个签名 key 的原因是为了支持滚动 key 场景。

最佳答案

TokenValidationParameters 中的自定义 IssuerSigningKeyResolver 可用于提供多个 key :

services.AddAuthentication(x =>
{
x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(x =>
{
x.RequireHttpsMetadata = false;
x.SaveToken = true;
x.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuerSigningKey = true,
IssuerSigningKeyResolver = (string token, SecurityToken securityToken, string kid, TokenValidationParameters validationParameters) =>
{

List<SecurityKey> keys = new List<SecurityKey>();

var signingKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes("yourFirstkey"));
keys.Add(signingKey);
var signingKey1 = new SymmetricSecurityKey(Encoding.ASCII.GetBytes("yourSecondkey"));
keys.Add(signingKey1);
return keys;
},
ValidateIssuer = false,
ValidateAudience = false
};
});

关于asp.net-core - ASP.NET Core JWT 身份验证是否支持多个对称签名 key ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59974471/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com