TwoFactorAuthenticatorSignInAsync always return failed

Solution for TwoFactorAuthenticatorSignInAsync always return failed
is Given Below:

I’m trying to setup Two Factor Authentication on my Blazor server app using Twillio. I’m able to send the code using

            var token = await _signInManager.UserManager.GenerateChangePhoneNumberTokenAsync(user, user.PhoneNumber);

        PhoneNumber = user.PhoneNumber;
        ReturnUrl = returnUrl;
        RememberMe = rememberMe;

        var message = "Your security code is: " + token;

But when I try to verify it using the code below, I always get result.failed. I have no idea why. The user I’m testing with has 2FA enabled and the phone number is valid.

        public async Task<IActionResult> OnPostAsync(bool rememberMe, string returnUrl = null)
    {
        if (!ModelState.IsValid)
        {
            return Page();
        }

        returnUrl = returnUrl ?? Url.Content("~/");

        var user = await _signInManager.GetTwoFactorAuthenticationUserAsync();
        if (user == null)
        {
            throw new InvalidOperationException($"Unable to load two-factor authentication user.");
        }

        var authenticatorCode = Input.TwoFactorCode.Replace(" ", string.Empty).Replace("-", string.Empty);

        var r = _signInManager.UserManager.GetValidTwoFactorProvidersAsync(user);
        var result = await _signInManager.TwoFactorAuthenticatorSignInAsync(authenticatorCode, rememberMe, Input.RememberMachine);

        if (result.Succeeded)
        {
            _logger.LogInformation("User with ID '{UserId}' logged in with 2fa.", user.Id);

            return LocalRedirect(returnUrl);
        }
        else if (result.IsLockedOut)
        {
            _logger.LogWarning("User with ID '{UserId}' account locked out.", user.Id);
            return RedirectToPage("./Lockout");
        }
        else
        {
            _logger.LogWarning("Invalid authenticator code entered for user with ID '{UserId}'.", user.Id);
            ModelState.AddModelError(string.Empty, "Invalid authenticator code.");
            return Page();
        }
    }