使用asp.net核心的google身份验证器

Sat*_*jit 5 google-authenticator two-factor-authentication asp.net-core-mvc

除了短信和电子邮件之外,Google身份验证器是否还有任何示例实现作为双因素身份验证实施?

找到一个样本. 使用asp.net示例Google身份验证器

但是在使用asp.net核心时会有很多变化.

Jea*_*n.R 7

您可以使用AspNetCore.Totp. https://github.com/damirkusar/AspNetCore.Totp

它的工作方式与GoogleAuthenticator完全相同,请查看Tests项目的实现(非常简单).

您只需编写几行来获取qurcode并验证pin代码:

using AspNetCore.Totp;

...

// To generate the qrcode/setup key

var totpSetupGenerator = new TotpSetupGenerator();
var totpSetup = totpSetupGenerator.Generate("You app name here", "The username", "YourSuperSecretKeyHere", 300, 300);

string qrCodeImageUrl = totpSetup.QrCodeImage;
string manualEntrySetupCode = totpSetup.ManualSetupKey;


// To validate the pin after user input (where pin is an int variable)
var totpValidator = new TotpValidator();
bool isCorrectPIN = totpValidator.Validate("YourSuperSecretKeyHere", pin);
Run Code Online (Sandbox Code Playgroud)

  • 该字符串是 base64 格式的图像数据。要在浏览器中显示它,请将字符串显示在 `<img>` 标签中,如下所示:`<img src="data:image/png;base64,iVBORw0KGgoAAAANSU....">` (2认同)