public async Task <IActionResult> OnPostAsync(string returnUrl = null) { returnUrl = returnUrl ?? Url.Content("~/Manage/"); if (ModelState.IsValid) { var user = new DealerEmployeeLoginCredential { UserName = Input.Email, Email = Input.Email }; var result = await _userManager.CreateAsync(user, Input.Password); if (result.Succeeded) { Dealer dealer = new Dealer { Name = Input.Name, Address = Input.Address, AddressLine2 = Input.AddressLine2, City = Input.City, State = Input.State, Zip = Input.Zip, PhoneNumber = Input.PhoneNumber, Website = Input.Website }; Dealer found = _outdoorPowerRepository.GetDealer(dealer); if (found == null) { _outdoorPowerRepository.AddDealer(dealer); if (!await _outdoorPowerRepository.SaveChangesAsync()) { ModelState.AddModelError("Save Failed", "Unable to save the dealer."); } else { found = dealer; } } DealerEmployee dealerEmployee = new DealerEmployee { FirstName = Input.FirstName, LastName = Input.LastName, DealerId = found.Id, DealerEmployeeLoginCredentialsId = new Guid(user.Id) }; _outdoorPowerRepository.AddDealerEmployee(dealerEmployee); if (!await _outdoorPowerRepository.SaveChangesAsync()) { ModelState.AddModelError("Save Failed", "Unable to save the user information."); } else { IEnumerable <Claim> claims = new List <Claim>( new Claim[] { new Claim("DealerId", found.Id.ToString()), new Claim("EditAccess", "DealerOwner") } ); var identityResult = await _userManager.AddClaimsAsync( user, claims ); } _logger.LogInformation("User created a new account with password."); var code = await _userManager.GenerateEmailConfirmationTokenAsync(user); var callbackUrl = Url.Page( "/Account/ConfirmEmail", pageHandler: null, values: new { userId = user.Id, code = code }, protocol: Request.Scheme); await _emailSender.SendEmailAsync(Input.Email, "Confirm your email", $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>."); await _signInManager.SignInAsync(user, isPersistent : false); return(LocalRedirect(returnUrl)); } else { bool duplicate = false; foreach (var error in result.Errors) { if (error.Code == "DuplicateUserName" || error.Code == "DuplicateEmail") { duplicate = true; } else { ModelState.AddModelError(string.Empty, error.Description); } } if (duplicate) { await _signInManager.SignInAsync(user, isPersistent : false); return(LocalRedirect(returnUrl)); } } } // If we got this far, something failed, redisplay form return(Page()); }