2017-02-07 16 views
4
私はJSONウェブトークンを使用するように構成されてopeniddictを使用しています

.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(); 

答えて

2

から生成されたトークンを署名するRSA秘密鍵を使用しているようです示すように、.NET 2.0でopeniddict

services.AddOpenIddict() 
.AddEntityFrameworkCoreStores<ApplicationDbContext>() 
.AddMvcBinders() 
.EnableTokenEndpoint("/connect/token") 
.AllowPasswordFlow() 
.AllowRefreshTokenFlow() 
.UseJsonWebTokens() 
// You can disable the HTTPS requirement during development or if behind a reverse proxy 
.DisableHttpsRequirement() 

// set your symmetric key 

.AddSigningKey(new SymmetricSecurityKey(Encoding.ASCII.GetBytes(Configuration.Get<AppOptions>().Jwt.SecretKey))); 
+0

は実際には、あなたはREGIする必要がありますOpenIddictオプションとJWTベアラミドルウェアオプションの両方で署名鍵を取得します。 – Pinpoint

+0

@PinpointそれはOpenIdDictserviceに設定するだけで、うまくいきます。 –

+0

私はそれを本当に疑っています。対称署名鍵をJWTミドルウェアオプションに登録しないと、IdentityModel(JWTミドルウェアの背後にあるJWTライブラリ)が 'SecurityTokenInvalidSignatureException'をスローします(' IDX10500:署名検証に失敗しました。 )、ユーザーは決して認証されません。 – Pinpoint

0

で構成され、あなたはまた、JWTのミドルウェアで、あなたのキーを登録する必要があります

services.AddAuthentication(opt => { 
       opt.DefaultScheme = JwtBearerDefaults.AuthenticationScheme; 
       opt.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; 
      }) 
      .AddJwtBearer(options => 
      { 
       options.RequireHttpsMetadata = false; 
       options.SaveToken = true; 
       //options.Audience = "http://localhost:13818/"; 
       //options.Authority = "http://localhost:13818/";     
       options.TokenValidationParameters = new 
       TokenValidationParameters 
       { 
        ValidateIssuerSigningKey = true, 
        IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes("iNivDmHLpUA223sqsfhqGbMRdRj1PVkH")), 
        ValidateIssuer = true, 
        ValidateAudience = true, 
        ValidateLifetime = true 
       }; 
      });