我希望能够根据客户的要求发出电话号码索赔或电子邮件索赔。我使用Identity Server 5(Duende(,但Identity Server 4的答案相同。
我知道我可以在ProfileService
中向身份令牌添加声明,但是如何在数据库中配置客户端,以便我能够在配置文件服务中检查客户端需要什么?
应在ApiScopeClaims
/ApiResourceClaims
或IdentityResourceClaims
中配置附加声明,具体取决于该声明应包含在访问令牌中还是包含在id令牌中(两者都可以(。
例如,如果您有:
IdentityResource
{Id=1,Name="profile"}
然后在IdentityResourceClaims
表中添加:
IdentityResourceClaim
{Id=1,IdentityResourceId=1、Type="phone_number"}IdentityResourceClaim
{Id=2,IdentityResourceId=1,Type="电子邮件"}
然后,当客户端请求profile
作用域时,phone_number
和email
声明类型将包含在ProfileService
中的ProfileDataRequestContext.RequestedClaimTypes
中。
然后在ProfileService
中,您可以使用ProfileDataRequestContext.RequestedClaimTypes
和context.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);
}
}