TwoFactorRecoveryCodeSignInAsync 始终失败,即使
[dbo].[AspNetUserTokens]
中存在记录。
你好,
我已经能够成功生成恢复代码,并且我可以在
[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
而不是 ;
但它不会成功。
其实我找到了答案。您获得 10 个代码,您需要一一使用每个代码,即 1 个代码用于 1 次登录。我想我需要一次性使用整堆。