C# 使用Asp.Net Core Identity Core为单页应用程序实现2FA

C# 使用Asp.Net Core Identity Core为单页应用程序实现2FA,c#,single-page-application,asp.net-core-identity,C#,Single Page Application,Asp.net Core Identity,我正在尝试使用Asp.Net核心标识为SPA实现2FA。但是我被卡住了 可在说明末尾找到Authenticate2fa方法的完整代码 在代码中,对于用户,下面的行始终具有空值。我认为在SignInManager.PasswordSignInAsync()中发生了一些神奇的事情,但是,即使我参考了源代码,我也无法找到神奇之处 var user = await _signInManager.GetTwoFactorAuthenticationUserAsync(); 因此,await\u sign

我正在尝试使用Asp.Net核心标识为SPA实现2FA。但是我被卡住了

可在说明末尾找到
Authenticate2fa
方法的完整代码

在代码中,对于
用户
,下面的行始终具有空值。我认为在
SignInManager.PasswordSignInAsync()
中发生了一些神奇的事情,但是,即使我参考了源代码,我也无法找到神奇之处

var user = await _signInManager.GetTwoFactorAuthenticationUserAsync();
因此,
await\u signInManager.two-factorauthenticatorsigninasync(authenticatorCode,false,false)就是不起作用

有关于如何为MVC实现2FA的官方文档,但是我找不到任何与SPA相关的内容

我在正确的轨道上,还是我错过了什么

    [HttpPost]
    [Route("authenticate2fa")]
    public async Task<IActionResult> Authenticate2fa([FromBody]LoginModel loginModel)
    {
        if (ModelState.IsValid)
        {
            var loginResult = await _signInManager.PasswordSignInAsync(loginModel.Username, loginModel.Password, isPersistent: false, lockoutOnFailure: false);

            if (!loginResult.Succeeded && !loginResult.RequiresTwoFactor)
            {
                return BadRequest("user-password-error");
            }

            var user = await _signInManager.GetTwoFactorAuthenticationUserAsync();
            if(user == null)
            {
                return BadRequest("unable-to-load-2fa-user");
            }

            var authenticatorCode = loginModel.TwoFactorCode.Replace(" ", string.Empty).Replace("-", string.Empty);
            var result = await _signInManager.TwoFactorAuthenticatorSignInAsync(authenticatorCode, false, false);

            if (result.Succeeded)
            {
                return Ok(await GenerateAuthenticatedUser(user));
            }
            else if (result.IsLockedOut)
            {
                return BadRequest("account-locked");
            }
            else
            {
                return BadRequest("invalid-2fa-code");
            }
        }

        return BadRequest(ModelState);
    }
[HttpPost]
[路由(“authenticate2fa”)]
公共异步任务Authenticate2fa([FromBody]LoginModel LoginModel)
{
if(ModelState.IsValid)
{
var loginResult=await _signInManager.PasswordSignInAsync(loginModel.Username,loginModel.Password,ispersist:false,lockoutOnFailure:false);
如果(!loginResult.successed&&!loginResult.RequiresTwoFactor)
{
返回错误请求(“用户密码错误”);
}
var user=await _signInManager.GetTwoFactorAuthenticationUserAsync();
if(user==null)
{
返回BADDREQUEST(“无法加载-2fa-user”);
}
var authenticatorCode=loginModel.TwoFactorCode.Replace(“,string.Empty”).Replace(“-”,string.Empty);
var result=await _signInManager.TwoFactorAuthenticatorSignInAsync(authenticatorCode,false,false);
if(result.successed)
{
返回Ok(等待GenerateAuthenticatedUser(用户));
}
else if(result.IsLockedOut)
{
退货申请(“账户锁定”);
}
其他的
{
返回错误请求(“无效-2fa-code”);
}
}
返回请求(ModelState);
}

我自己解决了这个问题

我只是直接使用下面的方法来验证双因素令牌

UserManager.VerifyTwoFactorTokenAsync(...)
由于SignInManager依赖于
HttpContext
,因此它使用
IAAuthenticationService
提供程序来处理2FA请求,您需要查看
IAAuthenticationHandlerProvider
的提供程序(对于
IdentityConstants.TwoFactorUserIdScheme
)是什么。在我的例子中,
TwoFactorUserIdScheme
CookieAuthenticationHandler
处理


虽然它是JWT令牌,但毫无疑问它不起作用。

我自己解决了这个问题

我只是直接使用下面的方法来验证双因素令牌

UserManager.VerifyTwoFactorTokenAsync(...)
由于SignInManager依赖于
HttpContext
,因此它使用
IAAuthenticationService
提供程序来处理2FA请求,您需要查看
IAAuthenticationHandlerProvider
的提供程序(对于
IdentityConstants.TwoFactorUserIdScheme
)是什么。在我的例子中,
TwoFactorUserIdScheme
CookieAuthenticationHandler
处理

虽然它是JWT代币,但毫无疑问它不起作用