C# 如何使用InboundClaimTypeMap进行索赔映射?

C# 如何使用InboundClaimTypeMap进行索赔映射?,c#,asp.net-web-api,thinktecture-ident-server,C#,Asp.net Web Api,Thinktecture Ident Server,我的问题与此类似: 但这个解决方案对我没有帮助 因此,让我们用图片和代码进行更详细的解释: 我在客户机上有这个: 无需映射,因为索赔列表中的NameClaimType(RoleClaimType)和索赔是相同的 JwtSecurityTokenHandler.InboundClaimTypeMap.Clear(); 关于Api项目,我有: 在这种情况下(如果我理解正确的话),我必须映射,因为NameClaimType和RoleClaimType与索赔值不同 JwtSecurityT

我的问题与此类似:

但这个解决方案对我没有帮助

因此,让我们用图片和代码进行更详细的解释:

我在客户机上有这个:

无需映射,因为索赔列表中的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 }
    };
JwtSecurityTokenHandler.InboundClaimTypeMap=新字典
{
{“role”,System.Security.Claims.ClaimTypes.role},
{“name”,System.Security.Claims.ClaimTypes.name}
};

但仍然不起作用。我做错了什么?

InboundClaimTypeMap用于转换传入的索赔。它不设置
NameClaimType
RoleClaimType
属性

您的身份验证中间件应该具有设置名称和角色声明类型的选项。例如:

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

这太棒了,这段代码解决了我这几天遇到的问题。谢谢但我仍然不清楚在什么情况下使用“InboundClaimTypeMap”?如果你能用一些简单的例子来解释我的话。至于,它会将传入声明的type属性映射到字典的映射值。因此,在您的示例映射中,如果传入令牌具有类型为“role”的声明,则它将在您的ClaimsPrincipalA中映射到“”。“声明转换”部分中描述了这种情况。我本人从未使用AddIdentityServerAuthentication,而是始终使用AddOpenIDConnect保护客户端,并使用AddJwtBearer保护API。看到和