我正在使用配置为使用 json Web 令牌的 openiddict:
// Add authentication
services.AddAuthentication();
// Add OpenId Connect/OAuth2
services.AddOpenIddict()
.AddEntityFrameworkCoreStores<ApplicationDbContext>()
.AddMvcBinders()
.EnableTokenEndpoint("/connect/token")
.AllowPasswordFlow()
.AllowRefreshTokenFlow()
.UseJsonWebTokens() // access_token should be jwt
// You can disable the HTTPS requirement during development or if behind a reverse proxy
.DisableHttpsRequirement()
// Register a new ephemeral key, that is discarded when the application
// shuts down. Tokens signed using this key are automatically invalidated.
// To be used during development
.AddEphemeralSigningKey();
我已通过 JWT 中间件按以下方式配置:
// Add Jwt middleware for authentication
var secretKey = Configuration.Get<AppOptions>().Jwt.SecretKey;
app.UseJwtBearerAuthentication(new JwtBearerOptions
{
AutomaticAuthenticate = true,
AutomaticChallenge = true,
RequireHttpsMetadata = env.IsProduction(),
Audience = Configuration.Get<AppOptions>().Jwt.Audience,
Authority = Configuration.Get<AppOptions>().Jwt.Authority,
TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(secretKey)),
ValidateIssuer = true,
// makes no difference seemingly being ignored
//ValidIssuer = Configuration.Get<AppOptions>().Jwt.Authority,
ValidateAudience = true,
ValidAudience = Configuration.Get<AppOptions>().Jwt.Audience,
ValidateLifetime = true,
}
});
// Add OpedId Connect middleware
app.UseOpenIddict();
如您所见,颁发者签名密钥设置为对称密钥:
IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(secretKey)),
但是创建的 jwt access_tokens 已将alg
声明设置为RS256
,因此似乎此设置被忽略了,并且 openiddict 使用 RSA 私钥对生成的令牌进行签名
.AddEphemeralSigningKey();