ABP Vnext 批量导入用户,密码问题处理

假设已有导入数据为List<UserImportInfo> importList,现在遍历该数据,转换为abp的用户实体(IdentityUser)代码如下:

var userList= new List<IdentityUser>();
importList.ForEach(async p =>
{
    var temp = new IdentityUser(GuidGenerator.Create(), p.UserName, p.Email);
    temp.SetPhoneNumber(p.PhoneNumber,false);
    temp.Name = p.Name;
    //设置密码
    await SetUserPwd(temp, SystemConstants.InitPassword);
    //添加部门信息
    temp.AddOrganizationUnit(p.OrgId!.Value);
    userList.Add(temp);
});
//批量插入数据
await _userRepository.InsertManyAsync(userList);

设置密码的方法为 SetUserPwd,具体代码如下:

private async Task SetUserPwd(IdentityUser user,string pwd)
{
    //生成密码
    string passwordHash = _passwordHasher.HashPassword(user, pwd);
    //密码设置到用户
    await (_userStore as IUserPasswordStore<IdentityUser>)!.SetPasswordHashAsync(user, passwordHash, CancellationToken.None).ConfigureAwait(continueOnCapturedContext: false);
    //生成SecurityStamp
    byte[] array = new byte[20];
    RandomNumberGenerator.Fill(array);
    //框架中 Base32 类为internal,自己代码中访问不到,所以复制一份来使用
    var NewSecurityStamp = Base32Helper.ToBase32(array); 
    //将SecurityStamp设置到用户
    await (_userStore as IUserSecurityStampStore<IdentityUser>)!.SetSecurityStampAsync(user, NewSecurityStamp, CancellationToken.None);
}

需要在构造函数注入的服务如下:

private readonly IRepository<IdentityUser> _userRepository;
private readonly IPasswordHasher<Volo.Abp.Identity.IdentityUser> _passwordHasher;
private readonly IUserStore<IdentityUser> _userStore;

最后,再附上Base32Helper类的代码(复制的abp框架中Base32代码)

public static class Base32Helper
{
	private const string _base32Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";

	public static string ToBase32(byte[] input)
	{
		if (input == null)
		{
			throw new ArgumentNullException("input");
		}

		StringBuilder stringBuilder = new StringBuilder();
		int offset = 0;
		while (offset < input.Length)
		{
			byte a;
			byte b;
			byte c;
			byte d;
			byte e;
			byte f;
			byte g;
			byte h;
			int nextGroup = GetNextGroup(input, ref offset, out a, out b, out c, out d, out e, out f, out g, out h);
			stringBuilder.Append((nextGroup >= 1) ? "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"[a] : '=');
			stringBuilder.Append((nextGroup >= 2) ? "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"[b] : '=');
			stringBuilder.Append((nextGroup >= 3) ? "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"[c] : '=');
			stringBuilder.Append((nextGroup >= 4) ? "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"[d] : '=');
			stringBuilder.Append((nextGroup >= 5) ? "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"[e] : '=');
			stringBuilder.Append((nextGroup >= 6) ? "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"[f] : '=');
			stringBuilder.Append((nextGroup >= 7) ? "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"[g] : '=');
			stringBuilder.Append((nextGroup >= 8) ? "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"[h] : '=');
		}

		return stringBuilder.ToString();
	}

	public static byte[] FromBase32(string input)
	{
		if (input == null)
		{
			throw new ArgumentNullException("input");
		}

		ReadOnlySpan<char> readOnlySpan = input.AsSpan().TrimEnd('=');
		if (readOnlySpan.Length == 0)
		{
			return Array.Empty<byte>();
		}

		byte[] array = new byte[readOnlySpan.Length * 5 / 8];
		int num = 0;
		int num2 = 0;
		int num3 = 0;
		int num4 = 0;
		while (num4 < array.Length)
		{
			int num5 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567".IndexOf(char.ToUpperInvariant(readOnlySpan[num2]));
			if (num5 < 0)
			{
				throw new FormatException();
			}

			int num6 = Math.Min(5 - num, 8 - num3);
			ref byte reference = ref array[num4];
			reference = (byte)(reference << num6);
			array[num4] |= (byte)(num5 >> 5 - (num + num6));
			num += num6;
			if (num >= 5)
			{
				num2++;
				num = 0;
			}

			num3 += num6;
			if (num3 >= 8)
			{
				num4++;
				num3 = 0;
			}
		}

		return array;
	}

	private static int GetNextGroup(byte[] input, ref int offset, out byte a, out byte b, out byte c, out byte d, out byte e, out byte f, out byte g, out byte h)
	{
		int result = (input.Length - offset) switch
		{
			1 => 2,
			2 => 4,
			3 => 5,
			4 => 7,
			_ => 8,
		};
		uint num = (uint)((offset < input.Length) ? input[offset++] : 0);
		uint num2 = (uint)((offset < input.Length) ? input[offset++] : 0);
		uint num3 = (uint)((offset < input.Length) ? input[offset++] : 0);
		uint num4 = (uint)((offset < input.Length) ? input[offset++] : 0);
		uint num5 = (uint)((offset < input.Length) ? input[offset++] : 0);
		a = (byte)(num >> 3);
		b = (byte)(((num & 7) << 2) | (num2 >> 6));
		c = (byte)((num2 >> 1) & 0x1Fu);
		d = (byte)(((num2 & 1) << 4) | (num3 >> 4));
		e = (byte)(((num3 & 0xF) << 1) | (num4 >> 7));
		f = (byte)((num4 >> 2) & 0x1Fu);
		g = (byte)(((num4 & 3) << 3) | (num5 >> 5));
		h = (byte)(num5 & 0x1Fu);
		return result;
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值