public async Task <IActionResult> LoginWith2fa(LoginWithTwoFactorViewModel model, bool rememberMe, string returnUrl = null) { if (!ModelState.IsValid) { return(View(model)); } ApplicationUser user = await _signInManager.GetTwoFactorAuthenticationUserAsync(); if (user == null) { throw new CustomApplicationException($"Unable to load user with ID '{_userManager.GetUserId(User)}'."); } string authenticatorCode = model.TwoFactorCode.Replace(" ", string.Empty).Replace("-", string.Empty); Microsoft.AspNetCore.Identity.SignInResult 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(bool rememberMe, string returnUrl = null) { // Ensure the user has gone through the username & password screen first ApplicationUser user = await _signInManager.GetTwoFactorAuthenticationUserAsync(); if (user == null) { throw new CustomApplicationException($"Unable to load two-factor authentication user."); } LoginWithTwoFactorViewModel model = new LoginWithTwoFactorViewModel { RememberMe = rememberMe }; ViewData["ReturnUrl"] = returnUrl; return(View(model)); }