您好,我创建了自己的 asp.net 项目(不是 MVC)。现在我想使用 Google Authenticator 实现两因素身份验证。因此,当用户获得注册时,用户将获得 key 或获得 QR 图像并使用其 Android 手机进行设置。对于登录,他们需要来自谷歌身份验证器应用程序的 key 。
我在asp.net 中得到了很少的MVC 代码。我需要如何集成到 asp.net 应用程序(非 MVC)中的步骤 请指导我如何实现此任何示例将不胜感激。
谢谢
最佳答案
要添加 Google 身份验证,您需要以下内容
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Security.Cryptography;
using System.Text;
using System.Web.Profile;
using System.Web.Security;
using Google.Authenticator;
获取 Google.Authenticator;在这里查看 https://www.nuget.org/packages/GoogleAuthenticator
现在设置谷歌身份验证。
TwoFactorAuthenticator tfa = new TwoFactorAuthenticator();
var setupInfo = tfa.GenerateSetupCode("Name of the app", "More info ABout the App", "SuperSecretKeyGoesHere", 300 , 300//the width and height of the Qr Code);
string qrCodeImageUrl = setupInfo.QrCodeSetupImageUrl; // assigning the Qr code information + URL to string
string manualEntrySetupCode = setupInfo.ManualEntryKey; // show the Manual Entry Key for the users that don't have app or phone
Image1.ImageUrl = qrCodeImageUrl;// showing the qr code on the page "linking the string to image element"
Label1.Text = manualEntrySetupCode; // showing the manual Entry setup code for the users that can not use their phone
您可以更改
SuperSecretKeyGoesHere
为您想要的任何值,但请确保它超过 10 个字符,否则生成的手动输入 key 将不起作用。现在您可以使用文本框和按钮单击检查用户输入
此位将查看用户条目并查看是否正常
string user_enter=TextBox1.Text;
TwoFactorAuthenticator tfa = new TwoFactorAuthenticator();
bool isCorrectPIN = tfa.ValidateTwoFactorPIN("SuperSecretKeyGoesHere", user_enter);
if (isCorrectPIN == true)
{
Label2.Text = "i am cool";
}
else
{
Label2.Text = "i am Fool";
}
关于asp.net - 在自己的 asp.net 项目中使用 Google Authenticator 进行两因素身份验证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23308701/