为什么ClaimTypes.NameIdentifier没有映射到'sub'? [英] Why is ClaimTypes.NameIdentifier not mapping to 'sub'?
问题描述
使用ASP.NET Core 2.2和Identity Server 4,我具有以下控制器:
Using ASP.NET Core 2.2 and Identity Server 4 I have the following controller:
[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
为空...
I get all the claims
but id
is null ...
我检查了 claims
中的所有值,并且有一个值为1的"sub"声明.
I checked all values in claims
and I have a 'sub' claim with value 1.
为什么ClaimTypes.NameIdentifier没有映射到'sub'?
Why is ClaimTypes.NameIdentifier not mapping to 'sub'?
推荐答案
-
要不让Microsoft Identity覆盖声明名称,您必须在
app.UseAuthentication()
之前使用JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
API启动.
使用直接的"sub"声明而不是ClaimThypes.NameIdentifier,例如 var id = this.User.FindFirstValue("sub");
Use direct "sub" claim instead of ClaimThypes.NameIdentifier e.g.
var id = this.User.FindFirstValue("sub");
有关更多参考,请参见有关它的详细讨论: https://github.com/IdentityServer/IdentityServer4/issues/2968#issuecomment-510996164
For further reference please see detailed discussion on it: https://github.com/IdentityServer/IdentityServer4/issues/2968#issuecomment-510996164
这篇关于为什么ClaimTypes.NameIdentifier没有映射到'sub'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!