abp vnext默认是用邮箱是必填项,但是有时候邮箱不一定是必填项目,所以为了解决这个问题,对新增用户的时候进行字段的重写。当然用户编辑的时候,也要修改对接的接口。这里我出入参数据不变,修改了字段的校验。
代码粘贴这里,因为用户信息这里需要扩展用户头像、微信等字段。扩展字段也在里面体现了。不需要该字段的,删除即可。
用户的创建修改,需要相应的权限,这里的权限也是abp字段的权限,如果不想用abp自带的权限,删除即可。
例如:[Authorize(IdentityPermissions.Users.Create)]
/// <summary>
/// 创建用户
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[Authorize(IdentityPermissions.Users.Create)]
public async Task<IdentityUserDto> CreateUsersAsync(UserInfoCreateDto input)
{
var userEntity = await _userManager.FindByNameAsync(input.UserName);
if (userEntity != null) throw new Exception("用户名已被创建,请修改后重试");
await _identityOptions.SetAsync();
var entity = new UsersInfoDto()
{
Email = input.Email,
Name = input.Name,
PasswordHash = input.Password,
PhoneNumber = input.PhoneNumber,
UserName = input.UserName,
EmpNo = (string)(input.ExtraProperties.FirstOrDefault(x => x.Key == "EmpNo").Value??"".ToString()),
WeChat = (string)(input.ExtraProperties.FirstOrDefault(x => x.Key == "WeChat").Value ?? "".ToString()),
Avatar = (string)(input.ExtraProperties.FirstOrDefault(x => x.Key == "Avatar").Value ?? "".ToString())
};
entity.SetProperty("EmpNo", entity.EmpNo);
entity.SetProperty("WeChat", entity.WeChat);
entity.SetProperty("Avatar", entity.Avatar);
var user = ObjectMapper.Map<UsersInfoDto, IdentityUser>(entity);
entity.PasswordHash = _passwordHasher.HashPassword(user, entity.PasswordHash);
var userPasswordHash = ObjectMapper.Map<UsersInfoDto, IdentityUser>(entity);
await _identityUserRepository.InsertAsync(userPasswordHash);
if (input.RoleNames.Length > 0)
{
await _userManager.SetRolesAsync(user, input.RoleNames);
}
await CurrentUnitOfWork.SaveChangesAsync();
return ObjectMapper.Map<IdentityUser, IdentityUserDto>(userPasswordHash);
}
/// <summary>
/// 检查用户名是否存在
/// </summary>
/// <param name="username">用户名</param>
/// <returns></returns>
public async Task<bool> CheckUserNameIsExist(string username)
{
var user = await _userManager.FindByNameAsync(username);
if (user == null) return false;
return true;
}
/// <summary>
/// 密码重置
/// </summary>
/// <param name="id"></param>
/// <param name="input"></param>
/// <returns></returns>
[Authorize(IdentityPermissions.Users.Update)]
public async Task<string> PasswordReset(Guid id, UserInfoUpdateDto input)
{
var user = await _userManager.FindByIdAsync(id.ToString());
if (user == null) throw new Exception("该用户不存在");
if (input.Password.IsNullOrEmpty())
{
input.Password = "1q2w3e*";
}
await _userManager.RemovePasswordAsync(user);
await _userManager.AddPasswordAsync(user, input.Password);
return input.Password;
}
/// <summary>
/// 更新用户,用户名禁止编辑
/// </summary>
/// <param name="id"></param>
/// <param name="input"></param>
/// <returns></returns>
[Authorize(IdentityPermissions.Users.Update)]
public async Task<IdentityUserDto> UpdateUsersAsync(Guid id, UserInfoUpdateDto input)
{
var user = await _userManager.FindByIdAsync(id.ToString());
if (user == null) return default;
await _identityOptions.SetAsync();
await _userManager.SetEmailAsync(user, input.Email);
await _userManager.SetPhoneNumberAsync(user, input.PhoneNumber);
user.Name = input.Name;
user.Surname = input.Surname;
input.MapExtraPropertiesTo(user);
user.SetProperty("EmpNo", input.ExtraProperties.FirstOrDefault(x => x.Key == "EmpNo").Value??"".ToString());
user.SetProperty("WeChat", input.ExtraProperties.FirstOrDefault(x => x.Key == "WeChat").Value??"".ToString());
user.SetProperty("Avatar", input.ExtraProperties.FirstOrDefault(x => x.Key == "Avatar").Value??"".ToString());
if (!input.Password.IsNullOrEmpty())
{
await _userManager.RemovePasswordAsync(user);
await _userManager.AddPasswordAsync(user, input.Password);
}
if (input.RoleNames.Length > 0)
{
await _userManager.SetRolesAsync(user, input.RoleNames);
}
await _userManager.UpdateAsync(user);
await CurrentUnitOfWork.SaveChangesAsync();
return ObjectMapper.Map<IdentityUser, IdentityUserDto>(user);
}
相关的构造函数如下所示:
private readonly IdentityUserManager _userManager;
private readonly IOptions<IdentityOptions> _identityOptions;
private readonly IConfiguration _configuration;
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly IIdentityUserRepository _identityUserRepository;
private readonly IPasswordHasher<IdentityUser> _passwordHasher;
最重要的一点是,using引用命名空间的时候,优先选择 Volo.Abp.xxx的命名,不要选择using Microsoft.AspNetCore.Identity....微软自带的,因为abp默认继承了Microsoft,会造成代码冲突。