public async Task <IActionResult> OnGetAsync(string msgJson) { MxReturnCode <IActionResult> rc = new MxReturnCode <IActionResult>("Index.OnGetAsync()", Page()); try { using (IAdminRepository repository = new AdminRepository(_conn)) { var resCnt = await repository.GetRoleCountAsync(); rc += resCnt; if (rc.IsSuccess()) { URDCount = String.Format("URD Count = {0}", resCnt.GetResult()); SetPageStatusMsg("Database access ok", ExistingMsg.Keep); rc.SetResult(Page()); } } } catch (Exception e) { rc.SetError(3130101, MxError.Source.Exception, e.Message, MxMsgs.MxErrUnknownException, true); } if (rc.IsError()) { _logger.LogError(rc.GetErrorTechMsg()); SetPageStatusMsg(rc.GetErrorUserMsgHtml(Startup.WebAppName, WebErrorHandling.GetMxRcReportToEmailBody()), ExistingMsg.Overwrite); } return(rc.GetResult()); }
public async Task <IActionResult> OnGetAsync() { MxReturnCode <IActionResult> rc = new MxReturnCode <IActionResult>("Index.OnGetAsync()", Page()); var userID = "[nobody logged-in]"; var msg = "unknown error"; try { var loggedInUser = await _userManager.GetUserAsync(User); userID = loggedInUser?.Id ?? "[nobody logged-in]"; msg = $"{loggedInUser?.UserName ?? "nobody"} is logged-in. "; if (loggedInUser?.EmailConfirmed == false) { msg += "Check your emails to complete your registration"; } using (IAdminRepo repo = new AdminRepo(_config?.GetConnectionString("DefaultConnection"))) { var resCnt = await repo.GetUrdCountAsync(); rc += resCnt; if (rc.IsError()) { DatabaseStatus = "Database access failed"; } else { DatabaseStatus = $"Database access ok, Role Count = {resCnt.GetResult()}"; rc.SetResult(Page()); } } } catch (Exception e) { rc.SetError(3040101, MxError.Source.Exception, e.Message, MxMsgs.MxErrUnknownException, true); } if (rc.IsError(true)) { SetPageStatusMsg(rc.GetErrorUserMsgHtml(userID), ExistingMsg.Overwrite); } else { SetPageStatusMsg(msg, ExistingMsg.Overwrite); } return(rc.GetResult()); }
public void OnGet(int?statusCode = null) { MxReturnCode <bool> rc = new MxReturnCode <bool>("Error.OnGet()"); if (statusCode.HasValue == false) { var feature = this.HttpContext.Features?.Get <IExceptionHandlerFeature>(); rc.SetError(3030101, MxError.Source.Exception, feature?.Error?.Message ?? "[not set]", MxMsgs.MxErrUnknownException, true); SetPageStatusMsg(rc.GetErrorUserMsgHtml(), ExistingMsg.Overwrite); } else { if (statusCode.Value == 404) //Microsoft.AspNetCore.Http.StatusCodes.Status404NotFound { SetPageStatusMsg("Error: Page not found. Correct the URL in your browser's address bar and try again", ExistingMsg.Overwrite); } else { SetPageStatusMsg($"Error: Invalid request; status={statusCode}. Correct the URL in your browser's address bar and try again", ExistingMsg.Overwrite); } rc.SetResult(true); } }
public void OnGet(int?statusCode = null) { MxReturnCode <bool> rc = new MxReturnCode <bool>("Error.OnGet()"); if (statusCode.HasValue == false) { var feature = this.HttpContext.Features?.Get <IExceptionHandlerFeature>(); rc.SetError(3140101, MxError.Source.Exception, feature?.Error?.Message ?? "[not set]", MxMsgs.MxErrUnknownException, true); //03-12-18 change to errorcde SetPageStatusMsg(rc.GetErrorUserMsgHtml(Startup.WebAppName, WebErrorHandling.GetMxRcReportToEmailBody()), ExistingMsg.Overwrite); _logger.LogError(rc.GetErrorTechMsg()); } else { if (statusCode.Value == 404) //Microsoft.AspNetCore.Http.StatusCodes.Status404NotFound { SetPageStatusMsg("Error: Page not found. Correct the URL in your browser's address bar and try again", ExistingMsg.Overwrite); } else { SetPageStatusMsg($"Error: Invalid request; status={statusCode}. Correct the URL in your browser's address bar and try again", ExistingMsg.Overwrite); } rc.SetResult(true); } }
public async Task <IActionResult> OnGetCallbackAsync(string returnUrl = null, string remoteError = null) { MxReturnCode <IActionResult> rc = new MxReturnCode <IActionResult>("Account.ExternalLogin.OnGetCallbackAsync()", RedirectToPage("./Login", new { ReturnUrl = returnUrl })); returnUrl = returnUrl ?? Url.Content("~/"); if ((returnUrl == null) || (remoteError != null)) { rc.SetError(3020101, MxError.Source.Service, $"Error from external provider: {remoteError}"); } else { try { var info = await _signInManager.GetExternalLoginInfoAsync(); if (info == null) { rc.SetError(3020102, MxError.Source.Service, "Error loading external login information."); } else { // Sign in the user with this external login provider if the user already has a login. var result = await _signInManager.ExternalLoginSignInAsync(info.LoginProvider, info.ProviderKey, isPersistent : false, bypassTwoFactor : false); if (result.Succeeded) { SetPageStatusMsg($"Welcome {info.Principal.Identity.Name} you have been authenticated by {info.LoginProvider}", ExistingMsg.Overwrite); rc.SetResult(LocalRedirect(returnUrl)); } else if (result.IsLockedOut) { rc.SetError(3020103, MxError.Source.Sys, "user account locked out", MxMsgs.MxErrAccountLockout); } else if (result.RequiresTwoFactor) { SetPageStatusMsg($"Welcome {info.Principal.Identity.Name} you have been authenticated by {info.LoginProvider}", ExistingMsg.Overwrite); rc.SetResult(LocalRedirect($"~/Identity/Account/LoginWith2fa?ReturnUrl={returnUrl ?? "%2f"}")); } else { // If the user does not have an account, then ask the user to create an account. ReturnUrl = returnUrl; LoginProvider = info.LoginProvider; if (info.Principal.HasClaim(c => c.Type == ClaimTypes.Email)) { Input = new InputModel { Email = info.Principal.FindFirstValue(ClaimTypes.Email) }; ProviderEmail = info.Principal.FindFirstValue(ClaimTypes.Email); } rc.SetResult(Page()); } } } catch (Exception e) { rc.SetError(3020104, MxError.Source.Exception, e.Message, MxMsgs.MxErrUnknownException, true); } } if (rc.IsError(true)) { SetPageStatusMsg(rc.GetErrorUserMsgHtml(), ExistingMsg.Overwrite); } return(rc.GetResult()); }
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()); }