背景

项目开发过程中,需要在Application层使用当前用户的Username和Name,然而查看CurrentUser发现,Name属性竟然为null,而UserName正常显示。

处理

查看了AuthServer的数据初始化代码,也没发现什么问题,

QQ截图20201126133430.jpg

明明已经添加了name的Claim,但是却无法显示,而Email则正常显示。于是猜测可能是这里的名为name的Claim并非CurrentUser中的Name属性。为了验证,我查看了abp vNext的源码,找到了AbpUserClaimsPrincipalFactory 类,代码如下:

 public class AbpUserClaimsPrincipalFactory : UserClaimsPrincipalFactory<IdentityUser, IdentityRole>, ITransientDependency
    {
        public AbpUserClaimsPrincipalFactory(
            UserManager<IdentityUser> userManager,
            RoleManager<IdentityRole> roleManager,
            IOptions<IdentityOptions> options)
            : base(
                  userManager,
                  roleManager,
                  options)
        {
        }

        [UnitOfWork]
        public async override Task<ClaimsPrincipal> CreateAsync(IdentityUser user)
        {
            var principal = await base.CreateAsync(user);
            var identity = principal.Identities.First();

            if (user.TenantId.HasValue)
            {
                identity.AddIfNotContains(new Claim(AbpClaimTypes.TenantId, user.TenantId.ToString()));
            }

            if (!user.Name.IsNullOrWhiteSpace())
            {
                identity.AddIfNotContains(new Claim(AbpClaimTypes.Name, user.Name));
            }

            if (!user.Surname.IsNullOrWhiteSpace())
            {
                identity.AddIfNotContains(new Claim(AbpClaimTypes.SurName, user.Surname));
            }

            if (!user.PhoneNumber.IsNullOrWhiteSpace())
            {
                identity.AddIfNotContains(new Claim(AbpClaimTypes.PhoneNumber, user.PhoneNumber));
            }

            identity.AddIfNotContains(new Claim(AbpClaimTypes.PhoneNumberVerified, user.PhoneNumberConfirmed.ToString()));

            if (!user.Email.IsNullOrWhiteSpace())
            {
                identity.AddIfNotContains(new Claim(AbpClaimTypes.Email, user.Email));
            }

            identity.AddIfNotContains(new Claim(AbpClaimTypes.EmailVerified, user.EmailConfirmed.ToString()));

            return principal;
        }
    }
}

## 解决方案

这里,我们看到了 AbpClaimTypes.Name 的名称,此名称应该为Name的Claim名称,我直接将其添加到了commonApiUserClaims列表中,发现命名空间下没有该属性,当时Apb版本为3.0.5,升级至3.2.1后,已经可以使用,o(╯□╰)o。之后再次调试,发现已经可以获得Name属性了。

 private async Task CreateApiResourcesAsync()
        {
            var commonApiUserClaims = new[]
            {
                //"email",
                //"email_verified",
                "name",
                //"phone_number",
                //"phone_number_verified",
                //"role",
                AbpClaimTypes.Name
            };

            await CreateApiResourceAsync("IdentityService", commonApiUserClaims);
            await CreateApiResourceAsync("InternalGateway", commonApiUserClaims);
            await CreateApiResourceAsync("WebAppGateway", commonApiUserClaims);
            //await CreateApiResourceAsync("TenantService", commonApiUserClaims);
            await CreateApiResourceAsync("IdeologyFrontService", commonApiUserClaims);
        }

标签: CurrentUser, Name为null, Abp vNext

仅有一条评论

  1. 路飞 路飞

    感谢分享

添加新评论