2016-07-21 126 views
1

使用ASP.NET Core 1.0(rtm),我生成了minimal verifiable sample,我看到我生成的JSON Web令牌不是經過.NET核心JSON網絡令牌中間件要求的挑戰,以及與此錯誤是失敗:UseJwtBearerAuthentication失敗,IDX10504:無法驗證簽名,令牌沒有簽名

Microsoft.IdentityModel.Tokens.SecurityTokenInvalidSignatureException: 
IDX10504: Unable to validate signature, token does not have a signature: 
'... ... token value here...' 

我已經產生了最小的樣品,其曾在ASP.NET核心RC1罰款,但不工作的RTM 。我沒有移植到RC2進行測試,但我相信這樣的測試毫無意義。您可以通過提供的測試腳本行使演示:

python tests\testloginapi.py 
    GET OK: 200 http://localhost:54993/authorize/login?username=TEST&pas... 
    POST OK: 200 http://localhost:54993/authorize/login?username=TEST&pas... 
    authorization token received... eyJhbGciOi... 
    expected status 200 but got 401 for GET http://localhost:54993/authorizetest/test 

我的小例子的突出要點是:

Startup.cs方法配置有:

 app.UseJwtBearerAuthentication(_keyArtifacts.getJwtBearerOptions()); 

承載選項都是這樣:

public static JwtBearerOptions CreateJwtBearerOption(RsaSecurityKey key, string tokenIssuer, string tokenAudience) 
     { 
      var jwtBearerOptions = new JwtBearerOptions(); 


      jwtBearerOptions.AutomaticAuthenticate = true; 
      jwtBearerOptions.AutomaticChallenge = true; 
      jwtBearerOptions.TokenValidationParameters.ValidateIssuerSigningKey = true; 
      jwtBearerOptions.TokenValidationParameters.IssuerSigningKey = key; 
      jwtBearerOptions.TokenValidationParameters.ValidIssuer = tokenIssuer; 
      jwtBearerOptions.TokenValidationParameters.ValidateIssuer = true; 
      jwtBearerOptions.TokenValidationParameters.ValidateLifetime = true; 
      jwtBearerOptions.TokenValidationParameters.ClockSkew = TimeSpan.Zero; 



      jwtBearerOptions.TokenValidationParameters.ValidAudience = tokenAudience; 
      return jwtBearerOptions; 
     } 

這個問題真的可以歸結爲:

  1. 在Asp.net核心1.0 rtm中,創建通過中間件挑戰的令牌的最小步驟是什麼?

  2. 我,我只是缺少一些簡單的步驟(如爲一個小行代碼也許),這將使這個演示的工作,其中包括有「簽約」的工作?

  3. 鑑於demo仍然是一段可怕的代碼,並且沒有人應該在生產中使用它(因爲我已經爲此感到羞愧),我希望這個問題仍然可以闡明如何使至少在演示範圍內,UseJwtBearerAuthentication系統實際上工作。

+0

有用的信息:https://stormpath.com/blog/token-authentication-asp-net-core –

回答

3

In Asp.net core 1.0 rtm, what are the minimal steps to create a token that will pass the middleware challenge? Am I simply missing some simple step (as little as one line of code perhaps) that would make this demo work, including having "signing" work?

IdentityModel(負責驗證由JWT承載中間件獲得令牌的庫)是無法驗證您的JWT令牌,因爲它們有效地沒有任何簽名,如錯誤你」解釋重新看。

缺少簽名很可能導致通過創建自己的令牌時,你並沒有使用SecurityTokenDescriptor.SigningCredentials事實:

JwtSecurityTokenHandler handler = BearerOptions 
    .SecurityTokenValidators 
    .OfType<JwtSecurityTokenHandler>() 
    .First(); 

var tokenData = new SecurityTokenDescriptor 
{ 

    Issuer = BearerOptions.TokenValidationParameters.ValidIssuer, 
    Audience = BearerOptions.TokenValidationParameters.ValidAudience, 
    Subject = new ClaimsIdentity(claims), 
    Expires = DateTime.Now.AddDays(1), 
    NotBefore = DateTime.Now 
}; 

/*JwtSecurityToken*/ 
var securityToken = 
    handler.CreateToken 
    (
     tokenData 
    ); 

解決這個問題,它應該工作。

Given that the demo is still a horrible piece of code, and nobody should ever use it in production (because I'm already ashamed of it), my hope is this question can still be illuminating as to how to make the UseJwtBearerAuthentication system actually work, at least at a demo scale.

其實,問題不在於你如何使用JWT承載中間件,但你如何生成自己的令牌。實現您自己的代幣發行醬是好的,但使用的OAuth2一樣或ID連接標準一般都比較好,更容易使用(例如,具有OIDC服務器時,您不必配置JWT承載中間件,因爲它會直接下載使用OIDC discovery簽署密鑰)。

隨意獲取更多信息,請閱讀本等SO answer

+0

我想我已經有一個SigningCredentials對象。所以也許我非常接近。 –

+0

你已經注入了'SigningCredentials'作爲構造參數,爲什麼不簡單地流動呢? – Pinpoint

+0

你搖滾先生。請接受我謙卑的讚美和崇拜。 –