2017-02-21 36 views
0

MVC 2FA有时会生成相同的OTP(我已设置为6个数字),并且当生成多个OTP时,可以使用先前的OTP。 有没有办法生成独特的OTP并禁用以前生成的OTP?如何防止在MVC中生成相同的OTP 2FA

string code = await UserManager.GenerateTwoFactorTokenAsync(user.Id, provider); 

这是设置的时间到期OTP

app.UseTwoFactorSignInCookie(DefaultAuthenticationTypes.TwoFactorCookie, TimeSpan.FromMinutes(2)); 
+0

一旦生成OTP,您不应允许用户在一段时间内重新生成OTP,比如说10秒。这将帮助您创建独特的OTP。还可以使用后端跟踪OTP到期而不是Cookie。 –

回答

0

OTP是基于时间和任何地方没有记录了。如果您在短时间内生成2个OTP,则会得到相同的字符串。这就是算法的工作方式,并且没有简单的方法来解决这个问题。

0

我刚刚偶然发现了这篇文章,发现了一个简单的解决方案。

https://www.stevejgordon.co.uk/asp-net-core-identity-token-providers

此链接描述了SecurityTokenStamp是在验证过程中使用的事实。所以对我而言,每次我为用户发送一个SMS令牌时,更新它都是一件简单的事情。有效地使原始的无效。

if(await userManager.UpdateSecurityStampAsync(user.Id) != IdentityResult.Success) 
     { 
      // https://www.stevejgordon.co.uk/asp-net-core-identity-token-providers 
      // we update it to effectively reset all the token validation stuff 
      return IdentityResult.Failed("failed to update the security stamp"); 
     } 

     // Send token - this may throw but thats ok as we just rollback 
     string code = await userManager.GenerateTwoFactorTokenAsync(user.Id, "twilio"); 
     await userManager.SmsService.SendAsync(new Microsoft.AspNet.Identity.IdentityMessage 
     { 
      Destination = user.UserName, 
      Body = "Your security code is: " + code 
     }); 
相关问题