2017-02-07 2 views
4
내가 JSON 웹 토큰을 사용하도록 구성되어 openiddict을 사용하고

:그물 코어 - 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 작성일 생성 ess_tokens는 alg 주장은 RS256로 설정, 그래서이 설정이 무시됩니다 보인다 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

는 사실 아니, 당신이 정합 할 필요가 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 
       }; 
      });