Share via

Need assistance and git hub code for multifactor authentication in core mvc or mvc project.

vinayak p 0 Reputation points
2025-02-20T10:29:37.1866667+00:00

Need assistance and git hub code for multifactor authentication in core mvc or mvc project.

regrads

vinayak

Developer technologies | ASP.NET Core | Other
0 comments No comments

5 answers

Sort by: Most helpful
  1. Danny Nguyen (WICLOUD CORPORATION) 7,025 Reputation points Microsoft External Staff Moderator
    2025-08-20T03:33:41.88+00:00

    Hi there,

    I understand you’re looking to implement multi-factor authentication (MFA) in an ASP.NET Core MVC (or MVC) project and are also looking for some sample code to get started.


    ASP.NET Core Identity has built-in support for two-factor authentication (2FA) using email, SMS, or authenticator apps (like Microsoft Authenticator or Google Authenticator). This means you don’t have to build MFA from scratch—you just need to enable it in your Identity configuration and UI.

    If you are using classic ASP.NET MVC (non-Core), you can still implement MFA, but most up-to-date examples and templates are available in ASP.NET Core Identity.

    Steps to Implement MFA in ASP.NET Core MVC

    1. Set up Identity in your project (if not already):
         services.AddIdentity<ApplicationUser, IdentityRole>()
             .AddEntityFrameworkStores<ApplicationDbContext>()
             .AddDefaultTokenProviders();
      
    2. Enable Token Providers for MFA:
      • Email (EmailTokenProvider)
      • Phone/SMS (PhoneNumberTokenProvider)
      • Authenticator apps (AuthenticatorTokenProvider)
    3. Configure 2FA flow in UI:
      • Register/Login → Ask for username & password
      • If MFA is enabled → Prompt for the second factor (code via SMS, email, or authenticator app)
      • Validate token → Sign in user
    4. Use the built-in Identity scaffolder to add account management pages for enabling/disabling 2FA.

    Also check out this Microsoft official example (ASP.NET Core Identity with 2FA): Two-Factor Authentication in ASP.NET Core

    Best Practice Notes

    • Use authenticator apps instead of SMS when possible (stronger security).
    • Store recovery codes securely so users don’t get locked out.
    • Consider integrating with Azure AD B2C or external identity providers if you need enterprise-level MFA.

    Hope this helps, feel free to reach out if you encounter any problem

    Was this answer helpful?


  2. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.

    5 deleted comments

    Comments have been turned off. Learn more

  3. SurferOnWww 6,016 Reputation points
    2025-02-23T01:06:56.93+00:00

    MFA using TOTP is a supported implementation using ASP.NET Core Identity. Please see the following Microsoft document:

    MFA TOTP (Time-based One-time Password Algorithm)

    enter image description here

    Was this answer helpful?

    0 comments No comments

  4. Anonymous
    2025-02-21T08:03:13.47+00:00

    Hi @vinayak p,

    As Burce said, asp.net core document contains the article about how you configure the asp.net core with the 2FA. The document is very clear.

    Also if you want to know if there is some sample codes inside the github, I suggest you could refer to below link:

    https://github.com/dotnet/AspNetCore.Docs/tree/main/aspnetcore/security/authentication/2fa/sample/Web2FA

    Was this answer helpful?

    0 comments No comments

  5. Bruce (SqlWork.com) 84,071 Reputation points
    2025-02-20T18:50:07.3333333+00:00

    asp.net core has sms and email support for MFA. a code is sent and used for login. see docs, its pretty clear. the sample uses twillo or aspsms for SMS, but you can change the code to match your preferred SMS provider.:

    https://learn.microsoft.com/en-us/aspnet/core/security/authentication/2fa?view=aspnetcore-1.1&viewFallbackFrom=aspnetcore-9.0

    if you want to use authenticator apps, then it probably easier to switch to Entra authentication.

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.