public async Task <IActionResult> LoginWithRecoveryCode(LoginWithRecoveryCodeModel model) { if (!ModelState.IsValid) { return(View(model)); } var user = await _signInManager.GetTwoFactorAuthenticationUserAsync(); if (user == null) { return(TwoFactorFail()); } var recoveryCode = model.RecoveryCode.Replace(" ", string.Empty); var result = await _signInManager.TwoFactorRecoveryCodeSignInAsync(recoveryCode); if (result.Succeeded) { return(Redirect("/")); } if (result.IsLockedOut) { return(RedirectToAction(nameof(Lockout))); } else { ModelState.AddModelError(string.Empty, "Invalid recovery code entered."); return(View()); } }
public async Task <IActionResult> LoginWithRecoveryCode(LoginWithRecoveryCodeModel model, string returnUrl = null) { if (!ModelState.IsValid) { return(View(model)); } var user = await _signInManager.GetTwoFactorAuthenticationUserAsync(); if (user == null) { throw new ApplicationException($"Unable to load two-factor authentication user."); } var recoveryCode = model.RecoveryCode.Replace(" ", string.Empty); var result = await _signInManager.TwoFactorRecoveryCodeSignInAsync(recoveryCode); if (result.Succeeded) { _logger.LogInformation("User with ID {UserId} logged in with a recovery code.", user.Id); return(RedirectToLocal(returnUrl)); } if (result.IsLockedOut) { _logger.LogWarning("User with ID {UserId} account locked out.", user.Id); return(RedirectToAction(nameof(Lockout))); } else { _logger.LogWarning("Invalid recovery code entered for user with ID {UserId}", user.Id); ModelState.AddModelError(string.Empty, "Invalid recovery code entered."); return(View()); } }
public async Task <IActionResult> LoginWithRecoveryCode(LoginWithRecoveryCodeModel model) { if (!ModelState.IsValid) { return(View(model)); } var userId = await _signInManager.GetTwoFactorAuthenticationUserAsync(); if (userId == null) { throw new InvalidOperationException($"Unable to load two-factor authentication user."); } var result = await _signInManager.TwoFactorRecoveryCodeSignInAsync(model.RecoveryCode); if (result.Succeeded) { _logger.LogInformation("User with ID '{UserId}' logged in with a recovery code.", userId); return(LocalRedirect(model.ReturnUrl ?? Url.Content("~/"))); } _logger.LogWarning("Invalid recovery code entered for user with ID '{UserId}' ", userId); ModelState.AddModelError(string.Empty, "Invalid recovery code entered."); return(View(model)); }
public async Task <ActionResult> RecoveryCode(LoginWithRecoveryCodeModel model) { if (!ModelState.IsValid) { return(View(model)); } var user = await _signInManager.GetTwoFactorAuthenticationUserAsync(); if (user == null) { throw new InvalidOperationException($"两步认证用户读取异常。"); } var recoveryCode = model.RecoveryCode.Replace(" ", string.Empty); var result = await _signInManager.TwoFactorRecoveryCodeSignInAsync(recoveryCode); if (result.Succeeded) { return(RedirectToLocal(model.ReturnUrl)); } if (result.IsLockedOut) { return(RedirectToAction("Lockout")); } else { ModelState.AddModelError(string.Empty, "无效的应急密码"); return(View(model)); } }
public async Task <IActionResult> LoginWithRecoveryCode(string returnUrl = null) { var model = new LoginWithRecoveryCodeModel(); var user = await _signInManager.GetTwoFactorAuthenticationUserAsync(); if (user == null) { throw new InvalidOperationException($"Unable to load two-factor authentication user."); } model.ReturnUrl = returnUrl; return(View(model)); }
public async Task <IActionResult> LoginWithRecoveryCode(string returnUrl = null) { // Ensure the user has gone through the username & password screen first var user = await _signInManager.GetTwoFactorAuthenticationUserAsync(); if (user == null) { throw new InvalidOperationException($"Unable to load two-factor authentication user."); } ReturnUrl = returnUrl; var model = new LoginWithRecoveryCodeModel(); return(View(model)); }
public async Task <ActionResult> RecoveryCode(LoginWithRecoveryCodeModel model) { if (!ModelState.IsValid) { return(Json(new { success = false, errors = ModelState.Values.SelectMany(m => m.Errors).Select(e => e.ErrorMessage).ToList() })); } var user = await _signInManager.GetTwoFactorAuthenticationUserAsync(); if (user == null) { throw new InvalidOperationException($"两步验证用户读取异常。"); } var recoveryCode = model.RecoveryCode.Replace(" ", string.Empty); var result = await _signInManager.TwoFactorRecoveryCodeSignInAsync(recoveryCode); if (result.Succeeded) { return(Json(new { success = true })); } if (result.IsLockedOut) { return(Json(new { success = false, errors = new[] { "此账户由于登陆尝试次数过多已被暂时锁定,请稍后再试。" }, })); } else { return(Json(new { success = false, errors = new[] { "无效的应急密码" }, })); } }