:.NETのコア - JWTミドルウェア認証署名キーは無視されて
// 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のACC作成したess_tokensはRS256
に設定alg
主張を持っているので、この設定は無視され、openiddictは、それがなければならない対称鍵を使用するようにopeniddictを強制するために
.AddEphemeralSigningKey();
は実際には、あなたはREGIする必要がありますOpenIddictオプションとJWTベアラミドルウェアオプションの両方で署名鍵を取得します。 – Pinpoint
@PinpointそれはOpenIdDictserviceに設定するだけで、うまくいきます。 –
私はそれを本当に疑っています。対称署名鍵をJWTミドルウェアオプションに登録しないと、IdentityModel(JWTミドルウェアの背後にあるJWTライブラリ)が 'SecurityTokenInvalidSignatureException'をスローします(' IDX10500:署名検証に失敗しました。 )、ユーザーは決して認証されません。 – Pinpoint