JWT令牌认证失败,并且消息"PII被隐藏". [英] JWT token authentication fails with message "PII is hidden"
问题描述
在我的.net core 2.2微服务中,我尝试从JWT令牌中提取声明以进行一些授权.身份验证是在系统的另一部分上完成的,因此我现在不需要这样做.
in my .net core 2.2 microservice, I try to extract claims from a JWT token to do some authorization. authentication is done on another part of the system so I don't need to do it at this point.
我在Startup.cs中使用以下代码:
I am using this code in the Startup.cs:
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
var signingKey = Encoding.UTF8.GetBytes("SECRET_KEY");
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = false,
ValidateAudience = false,
IssuerSigningKey = new SymmetricSecurityKey(signingKey)
};
});
在控制器上,我有以下代码:
On the controller I have this code:
[Authorize]
[HttpPost]
public async Task<ActionResult<CreateResponse>> Create()
{
var userIdClaim = HttpContext.User.Claims.Where(x => x.Type == "empId").SingleOrDefault();
return Ok($"Your User ID is {userIdClaim.Value} and you can create invoices!");
}
我总是收到此错误消息和未经授权"的响应:
I always get this error message and "Unauthorized" response:
Microsoft.IdentityModel.Tokens.SecurityTokenInvalidSignatureException:IDX10503:签名验证失败.尝试过的键:"[PII隐藏]". 捕获的异常: "[PII隐藏]". 令牌:"[PII隐藏]".
Microsoft.IdentityModel.Tokens.SecurityTokenInvalidSignatureException: IDX10503: Signature validation failed. Keys tried: '[PII is hidden]'. Exceptions caught: '[PII is hidden]'. token: '[PII is hidden]'.
推荐答案
您可以通过在Startup类的Configure()中添加以下内容来查看开发中的隐藏细节:
You can see the hidden details in development by adding the following to Configure() in the Startup class:
if (env.IsDevelopment())
{
IdentityModelEventSource.ShowPII = true;
}
收到完整的消息后,请检查所使用的密钥是否正确.
Once you have the full message check the key being used is correct for the token.
这篇关于JWT令牌认证失败,并且消息"PII被隐藏".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!