abp vnext將用戶名添加到token中


1.最無腦,不推薦使用。就是重寫此類,然后注入context.Services.AddTransient<IProfileService, ProfileService>();  你需要什么要加什么

public class ProfileService: IProfileService
    {
        private readonly UserManager<IdentityUser> _userManager; 
        public ProfileService(UserManager<IdentityUser> userManager)
        {
            _userManager = userManager;
        }

        public async Task GetProfileDataAsync(ProfileDataRequestContext context)
        {
            var user = await _userManager.GetUserAsync(context.Subject);

            var claims = new List<Claim>
            {
                new Claim("FullName", user.Name),
            };

            context.IssuedClaims.AddRange(claims);
        }
}

  

2.如果只要用戶的name信息的話跟一些user上有的信息,可以修改seeddata種子數據(DoMain層的seed類,如果微服務在authService) ,注釋的為添加的,其他的本來就有了。

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

            await CreateApiResourceAsync("BookStore", commonApiUserClaims);
        }

  

然后需要全局配置一個 AbpClaimTypes.Name = "given_name",或者使用這種方法會優雅一點

 Configure<AbpClaimsMapOptions>(options =>
            {
                options.Maps.Add("given_name", () => AbpClaimTypes.Name);
                options.Maps.Add("name", () => AbpClaimTypes.UserName);
            });

  

3.https://www.jianshu.com/p/a22ed53bfbf4 可以查看這位博主的方式。

接下來就可以去獲取token了 然后可以使用jwt工具直接查看,如果沒顯示可能你的用戶名為null。
我去稍微看了一下源碼,本質上還是從ClaimsPrincipal上使用的,所以abp.identityservice中的繼承了IUserClaimsPrincipalFactory。
AbpUserClaimsFactory<TUser> : IUserClaimsPrincipalFactory<TUser>。

好了目前就是這樣。歡迎評論跟交流。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM