【问题标题】:Id token does not contain email when email scope is requested请求电子邮件范围时,ID 令牌不包含电子邮件
【发布时间】:2018-12-03 20:51:24
【问题描述】:

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

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

我还可以在 UserClaimsPrincipalFactory 中看到它

受保护的覆盖异步任务

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

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

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

【问题讨论】:

    标签: c# asp.net-core asp.net-identity identityserver4


    【解决方案1】:

    客户端应用程序提示您输入电子邮件范围这一简单事实仅意味着 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 令牌应该几乎只有一个子声明 - 这就是 userinfo 端点的用途。

    【讨论】:

      【解决方案2】:

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

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

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

      一旦我这样做了,数据就开始在索赔中返回。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-06-07
        • 1970-01-01
        • 2017-09-23
        • 1970-01-01
        • 1970-01-01
        • 2013-02-10
        • 2014-10-07
        相关资源
        最近更新 更多