2017-02-21 37 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 
     }); 
相關問題