从 3.0 移动后,基于 JWT 令牌承载的身份验证在 .NET Core 2.2 下失败
Authentication based on JWT token bearer fails under .NET Core 2.2 after move from 3.0
我采用了具有有效身份验证的解决方案并将其从 Core 3.0 降级到 Core 2.2。之后,设置停止工作。显然,我遗漏的组织方式存在一些细微差别。对比博客和攻略,我看不下去了。
认证是这样配置的。很简单,只检查当前的时间戳。
TokenValidationParameters parameters = new TokenValidationParameters
{
ValidateLifetime = true,
ValidateAudience = false,
ValidateIssuer = false,
ValidIssuer = "https://localhost:44385",
ValidAudience = "https://localhost:44385"
};
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(config => config.TokenValidationParameters = parameters);
使用以下代码发行令牌。
TokenValidationParameters parameters = ...
byte[] secret = Encoding.UTF8.GetBytes("my_top_secret");
SecurityKey key = new SymmetricSecurityKey(secret);
SigningCredentials credentials = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
JwtSecurityToken jwt = new JwtSecurityToken(
"https://localhost:44385",
"https://localhost:44385",
{ new Claim("a", "b") },
DateTime.Now,
DateTime.Now.AddSeconds(3600),
credentials
);
string token = new JwtSecurityTokenHandler().WriteToken(jwt);
return Ok(new { token });
返回的值似乎是正确的,我在 JWT.IO 上对其进行了验证,所以我不怀疑这是问题所在。但是,当我将该值粘贴到 Postman 中时(就像我之前在一切正常时所做的那样),它给了我 401。
我不确定问题出在哪里,但我感觉这与我在启动 class 中配置身份验证的方式有关。我使用的参考资料是 here and here.
我错过了什么?或者,我还能如何解决它?
感谢@LGSon 的评论,我意识到问题出在哪里。由于一些奇怪和莫名其妙的原因,必须在参数中指定 IssuerSigningKey
。如果 ValidateIssuerSigningKey
的值设置为 true
是有道理的,但在我的例子中,我默认设置为 false
.
TokenValidationParameters parameters = ...;
parameters.IssuerSigningKey = Configuration.GetSecurityKey();
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(config => config.TokenValidationParameters = parameters);
在Core 2.2中无论是否使用都需要指定。在 Core 3.0 中(至少在最新的预览版中)不是!在我看来,这是一个偷偷摸摸的陷阱(边界错误已得到纠正)。
我采用了具有有效身份验证的解决方案并将其从 Core 3.0 降级到 Core 2.2。之后,设置停止工作。显然,我遗漏的组织方式存在一些细微差别。对比博客和攻略,我看不下去了。
认证是这样配置的。很简单,只检查当前的时间戳。
TokenValidationParameters parameters = new TokenValidationParameters
{
ValidateLifetime = true,
ValidateAudience = false,
ValidateIssuer = false,
ValidIssuer = "https://localhost:44385",
ValidAudience = "https://localhost:44385"
};
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(config => config.TokenValidationParameters = parameters);
使用以下代码发行令牌。
TokenValidationParameters parameters = ...
byte[] secret = Encoding.UTF8.GetBytes("my_top_secret");
SecurityKey key = new SymmetricSecurityKey(secret);
SigningCredentials credentials = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
JwtSecurityToken jwt = new JwtSecurityToken(
"https://localhost:44385",
"https://localhost:44385",
{ new Claim("a", "b") },
DateTime.Now,
DateTime.Now.AddSeconds(3600),
credentials
);
string token = new JwtSecurityTokenHandler().WriteToken(jwt);
return Ok(new { token });
返回的值似乎是正确的,我在 JWT.IO 上对其进行了验证,所以我不怀疑这是问题所在。但是,当我将该值粘贴到 Postman 中时(就像我之前在一切正常时所做的那样),它给了我 401。
我不确定问题出在哪里,但我感觉这与我在启动 class 中配置身份验证的方式有关。我使用的参考资料是 here and here.
我错过了什么?或者,我还能如何解决它?
感谢@LGSon 的评论,我意识到问题出在哪里。由于一些奇怪和莫名其妙的原因,必须在参数中指定 IssuerSigningKey
。如果 ValidateIssuerSigningKey
的值设置为 true
是有道理的,但在我的例子中,我默认设置为 false
.
TokenValidationParameters parameters = ...;
parameters.IssuerSigningKey = Configuration.GetSecurityKey();
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(config => config.TokenValidationParameters = parameters);
在Core 2.2中无论是否使用都需要指定。在 Core 3.0 中(至少在最新的预览版中)不是!在我看来,这是一个偷偷摸摸的陷阱(边界错误已得到纠正)。