Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# net core identity 2.1中的VerifyTwoFactorTokenAsync()方法存在问题_C#_Asp.net Core Identity - Fatal编程技术网

C# net core identity 2.1中的VerifyTwoFactorTokenAsync()方法存在问题

C# net core identity 2.1中的VerifyTwoFactorTokenAsync()方法存在问题,c#,asp.net-core-identity,C#,Asp.net Core Identity,我正在使用.Net Core Identity 2.1启用带有qrcode的验证器。当我使用VerifyTwoFactorTokenAsync()方法并在localhost中运行时,它工作正常。但当我上传到主机时,它不起作用,并且is2faTokenValid总是返回false。 有没有人遇到过类似的问题并找到了解决方案,或者知道我在哪里犯了错误 [HttpPost] [ValidateAntiForgeryToken] 公共异步任务EnableAuthenticator(Authenticat

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

[HttpPost]
[ValidateAntiForgeryToken]
公共异步任务EnableAuthenticator(AuthenticatorModel模型)
{
var user=await GetCurrentUserAsync();
if(user==null)
{
返回NotFound($“找不到Id为“{{u userManager.GetUserId(user)}”的用户”);
}
如果(!ModelState.IsValid)
{
等待LoadSharedKeyAndQrCodeUriAsync(用户);
返回视图();
}
//带状空间与hypens
var verificationCode=model.Code.Replace(“,string.Empty”).Replace(“-”,string.Empty);
var is2faTokenValid=await\u userManager.VerifyTwoFactorTokenAsync(
用户,_userManager.Options.Tokens.AuthenticatorTokenProvider,verificationCode);
如果(!is2faTokenValid)
{
AddModelError(“,”无效代码“);
var newmodel=等待LoadSharedKeyAndQrCodeUriAsync(用户);
返回视图(新模型);
}
wait_userManager.setTwoFactoryEnabledAsync(user,true);
var userId=wait_userManager.GetUserIdAsync(用户);
_logger.LogInformation(“ID为“{UserId}”的用户已使用验证器应用程序启用2FA。”,UserId);
返回重定向(“/Account/TwoFactorAuthentication”);
}
私有任务GetCurrentUserAsync()
{
返回_userManager.GetUserAsync(用户);
}
专用异步任务LoadSharedKeyAndQrCodeUriAsync(AppUser)
{
//加载要在表单上显示的验证器密钥和QR码URI
var unformatedkey=await\u userManager.GetAuthenticatorKeyAsync(用户);
if(string.IsNullOrEmpty(unformatedKey))
{
wait_userManager.ResetAuthenticatorKeyAsync(用户);
unformatedkey=await\u userManager.GetAuthenticatorKeyAsync(用户);
}            
var model=newauthenticatormodel();
model.SharedKey=FormatKey(未格式化key);
var userName=await\u userManager.GetUserNameAsync(用户);
model.AuthenticatorUri=GenerateQrCodeUri(用户名,unformatedKey);
收益模型;
}

Xem thờ我是詹·马奇ủ váthờ伊吉安củ一个ếTBị cócái náo sai ko:

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

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

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

您可以添加
GetCurrentUserAsync()
的代码吗?对于我添加到代码中的
LoadSharedKeyAndQrCodeUriAsync
也一样。请帮助我解决问题。我还从
VerifyTwoFactoryTokenAsync()
获得了错误返回。我试图将
AuthenticatorTokenProvider
更改为
EmailConfirmationTokenProvider
,但仍然得到错误返回。还是卡住了……哦,是的,这里也有同样的问题!我的服务器时间1分钟不准确,用户身份验证totp失败。只要运行w32tm/resync(最好创建一个一天运行一次的计划),它就再次工作了。非常感谢。