public async Task <IActionResult> Register(RegisterUserViewModel model) { if (ModelState.IsValid) { var user = await _userHelper.GetUserByEmailAsync(model.EmailAddress); if (user == null) { try { var country = await _countryRepository.GetByIdAsync(model.CountryId); user = _converterHelper.ToUser(model, country); var password = UtilityHelper.Generate(); var result = await _userHelper.AddUserAsync(user, password); if (!result.Succeeded) { ModelState.AddModelError(string.Empty, "The user couldn't be created."); return(View(model)); } await _userHelper.AddUSerToRoleAsync(user, user.SelectedRole); var myToken = await _userHelper.GeneratePasswordResetTokenAsync(user); var link = Url.Action( "ResetPassword", "Account", new { token = myToken, userId = user.Id }, protocol: HttpContext.Request.Scheme); _mailHelper.SendToNewUser(user.Email, link, user.Name); return(RedirectToAction(nameof(ListUsers))); } catch (Exception ex) { ModelState.AddModelError(string.Empty, ex.Message); } } else { ModelState.AddModelError(string.Empty, "This username is already registered."); } } model.Countries = _countryRepository.GetComboCountries(); model.Roles = _userHelper.GetComboRoles(); model.Genders = _clientRepository.GetComboGenders(); return(View(model)); }