📁 Sangam Dhakal - Portfolio v2.0 - Netscape Navigator 4.0
_
□
×

📚 Interview question

How do you connect EF Core to Azure SQL without a password in the connection string?

Senior CodeReview Advanced

🎯 Key points

  • Entra admin on the logical server; optionally Entra-only authentication
  • Managed identity on the hosting service (system- or user-assigned)
  • CREATE USER [identity] FROM EXTERNAL PROVIDER in the database
  • Least-privilege roles; migrations run under a separate identity
  • Authentication=Active Directory Default (or Managed Identity + client id)
  • Microsoft.Data.SqlClient acquires and refreshes tokens; no EF Core code change
  • Same connection string works locally with developer sign-in
  • No password to store, leak or rotate (Essential Eight, NDB risk)

⏱ 30-second answer

Use Microsoft Entra ID authentication with a managed identity. Steps: 1. set a Microsoft Entra admin on the Azure SQL logical server (optionally enable Microsoft Entra-only authentication to turn off SQL logins); 2. turn on a system- or user-assigned managed identity on the App Service, Container App or AKS workload; 3. connected as the Entra admin, create a contained user for it in the database, CREATE USER [my-api] FROM EXTERNAL PROVIDER;, and grant least privilege (db_datareader, db_datawriter, no DDL for the app); 4. use a connection string with Authentication=Active Directory Default (or Active Directory Managed Identity plus User Id=<client id> for a user-assigned identity). Microsoft.Data.SqlClient gets and refreshes tokens itself, so EF Core needs no code change, and locally the same string uses your Azure CLI or Visual Studio sign-in. No secret exists to leak or rotate.

🗣 Full answer (senior)

How I would answer this in an interview

"I remove the password entirely with Microsoft Entra authentication and a managed identity. On the Azure SQL server I set an Entra admin group and, ideally, enable Entra-only authentication so SQL logins can't be used at all. I turn on a managed identity for the App Service or Container App. Then, connected as the Entra admin, I run CREATE USER [orders-api] FROM EXTERNAL PROVIDER in the database and give it only the roles it needs, usually reader and writer; migrations run from the pipeline under a different identity that has DDL rights.

The connection string becomes Server=tcp:myserver.database.windows.net;Database=orders;Authentication=Active Directory Default;Encrypt=True. Microsoft.Data.SqlClient uses DefaultAzureCredential behind that keyword, gets a token for the managed identity and refreshes it, so UseSqlServer doesn't change. Locally the same string picks up my az login or Visual Studio account, so there is no secret in user-secrets either. For a user-assigned identity I use Active Directory Managed Identity with User Id set to its client id, which avoids the credential chain probing other sources.

The point is not convenience: a connection string with a password is a long-lived credential that ends up in config, logs and backups. With a managed identity there is nothing to leak or rotate, which lines up with the ACSC Essential Eight and reduces Notifiable Data Breach exposure."


{
  "ConnectionStrings": {
    "Orders": "Server=tcp:sql-orders-aue.database.windows.net,1433;Database=orders;Authentication=Active Directory Default;Encrypt=True;"
  }
}
CREATE USER [app-orders-api] FROM EXTERNAL PROVIDER;
ALTER ROLE db_datareader ADD MEMBER [app-orders-api];
ALTER ROLE db_datawriter ADD MEMBER [app-orders-api];

Options

Keyword Use
Active Directory Default Same string for local dev and Azure
Active Directory Managed Identity + User Id User-assigned identity, no credential probing
SQL login + password in Key Vault Only when Entra auth is impossible

Production example

Removing SQL logins from [six] services removed [six] secrets from Key Vault rotation, and a later leaked config file in a support ticket exposed no usable credential.

🔍 What the interviewer is checking

Checks secure production configuration.

↩ Usually asked after

More in EF Core Fundamentals

Last updated 23 Nov 2026.

🌐 Sangam's Webring

⏪ Previous 🏠 Main Next ⏩
💬 Chat with me