asp.net-core - JWT 令牌身份验证失败并显示消息“PII 已隐藏”
问题描述
在我的 .net core 2.2 微服务中,我尝试从 JWT 令牌中提取声明以进行一些授权。身份验证是在系统的另一部分完成的,所以我现在不需要这样做。
我在 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)
};
});
在控制器上我有这个代码:
[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!");
}
我总是收到此错误消息和“未经授权”的响应:
Microsoft.IdentityModel.Tokens.SecurityTokenInvalidSignatureException:IDX10503:签名验证失败。尝试的键:'[PII 被隐藏]'。捕获的异常:'[PII 被隐藏]'。令牌:'[PII 被隐藏]'。
解决方案
通过在 Startup 类的 Configure() 中添加以下内容,可以看到开发中隐藏的细节:
if (env.IsDevelopment())
{
IdentityModelEventSource.ShowPII = true;
}
获得完整消息后,请检查所使用的密钥对于令牌是否正确。
推荐阅读
- java - 运动包
- java - 如何在 selenium java 中将文本文件发送到 textarea
- python - 亚马逊可用性检查器
- php - 使用 Laravel 8 使用动态路由获取错误 500
- django - 如何计算 django 模型中的对象总数
- c - 项目:NodeMCU esp8266-esp12e 连接到另一个 esp8266-esp12e - 社交距离
- python - 运行 WSGI 应用程序时出现错误 NameError: 当我将 DJango 应用程序部署到 pythonanywhere 时?
- php - 我在 AWS linux 2 上设置弹性 bean stalk 时遇到问题。这是一个以 Postgres 作为数据库的 laravel 应用程序。用户密码验证失败
- android - Viewstub:如何使用两种方式数据绑定,文本不变
- redux - 当我们使用 React 在表单中选择单个产品时,如何绑定或填充复选框和单选按钮值