介绍 (Introduction)
In this article, we are going to learn how to perform two-factor authentication in an ASP.NET Core application using the Google Authenticator app.
在本文中,我们将学习如何使用Google Authenticator应用程序在ASP.NET Core应用程序中执行两因素身份验证。
To use it, you need to configure the Google Authenticator app on your smartphone using the QR code generated in the web app. When you login to the web application, you have to enter a six-digit pin that will be generated in the app to finish the two-factor authentication. The key generated in the app will be unique to your userID, and is a time-based one-time password (TOTP) — that is, it will expire after a certain time.
要使用它,您需要使用网络应用程序中生成的QR码在智能手机上配置Google Authenticator应用程序。 登录Web应用程序时,您必须输入一个将在应用程序中生成的六位数密码,以完成两因素身份验证。 应用程序中生成的密钥对于您的用户ID是唯一的,并且是基于时间的一次性密码(TOTP),也就是说,它将在特定时间后失效。
先决条件 (Prerequisites)
Install .NET Core 2.0.0 or above SDK from here.
从此处安装.NET Core 2.0.0或更高版本的SDK。
Install the latest version of Visual Studio 2017 Community Edition from here.
从此处安装最新版本的Visual Studio 2017社区版。
源代码 (Source Code)
Before proceeding, I would recommend that you get the source code from GitHub
在继续之前,我建议您从GitHub获取源代码。
创建MVC Web应用程序 (Create the MVC Web Application)
Open Visual Studio and select File >> New >> Project. After selecting the project, a “New Project” dialog will open. Select .NET Core inside the Visual C# menu from the left panel. Then, select “ASP.NET Core Web Application” from the available project types. Name the project “TwoFactAuth” and press OK.
打开Visual Studio,然后选择“文件>>新建>>项目”。 选择项目后,将打开“新建项目”对话框。 从左侧面板的Visual C#菜单中选择.NET Core。 然后,从可用的项目类型中选择“ ASP.NET Core Web应用程序”。 将项目命名为“ TwoFactAuth”,然后按OK。
After clicking OK, a new dialog will open asking you to select the project template. You can see two drop-down menus at the top left of the template window. Select “.NET Core” and “ASP.NET Core 2.0” from these drop-downs. Then, select the “Web application (Model-View-Controller)” template. Click on “Change Authentication” button. A “Change Authentication” dialog box will open. Select “Individual User Account” and click OK. Now, click OK again to create your web app.
单击确定后,将打开一个新对话框,要求您选择项目模板。 您可以在模板窗口的左上方看到两个下拉菜单。 从这些下拉列表中选择“ .NET Core”和“ ASP.NET Core 2.0”。 然后,选择“ Web应用程序(模型-视图-控制器)”模板。 点击“更改身份验证”按钮。 将打开“更改身份验证”对话框。 选择“个人用户帐户”,然后单击“确定”。 现在,再次单击“确定”以创建您的Web应用程序。