【发布时间】:2021-05-25 01:51:35
【问题描述】:
我正在尝试了解 Google Two Factor Authenticator 流程的工作原理,以便将其整合到我的网站中。我的理解是这个过程有两个不同的部分
- 我网站上的用户启用了 2FA,这在我的用户和应用程序之间创建了一个链接。这是一次性步骤,并非每次登录尝试都会发生。
- 用户每次登录时,都需要提供来自 Google Authenticator 应用的六位数代码。
现在,以下代码生成 QR 图像和设置代码以启用 2FA 并将帐户链接到 Google Authenticate。
TwoFactorAuthenticator tfa = new TwoFactorAuthenticator();
string accountSecretKey = Guid.NewGuid();
var setupInfo = tfa.GenerateSetupCode("Dotnet Awesome", login.Username, accountSecretKey, 300, 300);
ViewBag.BarcodeImageUrl = setupInfo.QrCodeSetupImageUrl;
ViewBag.SetupCode = setupInfo.ManualEntryKey;
现在对于每个请求,我都会使用以下代码对用户进行身份验证
TwoFactorAuthenticator tfa = new TwoFactorAuthenticator();
tfa.ValidateTwoFactorPIN(accountSecretKey, "Six Digit Code");
问题
在上面的代码中,accountSecretKey 代码是不是我必须保存到我的数据库中以便每次我想验证时都可以传递它?或者,accountSecretKey 在每次登录尝试时我都必须重新创建?如果这段代码我要存储到我的数据库中,它是否也应该像密码一样加密?
【问题讨论】:
标签: c# two-factor-authentication google-authenticator authenticator google-2fa