2016-07-21 122 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

你摇滚先生。请接受我谦卑的赞美和崇拜。 –