【问题标题】:Name claim returning username and not name名称声明返回用户名而不是名称
【发布时间】:2019-08-08 15:26:38
【问题描述】:

身份服务器 4 在 id 令牌中返回的一些值存在问题。id 令牌和 userinfo 端点都返回用户名而不是名称来声明名称。

{ 
  ........

  "preferred_username": "JohnD",
  "name": "JohnD",
  "email": "xxx@xxx.dk",      

  .....
}

如您所见,preferred_username 和 name 具有相同的值。如果我检查数据库。

  • 用户名:JohnD
  • 姓名:John Doe
  • 邮箱:xxx@xxx.dk

我直接从身份服务器 4 复制了 DefaultProfileService.cs

所以我的代码是一样的

/// <summary>
    /// This method is called whenever claims about the user are requested (e.g. during token creation or via the userinfo endpoint)
    /// </summary>
    /// <param name="context">The context.</param>
    /// <returns></returns>
    public virtual Task GetProfileDataAsync(ProfileDataRequestContext context)
    {
        context.LogProfileRequest(Logger);
        context.AddRequestedClaims(context.Subject.Claims);
        context.LogIssuedClaims(Logger);

        return Task.CompletedTask;
    }

我可以看到 context.Subject.Claims 似乎已经填充了这些数据,我无法将其移除或覆盖它。

我不知道如何解决这个问题

更新

我尝试添加 preferred_username 声明,现在我得到双打。

"preferred_username": [
"John Doe",
"JohnD"
 ]

我还尝试从列表中删除声明,但它是只读的。

【问题讨论】:

  • 您在 IS4 本身中使用什么来登录用户?例如身份还是其他?
  • Asp .net 身份与实体框架核心。
  • IS4 + Identity 集成注册了自己的IProfileService,实际上只是使用UserClaimsPrincipalFactory。你可能需要在那里定制一些东西。
  • 我从身份服务器项目中复制了默认的 DefaultProfileService.cs。它没有帮助。

标签: c# entity-framework asp.net-core oauth identityserver4


【解决方案1】:

好的,感谢Kirk Larkin 通过评论直接指出我的右侧。

实际上,我的代码中确实已经对 UserClaimsPrincipalFactory 进行了重载,我只是继续从官方代码中复制了有问题的行并交换了一些东西。

但是我现在想知道我对 name 和 preferred_username 之间区别的理解是否不正确。

 public class CustomUserClaimsPrincipalFactory : UserClaimsPrincipalFactory<ApplicationUser, IdentityRole<long>>
    {
        public CustomUserClaimsPrincipalFactory(
            UserManager<ApplicationUser> userManager,
            RoleManager<IdentityRole<long>> roleManager,
            IOptions<IdentityOptions> optionsAccessor)
            : base(userManager, roleManager, optionsAccessor)
        {
        }

        protected override async Task<ClaimsIdentity> GenerateClaimsAsync(ApplicationUser user)
        {  
            var userId = await UserManager.GetUserIdAsync(user);
            var userName = await UserManager.GetUserNameAsync(user);
            var id = new ClaimsIdentity("Identity.Application", 
                Options.ClaimsIdentity.UserNameClaimType,
                Options.ClaimsIdentity.RoleClaimType);
            id.AddClaim(new Claim(Options.ClaimsIdentity.UserIdClaimType, userId));
            id.AddClaim(new Claim(Options.ClaimsIdentity.UserNameClaimType, user.Name));
            id.AddClaim(new Claim("preferred_username", userName));
            if (UserManager.SupportsUserSecurityStamp)
            {
                id.AddClaim(new Claim(Options.ClaimsIdentity.SecurityStampClaimType,
                    await UserManager.GetSecurityStampAsync(user)));
            }
            if (UserManager.SupportsUserClaim)
            {
                id.AddClaims(await UserManager.GetClaimsAsync(user));
            }
            return id;
        }
    }

【讨论】:

  • 通常,preferred_username 类似于jskeetname 类似于Jon Skeet。 ASP.NET Core 标识没有没有扩展名的名称的概念,因此用户名是它真正可以使用的全部。
  • 是的,这与我得出的结论相同。我现在唯一的问题是我的 wsfed 新增功能之一是将名称声明加倍。其他一切正常。
猜你喜欢
  • 1970-01-01
  • 2021-06-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-13
相关资源
最近更新 更多