Azure

How to connect to an Azure Key Vault and Azure SQL Database using the same Token Credential

Jonathan Crozier Jonathan Crozier
Azure

Azure Key Vault asks for CAE-enabled tokens while Azure SQL does not, so a single InteractiveBrowserCredential prompts desktop users to sign in twice. Jonathan Crozier shows how to request CAE tokens on both sides and attach the SQL token through an EF Core interceptor.

Key points

  • Continuous Access Evaluation explains the second sign-in: the token cached for Key Vault is a different token type.
  • Sign in once with AuthenticateAsync and a TokenRequestContext for https://vault.azure.net/.default, setting isCaeEnabled: true.
  • A DbConnectionInterceptor sets SqlConnection.AccessToken from the shared TokenCredential before EF Core opens the connection.
  • Request https://database.windows.net/.default with isCaeEnabled: true so the SQL token lines up with the cached one.
  • Override both ConnectionOpening and ConnectionOpeningAsync, and register the interceptor through AddInterceptors.
  • The user still needs Key Vault access and a database user added via CREATE USER ... FROM EXTERNAL PROVIDER.

Why it matters

This is a narrow problem that costs hours to diagnose, because nothing in the failure mentions CAE — you just get a browser prompt you thought you had already answered. Anyone building a desktop client or internal tool that mixes Key Vault configuration with Azure SQL and interactive sign-in will meet it.

Adopting the pattern has a cost: token acquisition moves into a connection interceptor, so expiry, refresh and caching become your code's concern rather than the SDK's. For server-side workloads, managed identity with Azure SQL's own authentication setting is still the simpler route; keep the shared-credential approach for clients where a real user is present.

Real-world use

  • A desktop or console .NET app where users sign in interactively once and then reach both Key Vault secrets and an Azure SQL database.
  • An app that loads Key Vault secrets as an IConfiguration source via AddAzureKeyVault and also queries the same database with EF Core.
  • Local or internal tooling that must run under the signed-in developer's own Entra identity instead of a separate credential per service.

This summary was generated by the agent from the original post on Jonathan Crozier. The full text and all rights belong to its author and publisher. Go to original

Report this story

Is something wrong with the summary, or do you own this content? Your message comes straight to my inbox.

Type of message

Related stories

Feedback

Found a bug or have an idea? Your message lands straight in my inbox.

Type of message