When building .NET applications that connect to Azure SQL Database using ADO.NET, the connection string is your gateway to secure and reliable access. But did you know there’s more than one way to authenticate?
In this blog, we’ll explore:
✅ Different authentication methods in ADO.NET connection strings
✅ Their pros and cons
✅ Sample code snippets for each
✅ Best practices for secure and scalable applications
Understanding ADO.NET Basics
ADO.NET uses the SqlConnection
class to connect to Azure SQL Database. A typical usage looks like this:
using(var connection = new SqlConnection(connectionString)) { connection.Open(); // Run commands... }
The connection string you pass to SqlConnection
is where you define authentication details.
Different Authentication Methods
1️⃣ SQL Server Authentication (Username/Password)
This is the most common, straightforward way. You specify:
User Id
Password
Example:
var connectionString = "Server=tcp:<server>.database.windows.net;Database=<db>;User Id=<username>;Password=<password>;Encrypt=True;";
Advantages:
✅ Easy to configure
✅ Works across all environments
https://medium.com/@dotnetfullstackdev
Disadvantages:
❌ Password stored in config — potential security risk
❌ Rotating credentials requires app config changes
2️⃣ Azure Active Directory Password Authentication
Uses an Azure AD user to authenticate instead of SQL login. Specify:
Authentication=Active Directory Password
User Id
(your AAD user email)Password
(your AAD password)
Example:
var connectionString = "Server=tcp:<server>.database.windows.net;Database=<db>;Authentication=Active Directory Password;User Id=<user>@<domain>.com;Password=<aadPassword>;";
Advantages:
✅ Integrates with Azure AD security and policies
✅ Centralized user management
Disadvantages:
❌ Requires AAD credentials to be stored in config
❌ Password expiration might affect connection
3️⃣ Azure Active Directory Integrated Authentication
Uses the current Windows identity (domain-joined machine or Azure AD join) to authenticate.
Example:
var connectionString = "Server=tcp:<server>.database.windows.net;Database=<db>;Authentication=Active Directory Integrated;";
Advantages:
✅ Passwordless — uses Windows identity
✅ Seamless integration for domain-joined or AAD-joined devices
Disadvantages:
❌ Only works with domain-joined or properly configured Azure AD environments
❌ Might require additional Kerberos or AAD configuration
4️⃣ Azure Active Directory Interactive Authentication
Prompts the user with a login dialog (browser-based). Useful for interactive tools or local development.
Example:
var connectionString = "Server=tcp:<server>.database.windows.net;Database=<db>;Authentication=Active Directory Interactive;";
Advantages:
✅ Great for developers or tools that need user context
✅ No password stored in the app config
Disadvantages:
❌ Not suitable for unattended services
❌ Requires user interaction at runtime
5️⃣ Azure Managed Identity (Recommended for Cloud Services)
Uses the Managed Identity of the Azure App Service, VM, or container to authenticate without any credentials stored in code.
Example:
Note:
SqlConnection
doesn’t accept a direct connection string for Managed Identity by default — you set the Access Token manually.
Step 1:
Connection string:
var connectionString = "Server=tcp:<server>.database.windows.net;Database=<db>;Encrypt=True;";
Step 2:
Get the access token:
var connection = new SqlConnection(connectionString);
var azureServiceTokenProvider = new AzureServiceTokenProvider();
connection.AccessToken = await azureServiceTokenProvider.GetAccessTokenAsync("https://database.windows.net/");
await connection.OpenAsync();
Advantages:
✅ Best practice for cloud apps — no passwords stored
✅ Tightly integrated with Azure security
✅ Supports automatic credential rotation
Disadvantages:
❌ Only works in Azure environments (App Service, Functions, VM)
❌ Slightly more code complexity
6️⃣ Azure Active Directory Service Principal Authentication
What is it?
A Service Principal is an Azure AD application identity that allows your app to authenticate to Azure services without user interaction.
When to use it?
For background services or daemon apps running outside of Azure (like on-premises servers, containers).
When you want to separate app permissions from human users.
Connection String Example:
var connectionString = "Server=tcp:<server>.database.windows.net;Database=<db>;Authentication=Active Directory Service Principal;User Id=<clientId>;Password=<clientSecret>;";
How to get ClientId and ClientSecret?
Register an App Registration in Azure AD.
Create a Client Secret in the Azure portal.
Assign appropriate RBAC roles to the app.
Code Example:
var connectionString = "Server=tcp:<server>.database.windows.net;Database=<db>;Authentication=Active Directory Service Principal;User Id=<clientId>;Password=<clientSecret>;Encrypt=True;";
using(var connection = new SqlConnection(connectionString))
{
connection.Open();
// Run queries
}
Advantages:
✅ Passwordless (no personal account) — app identity.
✅ Supports least privilege via RBAC.
Disadvantages:
❌ Client Secret stored in app config unless using Key Vault.
❌ Secret rotation required.
7️⃣ Azure Active Directory Device Code Flow
What is it?
Uses a device code flow — useful when you have devices with limited input capabilities.
When to use it?
Tools that run on devices where a browser is not readily available.
Interactive developer scenarios.
Connection String Example:
var connectionString = "Server=tcp:<server>.database.windows.net;Database=<db>;Authentication=Active Directory Device Code Flow;";
Advantages:
✅ User signs in via a separate device — supports MFA.
✅ No need to store credentials in app.
Disadvantages:
❌ Requires user interaction during sign-in.
❌ Not suitable for background services.
8️⃣ Azure Active Directory Access Token
This is a flexible option — you obtain an access token using MSAL or Azure.Identity, then set it on the SqlConnection
.
When to use it?
When you need advanced control over token management.
When integrating with other Azure services using a unified identity approach.
Code Example:
var connectionString = "Server=tcp:<server>.database.windows.net;Database=<db>;Encrypt=True;";
var connection = new SqlConnection(connectionString);
var credential = new ClientSecretCredential(tenantId, clientId, clientSecret);
var token = credential.GetToken(new TokenRequestContext(new[] { "https://database.windows.net/" }));
connection.AccessToken = token.Token;
connection.Open();
Advantages:
✅ Centralizes authentication logic.
✅ Integrates with token caching, Azure SDKs.
Disadvantages:
❌ More code complexity.
❌ Requires MSAL or Azure.Identity SDK.
Best Practices
✅ Use Managed Identity for Azure apps — no credentials in code.
✅ Use Service Principal for non-interactive services or automation.
✅ Store secrets securely in Azure Key Vault (not in code or config files).
✅ Always use Encrypt=True
and TrustServerCertificate=False
for secure communication.
✅ Rotate secrets and certificates regularly.
Conclusion
ADO.NET is flexible enough to connect to Azure SQL Database using a variety of authentication methods. The right choice depends on where your app runs, how users interact, and security requirements.
Use this guide to pick the right sword for the right battle — and connect your .NET apps securely and efficiently.