因為ABP Vnext在密碼加密方面使用的鹽加密的方式,底層的加密方式讓人摸不著頭腦。如何需要批量導入用戶的時候,這個密碼問題就很頭疼。
假設,已經有一個集合List<entity>的用戶數據了,此時進行循環取出一條用戶信息,進行 abpUser實體的轉換。代碼如下
//判斷密碼字段是否為空if (string.IsNullOrEmpty(entity.PasswordHash)){entity.Remark = "密碼不能為空";entityRepeatList.Add(entity);continue;}//這是擴展字段的信息,如果沒有可以刪除entity.SetProperty("EmpNo", entity.EmpNo);entity.SetProperty("WeChat", entity.WeChat);await _identityOptions.SetAsync();//UsersInfoDto?自己模仿著 IdentityUser 寫一個var user = ObjectMapper.Map<UsersInfoDto, Volo.Abp.Identity.IdentityUser>(entity);entity.PasswordHash = _passwordHasher.HashPassword(user, entity.PasswordHash);var?userPasswordHash?=?ObjectMapper.Map<UsersInfoDto,?Volo.Abp.Identity.IdentityUser>(entity);await _identityUserRepository.InsertAsync(userPasswordHash);//此時插入數據,并不立即生效,沒有觸發savachange(),本接口遍歷完畢,才觸發保存,所以不用擔心每次重復打開關閉連接池的問題
用到的構造函數如下:
private?readonly?IOptions<IdentityOptions>?_identityOptions;private?readonly?IPasswordHasher<Volo.Abp.Identity.IdentityUser>?_passwordHasher;
最重要的一點是,using引用命名空間的時候,優先選擇?Volo.Abp.xxx的命名,不要選擇using Microsoft.AspNetCore.Identity....微軟自帶的,因為abp默認繼承了Microsoft,會造成代碼沖突。