如何根据客户发布一些用户声明



我希望能够根据客户的要求发出电话号码索赔或电子邮件索赔。我使用Identity Server 5(Duende(,但Identity Server 4的答案相同。

我知道我可以在ProfileService中向身份令牌添加声明,但是如何在数据库中配置客户端,以便我能够在配置文件服务中检查客户端需要什么?

应在ApiScopeClaims/ApiResourceClaimsIdentityResourceClaims中配置附加声明,具体取决于该声明应包含在访问令牌中还是包含在id令牌中(两者都可以(。

例如,如果您有:

IdentityResource{Id=1,Name="profile"}

然后在IdentityResourceClaims表中添加:

IdentityResourceClaim{Id=1,IdentityResourceId=1、Type="phone_number"}IdentityResourceClaim{Id=2,IdentityResourceId=1,Type="电子邮件"}

然后,当客户端请求profile作用域时,phone_numberemail声明类型将包含在ProfileService中的ProfileDataRequestContext.RequestedClaimTypes中。

然后在ProfileService中,您可以使用ProfileDataRequestContext.RequestedClaimTypescontext.AddRequestedClaims仅添加客户端请求的Claims

public async Task GetProfileDataAsync(ProfileDataRequestContext context)
{
if (context.RequestedClaimTypes.Any())
{
...
// create the user claims list
var claims = CreateClaims(user);
// this will filter claims list and only add those requested by the client
context.AddRequestedClaims(claims);
}
}

相关内容

最新更新