public async Task <IActionResult> LoginWith2fa(LoginWith2faModel model, bool rememberMe, string returnUrl = null) { if (!ModelState.IsValid) { return(View(model)); } var user = await _signInManager.GetTwoFactorAuthenticationUserAsync(); if (user == null) { throw new ApplicationException($"Unable to load user with ID '{_userManager.GetUserId(User)}'."); } var authenticatorCode = model.TwoFactorCode.Replace(" ", string.Empty).Replace("-", string.Empty); var result = await _signInManager.TwoFactorAuthenticatorSignInAsync(authenticatorCode, rememberMe, model.RememberMachine); if (result.Succeeded) { _logger.LogInformation("User with ID {UserId} logged in with 2fa.", user.Id); return(RedirectToLocal(returnUrl)); } else if (result.IsLockedOut) { _logger.LogWarning("User with ID {UserId} account locked out.", user.Id); return(RedirectToAction(nameof(Lockout))); } else { _logger.LogWarning("Invalid authenticator code entered for user with ID {UserId}.", user.Id); ModelState.AddModelError(string.Empty, "Invalid authenticator code."); return(View()); } }
public async Task <IActionResult> LoginWith2fa(LoginWith2faModel model) { if (!ModelState.IsValid) { return(View(model)); } var user = await _signInManager.GetTwoFactorAuthenticationUserAsync(); if (user == null) { return(TwoFactorFail()); } var authenticatorCode = model.TwoFactorCode.Replace(" ", string.Empty).Replace("-", string.Empty); var result = await _signInManager.TwoFactorAuthenticatorSignInAsync(authenticatorCode, isPersistent : false, rememberClient : false); if (result.Succeeded) { return(Redirect("/")); } else if (result.IsLockedOut) { return(RedirectToAction(nameof(Lockout))); } else { ModelState.AddModelError(string.Empty, "Invalid authenticator code."); return(View()); } }
public async Task <IActionResult> OnPostAsync(LoginWith2faModel loginParameters) { var result = false; if (ModelState.IsValid) { var user = await _signInManager.GetTwoFactorAuthenticationUserAsync(); if (user != null) { await _signInManager.RefreshSignInAsync(user); if (loginParameters.RememberMachine) { await _signInManager.RememberTwoFactorClientAsync(user); } result = true; } } if (!result) { loginParameters.ReturnUrl = $"{Settings.LoginWith2faPath}/{loginParameters.ReturnUrl ?? string.Empty}"; } return(LocalRedirect(Url.Content($"~{loginParameters.ReturnUrl}"))); }
public async Task <IActionResult> LoginWith2fa(bool rememberMe, string returnUrl = null) { var model = new LoginWith2faModel(); var user = await _signInManager.GetTwoFactorAuthenticationUserAsync(); if (user == null) { throw new InvalidOperationException($"Unable to load two-factor authentication user."); } model.ReturnUrl = returnUrl; model.RememberMe = rememberMe; return(View(model)); }
public async Task <IActionResult> LoginWith2fa(bool rememberMe, string returnUrl = null) { // Ensure the user has gone through the username & password screen first var user = await _signInManager.GetTwoFactorAuthenticationUserAsync(); if (user == null) { throw new ApplicationException($"Unable to load two-factor authentication user."); } var model = new LoginWith2faModel { RememberMe = rememberMe }; ViewData["ReturnUrl"] = returnUrl; return(View(model)); }
public async Task <IActionResult> LoginWith2fa(LoginWith2faModel twofaModel, bool rememberMe, string returnUrl = null) { if (!ModelState.IsValid) { return(View(twofaModel)); } var user = await _signInManager.GetTwoFactorAuthenticationUserAsync(); if (user == null) { throw new ApplicationException($"Unable to load user with ID '{_userManager.GetUserId(User)}'."); } var result = await _signInManager.TwoFactorSignInAsync("Email", twofaModel.TwoFactorCode, twofaModel.RememberMe, rememberClient : false);; if (result.Succeeded) { if (User.IsInRole("Admin")) { return(RedirectToAction("ListUsers", "Administration")); } return(RedirectToAction(nameof(ManageController.Index), "Manage")); } else if (result.IsLockedOut) { ModelState.AddModelError("", "The account is locked out"); return(View()); } else { ModelState.AddModelError("", "Invalid Login Attempt"); return(View()); } }