public async Task OnPostAsync()

in Source/WebApp-IdentityProvider-MFA/Areas/Identity/Pages/Account/Manage/EnableFIDO2.cshtml.cs [70:107]


        public async Task<IActionResult> OnPostAsync()
        {
            var user = await _userManager.GetUserAsync(User);
            if (user == null)
            {
                return NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
            }
            if (!ModelState.IsValid)
            {
                return Page();
            }

            bool result = await _fido2TwoFactorProvider.RegisterCredentialAsync(CredentialCreateOptions.FromJson(CredentialRegistrationOptions), JsonConvert.DeserializeObject<AuthenticatorAttestationRawResponse>(Input.AttestationRawResponse), user);
            
            if (!result)
            {
                ModelState.AddModelError(String.Empty,"L'ajout de la clé de sécurité a échoué.");
                return Page();
            }


            await _userManager.SetTwoFactorEnabledAsync(user, true);
            var userId = await _userManager.GetUserIdAsync(user);
            _logger.LogInformation("User with ID '{UserId}' has enabled 2FA with a security key.", userId);

            StatusMessage = "Votre clé de sécurité a été validée.";

            if (await _userManager.CountRecoveryCodesAsync(user) == 0)
            {
                var recoveryCodes = await _userManager.GenerateNewTwoFactorRecoveryCodesAsync(user, 10);
                RecoveryCodes = recoveryCodes.ToArray();
                return RedirectToPage("./ShowRecoveryCodes");
            }
            else
            {
                return RedirectToPage("./TwoFactorAuthentication");
            }
        }