private async Task UpdateRolesAsync(string userId, string[] roles, PasswordBoxUserManager manager = null) { if (manager == null) { manager = PasswordBoxUserManager.Create(); } var user = await manager.FindByIdAsync(userId); if (user == null) { throw new ArgumentNullException(nameof(user)); } var userRoles = await manager.GetRolesAsync(user); if (userRoles != null && userRoles.Count > 0) { await manager.RemoveFromRolesAsync(user, userRoles); } if (roles != null) { await manager.AddToRolesAsync(user, roles); } }
public async Task <User> Add(User entity, string password, string[] roles = null) { if (String.IsNullOrWhiteSpace(password)) { throw new BusinessException(MessageText.PleaseWritePassword); } using (PasswordBoxUserManager manager = PasswordBoxUserManager.Create()) { IdentityResult result = await manager.CreateAsync(entity, password); if (!result.Succeeded) { throw new BusinessException(result.Errors.Select(a => a.Description).ToList()); } if (roles != null) { await manager.AddToRolesAsync(entity, roles); } } return(entity); }