JWT 令牌身份验证失败并显示消息“PII 已隐藏"

编程入门 行业动态 更新时间:2024-10-07 22:18:52
本文介绍了JWT 令牌身份验证失败并显示消息“PII 已隐藏"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在我的 core 2.2 微服务中,我尝试从 JWT 令牌中提取声明以进行一些授权.身份验证是在系统的另一部分完成的,所以我现在不需要这样做.

in my 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 被隐藏]'.token: '[PII is hidden]'.

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 已隐藏"

本文发布于:2023-11-24 00:54:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1623374.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:令牌   身份验证   消息   JWT   PII

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!