public async Task <IActionResult> OnPostRegister() { foreach (var item in ModelState) { if (!item.Key.Contains("RegisterModel")) { item.Value.ValidationState = Microsoft.AspNetCore.Mvc.ModelBinding.ModelValidationState.Valid; } } if (ModelState.IsValid) { var user = new ApplicationUser { UserName = RegisterModel.Email, Email = RegisterModel.Email, FirstName = RegisterModel.FirstName, LastName = RegisterModel.LastName, PhoneNumber = RegisterModel.PhoneNumber, Gender = RegisterModel.Gender }; var result = await _userManager.CreateAsync(user, RegisterModel.Password); if (result.Succeeded) { string anonymousBasketId = Request.Cookies[Constants.BASKET_COOKIENAME]; if (!string.IsNullOrEmpty(anonymousBasketId)) { await _basketService.TransferBasketAsync(anonymousBasketId, RegisterModel.Email, false); Response.Cookies.Delete(Constants.BASKET_COOKIENAME); } await SendConfirmationEmailAsync(user); //Check Subscriber if (RegisterModel.SubscribeNewsletter) { await _mailChimpService.AddSubscriberAsync(RegisterModel.Email); await _emailSender.SendGenericEmailAsync(_settings.FromInfoEmail, _settings.CCEmails, "Subscrição da newsletter feita na loja", $"O utilizador {RegisterModel.FirstName} {RegisterModel.LastName} registou-se na loja e subscreveu-se na newsletter com o email: {RegisterModel.Email}"); } else { await _emailSender.SendGenericEmailAsync(_settings.FromInfoEmail, _settings.CCEmails, "Novo registo na loja", $"O utilizador {RegisterModel.FirstName} {RegisterModel.LastName} registou-se na loja com o email: {RegisterModel.Email}"); } await _signInManager.SignInAsync(user, isPersistent : false); return(RedirectToPage("CheckoutStep2")); } AddErrors(result); } //Something went wrong await SetBasketModelAsync(); ViewData["RegisterFailed"] = true; return(Page()); }
public async Task <IActionResult> OnPostRegister(string returnUrl = "/Index") { ViewData["ReturnUrl"] = returnUrl; _logger.LogInformation($"OnPostRegister return url: {returnUrl}"); foreach (var item in ModelState) { if (!item.Key.Contains("UserDetails")) { item.Value.ValidationState = Microsoft.AspNetCore.Mvc.ModelBinding.ModelValidationState.Valid; } } if (ModelState.IsValid) { var user = new ApplicationUser { UserName = UserDetails.Email, Email = UserDetails.Email, FirstName = UserDetails.FirstName, LastName = UserDetails.LastName, PhoneNumber = UserDetails.PhoneNumber, Gender = UserDetails.Gender }; var result = await _userManager.CreateAsync(user, UserDetails.Password); if (result.Succeeded) { string anonymousBasketId = Request.Cookies[Constants.BASKET_COOKIENAME]; if (!String.IsNullOrEmpty(anonymousBasketId)) { await _basketService.TransferBasketAsync(anonymousBasketId, UserDetails.Email, false); Response.Cookies.Delete(Constants.BASKET_COOKIENAME); } await SendConfirmationEmailAsync(user); //Check Subscriber if (UserDetails.SubscribeNewsletter) { await _mailChimpService.AddSubscriberAsync(UserDetails.Email); await _emailSender.SendGenericEmailAsync(_settings.FromInfoEmail, _settings.CCEmails, "Subscrição da newsletter feita na loja", $"O utilizador {UserDetails.FirstName} {UserDetails.LastName} registou-se na loja e subscreveu-se na newsletter com o email: {UserDetails.Email}"); } else { await _emailSender.SendGenericEmailAsync(_settings.FromInfoEmail, _settings.CCEmails, "Novo registo na loja", $"O utilizador {UserDetails.FirstName} {UserDetails.LastName} registou-se na loja com o email: {UserDetails.Email}"); } await _signInManager.SignInAsync(user, isPersistent : false); return(RedirectToPage(returnUrl)); } AddErrors(result); } ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList(); return(Page()); }
public async Task <IActionResult> OnPostSubscribeNewsletterAsync(string newsletterEmail) { if (Utils.IsValidEmail(newsletterEmail)) { await _mailChimpService.AddSubscriberAsync(newsletterEmail); StatusMessage = "A subscrição foi efetuada com sucesso!"; } else { StatusMessage = "O email não é valido, por favor tente de novo"; } return(RedirectToPage()); }
public async Task <IActionResult> OnPostConfirmationAsync(string returnUrl = null) { returnUrl = returnUrl ?? Url.Content("~/"); returnUrl = Utils.FixBasePath(returnUrl); // Get the information about the user from the external login provider var info = await _signInManager.GetExternalLoginInfoAsync(); if (info == null) { ErrorMessage = "Erro ao carregar as informações do login externo durante a confirmação"; return(RedirectToPage("./Signin", new { ReturnUrl = returnUrl })); } if (ModelState.IsValid) { //Check if exists var user = await _userManager.FindByEmailAsync(Input.Email); IdentityResult result = null; if (user == null) { user = new ApplicationUser { UserName = Input.Email, Email = Input.Email, FirstName = Input.FirstName, LastName = Input.LastName }; result = await _userManager.CreateAsync(user); } if (result == null || (result != null && result.Succeeded)) { result = await _userManager.AddLoginAsync(user, info); if (result.Succeeded) { await _signInManager.SignInAsync(user, isPersistent : false); _logger.LogInformation("User created an account using {Name} provider.", info.LoginProvider); await TransferBasket(Input.Email); //Check Subscriber if (Input.SubscribeNewsletter) { await _mailChimpService.AddSubscriberAsync(Input.Email); await _emailSender.SendGenericEmailAsync(_settings.FromInfoEmail, _settings.CCEmails, "Subscrição da newsletter feita na loja", $"O utilizador {Input.FirstName} {Input.LastName} registou-se na loja e subscreveu-se na newsletter com o email: {Input.Email}"); } else { await _emailSender.SendGenericEmailAsync(_settings.FromInfoEmail, _settings.CCEmails, $"Novo registo feito na loja via {info.ProviderDisplayName}", $"O utilizador {Input.FirstName} {Input.LastName} registou-se na loja com o email: {Input.Email}"); } return(RedirectToPage(returnUrl)); } } foreach (var error in result.Errors) { ModelState.AddModelError(string.Empty, error.Description); } } LoginProvider = info.LoginProvider; ReturnUrl = returnUrl; return(Page()); }