如何使用 InboundClaimTypeMap 进行声明映射?

How to use InboundClaimTypeMap for claim mapping?

我遇到了与此处类似的问题:https://github.com/IdentityServer/IdentityServer3.Samples/issues/9

但是解决方案对我没有帮助。

所以让我们用图片和代码更详细地解释一下:

我在客户端有这个:

不需要映射,因为 NameClaimType(RoleClaimType) 和声明列表中的声明相同

JwtSecurityTokenHandler.InboundClaimTypeMap.Clear();

在 Api 项目中我有:

在这种情况下(如果我理解正确),我必须映射,因为 NameClaimType 和 RoleClaimType 与声明值不同。

    JwtSecurityTokenHandler.InboundClaimTypeMap = new Dictionary<string, string>
    {
        {"role", System.Security.Claims.ClaimTypes.Role},
        {"name",System.Security.Claims.ClaimTypes.Name }
    };

但还是不行。我做错了什么?

InboundClaimTypeMap 用于转换传入的声明。它不设置 NameClaimTypeRoleClaimType 属性。

您的身份验证中间件应该可以选择设置名称和角色声明类型。例如:-

app.UseIdentityServerBearerTokenAuthentication(new IdentityServerBearerTokenAuthenticationOptions
                {
                    ...,
                    NameClaimType = System.Security.Claims.ClaimTypes.Name,
                    RoleClaimType = System.Security.Claims.ClaimTypes.Role 
                });