代码之家  ›  专栏  ›  技术社区  ›  Linda Lawton - DaImTo

请求电子邮件作用域时,Id令牌不包含电子邮件

  •  0
  • Linda Lawton - DaImTo  · 技术社区  · 7 年前

    我有一个identity server 4应用程序,并已将其添加到数据库中的电子邮件作用域[IdentityResources]表中。我还将电子邮件作用域添加到我与我的客户端应用程序一起使用的客户端。

    enter image description here

    我还可以看到它在UserClaimsPrincipalFactory中

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

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

    2 回复  |  直到 7 年前
        1
  •  0
  •   Wim Ombelets    7 年前

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

    魔法就在这个世界上 GetProfileDataAsync 你的方法 IProfileService 实施

    此配置文件服务用于检索您想要的任何声明并将其添加到 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
  •  0
  •   Linda Lawton - DaImTo    7 年前

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

    enter image description here

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

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

    enter image description here

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