【发布时间】:2020-01-19 17:39:04
【问题描述】:
使用 ASP.NET Core 2.2 和 Identity Server 4 我有以下控制器:
[HttpGet("posts"), Authorize]
public async Task<IActionResult> GetPosts() {
var authenticated = this.User.Identity.IsAuthenticated;
var claims = this.User.Identities.FirstOrDefault().Claims;
var id = this.User.FindFirstValue(ClaimTypes.NameIdentifier);
}
我得到了所有的claims 但id 是空的......
我检查了 claims 中的所有值,并且我有一个值为 1 的“子”声明。
为什么 ClaimTypes.NameIdentifier 没有映射到“sub”?
【问题讨论】:
-
尝试设置:JwtSecurityTokenHandler.DefaultMapInboundClaims = false;这主要是为了解决
sub声明被转换为nameidentifier声明的事实。 -
它没有解决它... id 仍然为空。
-
这是一个很好的问题,这就是为什么我支持它(现在从 -1 到 0),但你的例子与你的问题无关。如果您在声明中看到“sub”,则 FindFirstValue of “sub”。
标签: c# asp.net-core identityserver4 asp.net-core-2.2