请求电子邮件范围时 ID 令牌不包含电子邮件

Id token does not contain email when email scope is requested

我有一个身份服务器 4 应用程序,我已将其添加到数据库中的电子邮件范围 [IdentityResources] table。我还将电子邮件范围添加到我与我的客户端应用程序一起使用的客户端。

客户端应用程序现在在登录后提示用户同意电子邮件范围。

我还可以在 UserClaimsPrincipalFactory 中看到它

受保护的覆盖异步任务

GenerateClaimsAsync(ApplicationUser user)
        {
            var identity = await base.GenerateClaimsAsync(user);
            if (user.IsXenaSupporter)
                identity.AddClaim(new Claim("Supporter", user.Id.ToString()));
            return identity;
        }

身份确实包含电子邮件。然而,当 Id 令牌和访问令牌返回给应用程序时,它们都不包含电子邮件。当我从用户信息端点请求时也没有电子邮件。

当应用程序请求电子邮件范围时,我需要做什么才能在声明中填充电子邮件地址?我的自定义支持者声明也没有被添加

客户端应用程序提示您输入电子邮件范围这一简单事实仅意味着该范围在 IdentityServer 中被允许并在客户端请求,但不一定检索到此信息。

神奇之处在于 IProfileService 实现的 GetProfileDataAsync 方法。

此配置文件服务是您检索所需的任何声明并将其添加到 ProfileDataRequestContext 的地方。

public Task GetProfileDataAsync(ProfileDataRequestContext context)
{
    var subjectId = context.Subject.GetSubjectId();
    Guid.TryParse(subjectId, out Guid g);

    //whatever way or wherever you retrieve the claims from
    var claimsForUser = idRepo.GetUserClaimsBySubjectId(g);

    context.IssuedClaims = claimsForUser.Select(c => 
        new Claim(c.ClaimType, c.ClaimValue)).ToList();

    return Task.FromResult(0);
}

正如 here 所解释的那样,一个 id 令牌几乎应该只有一个 sub 声明 - 这就是 userinfo 端点的用途。

问题是我只将它添加到 [IdentityResources] table。

这只是定义了不同的范围。但它实际上并没有分配任何数据。

为此,我需要将其添加到 [IdentityClaims] table。

我一这样做,数据就开始在声明中返回。