c# - net core identity 2.1 中方法 VerifyTwoFactorTokenAsync() 的问题

标签 c# asp.net-core-identity

我正在使用 .Net Core Identity 2.1 启用带有二维码的身份验证器。当我使用方法 VerifyTwoFactorTokenAsync() 并在本地主机中运行时,它工作正常。但是当我上传到主机时,它不起作用并且 is2faTokenValid 总是返回 false。 有没有人遇到过类似的问题并找到了解决方案或知道我在哪里犯了错误?

        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> EnableAuthenticator(AuthenticatorModel model)
        {
            var user = await GetCurrentUserAsync();
            if (user == null)
            {
                return NotFound($"Not found user with Id '{_userManager.GetUserId(User)}'.");
            }

            if (!ModelState.IsValid)
            {
                await LoadSharedKeyAndQrCodeUriAsync(user);
                return View();
            }

            // Strip spaces and hypens
            var verificationCode = model.Code.Replace(" ", string.Empty).Replace("-", string.Empty);
            var is2faTokenValid = await _userManager.VerifyTwoFactorTokenAsync(
                user, _userManager.Options.Tokens.AuthenticatorTokenProvider, verificationCode);  
            if (!is2faTokenValid)
            {

                ModelState.AddModelError("", "Invalid code.");
                var newmodel = await LoadSharedKeyAndQrCodeUriAsync(user);
                return View(newmodel);
            }

            await _userManager.SetTwoFactorEnabledAsync(user, true);
            var userId = await _userManager.GetUserIdAsync(user);
            _logger.LogInformation("User with ID '{UserId}' has enabled 2FA with an authenticator app.", userId);
            return Redirect("/Account/TwoFactorAuthentication");
        }
        private Task<AppUser> GetCurrentUserAsync()
        {
            return _userManager.GetUserAsync(User);
        }
        private async Task<AuthenticatorModel> LoadSharedKeyAndQrCodeUriAsync(AppUser user)
        {
            // Load the authenticator key & QR code URI to display on the form
            var unformattedKey = await _userManager.GetAuthenticatorKeyAsync(user);
            if (string.IsNullOrEmpty(unformattedKey))
            {
                await _userManager.ResetAuthenticatorKeyAsync(user);
                unformattedKey = await _userManager.GetAuthenticatorKeyAsync(user);
            }            
            var model = new AuthenticatorModel();
            model.SharedKey = FormatKey(unformattedKey);
            var userName = await _userManager.GetUserNameAsync(user);
            model.AuthenticatorUri = GenerateQrCodeUri(userName, unformattedKey);
            return model;
        }

最佳答案

Xem thời gian máy chủ và thời gian của thiết bị có cái nào sai sai ko:

TOTP 客户端和服务器时间偏差

TOTP(基于时间的一次性密码)身份验证取决于服务器和身份验证器设备都具有准确的时间。 token 仅持续 30 秒。如果 TOTP 2FA 登录失败,请检查服务器时间是否准确,最好与准确的 NTP 服务同步。

使用:与互联网时间服务器同步:time.nist.gov 不要使用:time.windows.com

关于c# - net core identity 2.1 中方法 VerifyTwoFactorTokenAsync() 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56243238/

相关文章:

asp.net-mvc - 如何为未经同意的用户创建身份服务器页面?

c# - 如何支持用户名unicode?

c# - 如何在 ASP.NET Core 3 上同时使用 Azure AD 身份验证和身份?

c# - ASP.NET Core 2.0 JWT 验证失败,出现 `Authorization failed for user: (null)` 错误

c# - 从局部 View 向页面的 <head> 添加 CSS 引用

c# - 将包含图像及其图层的 HTML div 标记保存到新图像

c# - Asp Core 2.1 Jwt + 身份。 userManager 存储未实现 IUserRoleStore

c# - 独立于平台的 protobuf 套接字协议(protocol)——我会收到什么消息?

c# - Visual Studio 解决方案引用

c# - DependencyResolver 不解析依赖关系。为什么?