[在自己的asp.net项目中使用Google Authenticator进行两因素身份验证?

问题描述 投票:6回答:2

您好,我已经创建了自己的asp.net项目(不是MVC)。现在,我想使用Google Authenticator实施两因素身份验证。因此,当用户获得注册时,用户将获得密钥或获取QR图片并使用其Android手机进行设置。并且要登录,他们需要来自Google Authenticator应用的密钥。

我在asp.net中没有几个MVC代码。我需要步骤如何集成到asp.net应用程序(不是MVC)中,请指导如何实现此示例,我们将不胜感激。

谢谢

asp.net two-factor-authentication google-authenticator
2个回答
5
投票

要添加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

现在正在设置Google身份验证。

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个字符,否则生成的手动输入键将无法使用。现在,您可以使用文本框检查用户输入,然后单击按钮。

此位将查看用户条目,看是否还可以

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";
}

2
投票

[旧问题,但我已经确切地将您的情况写了博客,您可以阅读以下文章:Two Factor Authentication in ASP.NET Web API & AngularJS using Google Authenticator希望这能回答您的问题。

© www.soinside.com 2019 - 2024. All rights reserved.