为什么我得到的角色声明有时有命名空间,有时没有命名空间

why am i getting role claims sometimes namespaced and sometimes without namespace

我正在使用 IdentityServer4 进行身份验证。一些客户端使用引用令牌,一些客户端使用自包含令牌。

第一次我得到了键 "role" 的角色声明,第二次我得到了键“http://schemas.microsoft.com/ws/2008/06/identity/claims/role

的角色声明

知道如何将这两种情况排列成一个键吗?

你没有说你的 "Selfcontaining" 客户使用什么技术,但我假设它是一些 .NET。

以下是解决方案:

.NET 核心:

在您的 Startup.cs(客户端项目)中添加此行:

JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();

Documentation 对于 .NET Core。

.NET 框架:

再次在 Startup.cs 添加以下内容:

AntiForgeryConfig.UniqueClaimTypeIdentifier = Constants.ClaimTypes.Subject;
JwtSecurityTokenHandler.InboundClaimTypeMap = new Dictionary<string, string>();

Documentation 用于 .NET Framework。

这应该可以解决您的问题,您的所有声明都将带有简称。