TwoFactorRecoveryCodeSignInAsync 在 Asp.Net Identity 框架中总是失败

问题描述 投票:0回答:1

TwoFactorRecoveryCodeSignInAsync 始终失败,即使

[dbo].[AspNetUserTokens]
中存在记录。

Table screenshot

你好,

我已经能够成功生成恢复代码,并且我可以在

[dbo].[AspNetUserTokens]

中看到数据库中的条目

这是登录代码:

[HttpPost]
public async Task<ActionResult> LoginWithRecoveryCode(LoginwithRecoveryCodeViewModel loginwithRecoveryCodeViewModel)
{
    if (!ModelState.IsValid)
    {
        return View(loginwithRecoveryCodeViewModel);
    }

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

    var recoveryCode = Regex.Replace(loginwithRecoveryCodeViewModel.RecoveryCode, "\\s+", ";");

    var result = await _signInManager.TwoFactorRecoveryCodeSignInAsync(recoveryCode);

    if (result.Succeeded)
    {
        return LocalRedirect(loginwithRecoveryCodeViewModel.ReturnUrl ?? Url.Content("~/"));
    }          
    else
    {
        ModelState.AddModelError(string.Empty, "Invalid recovery code entered.");
        return View(loginwithRecoveryCodeViewModel);
    }
}

它总是进入

Failed
块。注意这个块。我正在尝试将带有空格的代码替换为 ;因为数据库表包含;如屏幕截图所示。但我也尝试用
string.empty
而不是
;
但它不会成功。

c# asp.net asp.net-core asp.net-identity asp.net-core-6.0
1个回答
0
投票

其实我找到了答案。您获得 10 个代码,您需要一一使用每个代码,即 1 个代码用于 1 次登录。我想我需要一次性使用整堆。

© www.soinside.com 2019 - 2024. All rights reserved.