Replace Twilio-based OTP (SMS login) system with Azure Active Directory (Azure AD)
Kiran Rongali

Kiran Rongali @kiranrongali

About: 17 years of experience in designing and developing web, Windows, and API services using Microsoft .NET (C#, ASP.NET Core, MVC, WCF, WPF, ADO.NET), Java, and cloud-native technologies.

Location:
Florida
Joined:
Apr 15, 2025

Replace Twilio-based OTP (SMS login) system with Azure Active Directory (Azure AD)

Publish Date: Apr 15
16 2

Switching your .NET application from Twilio-based authentication (like SMS 2FA/OTP) to Active Directory authentication (either Azure AD or on-prem AD) involves replacing your current authentication mechanism with one based on enterprise identity management.

After the migration, authentication will be secured through Azure Active Directory, supporting Single Sign-On (SSO) and ready for Multi-Factor Authentication (MFA).
The identity storage is fully managed within Azure AD, removing the need for custom solutions.
User onboarding will automatically be handled through Active Directory, simplifying access management.
SMS-related costs, such as those from Twilio, will be eliminated.
Lastly, the overall enterprise compliance posture will be improved through standardized and secure identity practices.

Let’s break this down clearly:
Pre-Requisites:

  1. Your app is built in ASP.NET Core (.NET 5, 6, 7 or 8).
  2. You have access to an Azure AD tenant.
  3. You are able to register applications in Azure AD (via Azure Portal or your AD admin).
  4. Your app currently uses Twilio for OTP logins (e.g., phone number + code). Overview of Key Differences

Twilio OTP :
User Identity Source uses a Custom database or phone-based
Auth Type uses one time pass code (OTP) using SMS
Security & Scalability are Basic
User Experience is Manual login

Auth Azure AD Authentication :
User Identity Source uses Azure AD (SSO, corporate directory)
Auth Type is OAuth2 + OpenID Connect
Security & Scalability - Enterprise-grade (MFA, conditional access)
User Experience - Seamless SSO, AD-integrated login

Step-by-Step : Switch from Twilio to Active Directory Authentication

Step 1: Identify Your Target AD
Azure Active Directory (Azure AD) – for cloud-based authentication (common for web apps)

On-Premises Active Directory – use Windows Authentication or LDAP via a VPN or internal network

Most modern .NET Core apps use Azure AD for seamless SSO and federation.

Step 2: Remove Twilio Auth Logic
Remove or comment out OTP generation and SMS logic in your login flow

Typically, this would be in services like AuthService, OTPController, or middleware

Example:

//C#
// Remove code that sends verification code via Twilio
// twilioClient.SendMessage(phoneNumber, "Your OTP is...");
Enter fullscreen mode Exit fullscreen mode

Step 3: Add Azure AD Authentication (for ASP.NET Core)

3.1. Install the NuGet Package:

bash: dotnet add package Microsoft.AspNetCore.Authentication.AzureAD.UI
Enter fullscreen mode Exit fullscreen mode

For .NET 6+, use Microsoft.Identity.Web

3.2. In Program.cs or Startup.cs:

//If .NET 6+
//C#
builder.Services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
    .AddMicrosoftIdentityWebApp(builder.Configuration.GetSection("AzureAd"));

builder.Services.AddControllersWithViews();
Enter fullscreen mode Exit fullscreen mode
//If .NET Core 3.1/5.0:
services.AddAuthentication(AzureADDefaults.AuthenticationScheme)
    .AddAzureAD(options => Configuration.Bind("AzureAd", options));
Enter fullscreen mode Exit fullscreen mode

3.3. In appsettings.json:

"AzureAd": {
  "Instance": "https://login.microsoftonline.com/",
  "Domain": "yourcompany.com",
  "TenantId": "YOUR_TENANT_ID",
  "ClientId": "YOUR_CLIENT_ID",
  "CallbackPath": "/signin-oidc"
Enter fullscreen mode Exit fullscreen mode

3.4. Add Authentication Middleware:

//C#
app.UseAuthentication();
app.UseAuthorization();
Enter fullscreen mode Exit fullscreen mode

Step 4: Protect Your Controllers/Pages

//C#
[Authorize]
public class DashboardController : Controller
{
    public IActionResult Index()
    {
        return View();
    }
}

Enter fullscreen mode Exit fullscreen mode

Step 5: (Optional) Display AD Info or Roles

//C#
User.Identity.Name; // Gets AD username
User.IsInRole("Admin"); // Role-based access
Enter fullscreen mode Exit fullscreen mode

Note: If you need to access more user info (email, group membership, etc.), you can use Microsoft Graph API.

Final Step: Test the Authentication Flow

  • When users hit a protected route, they should be redirected to Microsoft login
  • After login, they’re redirected back to your app with an authenticated session

Comments 2 total

  • Chnet
    ChnetApr 30, 2025

    Nice post!!

  • Chnet
    ChnetApr 30, 2025

    Awesome write up!

Add comment