public async Task <bool> AddNewUser(UserInputViewModel userInputVm) { var user = new User { UserName = userInputVm.Username, Email = userInputVm.Email }; var randomPassword = RandomString.GenerateRandomString(); var result = await _userService.CreateAsync(user, randomPassword); if (!result.Succeeded) { return(false); } await _userService.AddUserToRolesAsync(user, userInputVm.Roles); var context = _httpContextAccessor.HttpContext; var code = await _userService.GenerateEmailConfirmationTokenAsync(user); var callbackUrl = _urlHelperExtension.EmailConfirmationLink(user.Id, code, context.Request.Scheme); var emailOptions = new EmailOptions { Url = callbackUrl, Password = randomPassword, UserName = userInputVm.Username }; await _emailSender.SendEmailAsync(userInputVm.Email, "", emailOptions, EmailType.AccountConfirm); return(true); }
public async Task <bool> AddNewUser(UserInputViewModel userInputVm) { var user = new User { UserName = userInputVm.Username, Email = userInputVm.Email }; var randomPassword = RandomString.GenerateRandomString(AppEnum.MinPasswordChar); var result = await _userService.CreateAsync(user, randomPassword); if (!result.Succeeded) { return(false); } using (var transaction = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { try { await _userService.AddUserToRolesAsync(user, userInputVm.Roles); var context = _httpContextAccessor.HttpContext; var code = await _userService.GenerateEmailConfirmationTokenAsync(user); var callbackUrl = _urlHelperExtension.EmailConfirmationLink(user.Id, code, context.Request.Scheme); var emailOptions = new EmailOptions { Url = callbackUrl, Password = randomPassword, UserName = userInputVm.Username }; await _emailSender.SendEmailAsync(userInputVm.Email, "", emailOptions, EmailType.AccountConfirm); transaction.Complete(); } catch (Exception) { _unitOfWork.Rollback(); } } return(true); }