Two-factor-authentication-using-google-authenticator-in-asp-net-core 6-mvc
Loading
Two-factor-authentication-using-google-authenticator-in-asp-net-core 6-mvc
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Brahma Prakash ShuklaRank #633 · 3.8K reputationPosted Jul 14, 2023, 7:50 AM EDT
To implement two-factor authentication using Google Authenticator in an ASP.NET Core 6 MVC application, you can follow these steps:
Step 1: Install the required packages In your ASP.NET Core MVC project, install the following NuGet packages:
Google.Authenticator: This package provides the functionality to generate and validate the OTP codes.QRCoder: This package helps in generating QR codes for the user to scan and set up the authenticator app.You can install these packages using the NuGet Package Manager Console or the NuGet Package Manager UI in Visual Studio.
Step 2: Set up the User model Create a User model or extend your existing User model to store the secret key and enable two-factor authentication for users. Add the following properties to the User model:
public class User { // Existing properties public string SecretKey { get; set; } public bool TwoFactorEnabled { get; set; } }Step 3: Generate and display QR code for setup When a user enables two-factor authentication, you need to generate a secret key and display a QR code for the user to scan with their authenticator app. In your AccountController or any appropriate controller action, add the following code:
public IActionResult EnableTwoFactorAuth() { var user = // Get the currently logged-in user var tfaKey = TwoFactorAuthenticator.GenerateKey(); var tfaUrl = $"otpauth://totp/{Uri.EscapeDataString("YourAppName")}:{Uri.EscapeDataString(user.Email)}?secret={tfaKey}"; user.SecretKey = tfaKey; user.TwoFactorEnabled = false; // Set to true once user verifies the setup var qrCodeGenerator = new QRCodeGenerator(); var qrCodeData = qrCodeGenerator.CreateQrCode(tfaUrl, QRCodeGenerator.ECCLevel.Q); var qrCode = new QRCode(qrCodeData); var qrCodeImage = qrCode.GetGraphic(20); // Pass the qrCodeImage and other required data to the view return View(qrCodeImage); }In the view associated with the
EnableTwoFactorAuthaction, display the QR code image to the user.Step 4: Verify the OTP code After the user scans the QR code and sets up their authenticator app, you need to verify the OTP code entered by the user. Add the following code to the appropriate controller action:
public IActionResult VerifyTwoFactorAuth(string code) { var user = // Get the currently logged-in user var tfa = new TwoFactorAuthenticator(); var isValid = tfa.ValidateTwoFactorPIN(user.SecretKey, code); if (isValid) { user.TwoFactorEnabled = true; // Enable two-factor authentication for the user // Additional logic after successful verification return RedirectToAction("Index", "Home"); } ModelState.AddModelError("", "Invalid verification code"); // Additional logic for unsuccessful verification return View(); }Step 5: Disable two-factor authentication If the user wants to disable two-factor authentication, add the following code to an appropriate controller action:
public IActionResult DisableTwoFactorAuth() { var user = // Get the currently logged-in user user.TwoFactorEnabled = false; // Disable two-factor authentication for the user // Additional logic after disabling two-factor authentication return RedirectToAction("Index", "Home"); }These are the general steps to implement two-factor authentication using Google Authenticator in ASP.NET Core 6 MVC. You may need to customize the code based on your specific requirements and application architecture.