public async Task <IActionResult> OnPostConfirmationAsync(string returnUrl = null) { returnUrl = returnUrl ?? Url.Content("~/"); MxReturnCode <IActionResult> rc = new MxReturnCode <IActionResult>("Account.ExternalLogin.OnPostConfirmationAsync()", RedirectToPage("./Login", new { ReturnUrl = returnUrl })); try { // Get the information about the user from the external login provider var info = await _signInManager.GetExternalLoginInfoAsync(); if (info == null) { rc.SetError(3090201, MxError.Source.Sys, "Error loading external login information during confirmation."); } else { if (ModelState.IsValid == false) { rc.SetError(3090202, MxError.Source.Data, WebErrorHandling.GetModelStateErrors(ModelState, WebErrorHandling.FormValidationErrorPreamble)); } else { var providerEmail = ProviderEmail; if (providerEmail != Input.Email) { rc.SetError(3090203, MxError.Source.Sys, $"{providerEmail} from provider != {Input.Email} from form", MxMsgs.MxErrUnexpected); } else { IdentityUser user = null; if (await _userManager.FindByEmailAsync(providerEmail) == null) { user = new IdentityUser { UserName = providerEmail, Email = providerEmail, EmailConfirmed = true }; var result = await _userManager.CreateAsync(user); if (result.Succeeded == false) { rc.SetError(3090204, MxError.Source.Sys, WebErrorHandling.GetIdentityErrors(result, $"cannot create user account for {providerEmail}")); } } if (rc.GetErrorCode() != 3090204) { if ((user = await _userManager.FindByEmailAsync(providerEmail)) == null) { rc.SetError(3090205, MxError.Source.Sys, $"Unable to load user {providerEmail}", MxMsgs.MxErrUnexpected, true); } else { var result = await _userManager.AddLoginAsync(user, info); if (result.Succeeded == false) { rc.SetError(3090206, MxError.Source.Sys, WebErrorHandling.GetIdentityErrors(result, $"cannot add {info.LoginProvider} login for {providerEmail}")); } else { await _signInManager.SignInAsync(user, isPersistent : false); SetPageStatusMsg($"Welcome {info.Principal.Identity.Name} you have been authenticated by {info.LoginProvider}", ExistingMsg.Overwrite); rc.SetResult(LocalRedirect(returnUrl)); } } } } } } } catch (Exception e) { rc.SetError(3090207, MxError.Source.Exception, e.Message, MxMsgs.MxErrUnknownException, true); } if (rc.IsError(true)) { SetPageStatusMsg(rc.GetErrorUserMsgHtml(), ExistingMsg.Overwrite); } return(rc.GetResult()); }
public async Task <IActionResult> OnPostAsync(string returnUrl = null) { MxReturnCode <IActionResult> rc = new MxReturnCode <IActionResult>("Account.Manage.Register.OnPostAsync()", Page()); string userId = null; returnUrl = returnUrl ?? Url.Content("~/"); if (!ModelState.IsValid) { rc.SetError(3010101, MxError.Source.User, WebErrorHandling.GetModelStateErrors(ModelState, WebErrorHandling.FormValidationErrorPreamble)); } else { try { if (await ValidateForm() == false) { rc.SetError(3010102, MxError.Source.User, WebErrorHandling.GetModelStateErrors(ModelState, WebErrorHandling.FormValidationErrorPreamble)); } else { var user = new IdentityUser { UserName = Input.Email, Email = Input.Email }; var result = await _userManager.CreateAsync(user, Input.Password); if (result.Succeeded == false) { rc.SetError(3010103, MxError.Source.Sys, WebErrorHandling.GetIdentityErrors(result, $"cannot register user {Input.Email}")); } else { 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>."); userId = user?.Id; await _signInManager.SignInAsync(user, isPersistent : false); rc.SetResult(LocalRedirect(returnUrl)); } } } catch (Exception e) { rc.SetError(3010104, MxError.Source.Exception, e.Message, MxMsgs.MxErrUnknownException, true); } } if (rc.IsError(true)) { SetPageStatusMsg(rc.GetErrorUserMsgHtml(userId), ExistingMsg.Overwrite); } return(rc.GetResult()); }