public async Task <IActionResult> OnPostAsync() { var userId = _currentUserService.UserId; if (String.IsNullOrEmpty(userId)) { return(NotFound($"Unable to load user.")); } if (!ModelState.IsValid) { await LoadSharedKeyAndQrCodeUriAsync(userId); return(Page()); } // Strip spaces and hypens var verificationCode = Input.Code.Replace(" ", string.Empty).Replace("-", string.Empty); var is2faTokenValid = await _authenticatorService.VerifyAuthenticatorCodeAsync( userId, verificationCode); if (!is2faTokenValid) { ModelState.AddModelError("Input.Code", "Verification code is invalid."); await LoadSharedKeyAndQrCodeUriAsync(userId); return(Page()); } await _authenticatorService.SetAuthenticatorEnabledAsync(userId, true); _logger.LogInformation("User with ID '{UserId}' has enabled 2FA with an authenticator app.", userId); StatusMessage = "Your authenticator app has been verified."; return(RedirectToPage()); }