public async Task <IActionResult> CreateWithRole(CreateAccountViewModel model) { if (ModelState.IsValid) { var user = await _userHelper.GetUserByEmailAsync(model.Username); if (user == null) { user = new User { Email = model.Username, UserName = model.Username, }; var result = await _userHelper.AddUserAsync(user, model.Password); if (result != IdentityResult.Success) { this.ModelState.AddModelError(string.Empty, "The user could not be created"); return(View(model)); } if (model.Role == "Client") { await _userHelper.AddUserToRoleAsync(user, "Client"); var client = _converterHelper.ToClientCreate(model); client.User = user; client.ImageUrl = $"~/images/Placeholder/placeholderUser.png"; await _clientRepository.CreateAsync(client); } else if (model.Role == "Secretary") { await _userHelper.AddUserToRoleAsync(user, "Secretary"); var secret = _converterHelper.ToSecretaryCreate(model); secret.ImageUrl = $"~/images/Placeholder/placeholderUser.png"; secret.User = user; await _secretaryRepository.CreateAsync(secret); } else if (model.Role == "Admin") { await _userHelper.AddUserToRoleAsync(user, "Admin"); var admin = _converterHelper.ToAdminCreate(model); admin.ImageUrl = $"~/images/Placeholder/placeholderUser.png"; admin.User = user; await _adminRepository.CreateAsync(admin); } else if (model.Role == "Mechanic") { await _userHelper.AddUserToRoleAsync(user, "Mechanic"); var mecha = _converterHelper.ToMechanicCreate(model); mecha.SpecialtyId = model.SpecialtyId; mecha.ImageUrl = $"~/images/Placeholder/placeholderUser.png"; mecha.User = user; await _mechanicRepository.CreateAsync(mecha); } var myToken = await _userHelper.GenerateEmailConfirmationTokenAsync(user); var tokenLink = Url.Action("ConfirmEmail", "Account", new { userid = user.Id, token = myToken, }, protocol: HttpContext.Request.Scheme); _mailHelper.SendMail(model.Username, "Email confirmation", $"<h1>Verify your email to finish signing up for AutoWorkshop.</h1>" + $"<br><br>Please confirm your email by using this link:</br></br><a href = \"{tokenLink}\">Confirm Email</a>"); this.ViewBag.Message = "Account Creation Sucessfull"; return(View(model)); } ModelState.AddModelError(string.Empty, "The username is already registered"); return(View(model)); } return(View(model)); }