Retrieving Secrets in Applications
This lesson explains SecretClient and DefaultAzureCredential, caching and resilience, and the secret reference features of Azure hosting platforms.
Learning outcomes
After completing this lesson, you will be able to:
- Describe how DefaultAzureCredential chooses a credential.
- Explain how to cache secrets safely.
- Compare platform references for App Service, Container Apps and AKS.
- Choose between the SDK and platform references.
SecretClient and DefaultAzureCredential
The azure-keyvault-secrets library's SecretClient reads and manages secrets. It authenticates with a credential from azure-identity, almost always DefaultAzureCredential, which tries a chain of sources in order: environment variables, workload identity (in AKS), managed identity (in Azure services), and developer sign-ins such as the Azure CLI. The same code therefore works on a developer's machine with their own sign-in and in Azure with the application's managed identity.
from azure.identity import DefaultAzureCredential
from azure.keyvault.secrets import SecretClient
client = SecretClient(vault_url="https://VAULT.vault.azure.net", credential=DefaultAzureCredential())
secret = client.get_secret("model-api-key") # current version
print(secret.properties.version, secret.properties.expires_on)
api_key = secret.value
older = client.get_secret("model-api-key", version="3f2a9c...") # a specific version
for props in client.list_properties_of_secrets(): # names and attributes, no values
print(props.name, props.enabled, props.expires_on)When an application has several user-assigned identities, DefaultAzureCredential(managed_identity_client_id=...) or ManagedIdentityCredential(client_id=...) selects one. Errors surface as ResourceNotFoundError for a missing secret and HttpResponseError with status 403 for a missing role assignment or a firewall block.
SecretClient calls
Caching and resilience
Retrieving a secret on every request adds latency and risks throttling. Applications should read secrets at start-up or on first use and cache them in memory for a period, such as an hour, refreshing in the background. A useful refinement is to refresh immediately when the downstream service rejects the cached credential, which handles rotation gracefully: after the key is rotated, the first authentication failure triggers a reload of the new version.
Secrets must never be written to logs, error messages or telemetry. The Azure SDK clients retry transient failures automatically, and applications should fail clearly at start-up if a required secret cannot be read, rather than running with a missing value.
Using secrets safely
Platform references
Hosting platforms can resolve secrets on the application's behalf, so the code only reads an environment variable or a file. Each platform uses the application's managed identity, which needs Key Vault Secrets User.
Azure App Configuration offers a fourth option: a key-value can be a Key Vault reference, and the App Configuration provider resolves it with the application's credential when it loads configuration. This keeps secrets in Key Vault while letting one configuration load deliver both ordinary settings and secrets.
The following diagram compares a Key Vault secret reference blocked by the vault firewall with the fixed connectivity.

Platform references
Choosing between the SDK and references
References need no code and keep secrets out of the application's logic, which suits most settings read at start-up. The SDK is the better choice when the application must read secrets dynamically (for example, per tenant), control caching and refresh precisely, or handle rotation without a restart. Many applications combine them: platform references for their own connection settings, and the SDK for per-tenant or rotating credentials.
SDK or platform reference?
A 403 after deployment
A container app reads a database password through a Key Vault secret reference using its system-assigned identity. After the vault's firewall was enabled with "Allow trusted Microsoft services" and the app's outbound IP addresses added, the app still fails to start, and the system logs show the secret could not be retrieved with a 403 error. The identity holds Key Vault Secrets User.
Container Apps' outbound addresses can change and are not a reliable firewall rule, and Container Apps is not a trusted service for this purpose. The robust fix is network connectivity that the firewall recognizes: integrate the Container Apps environment with a virtual network and give the vault a private endpoint in that network (with its private DNS zone linked), or allow the environment's subnet through a virtual network rule.
Assigning a broader role would not help, because authorization already passes and the network blocks the request. Disabling the firewall would solve it by removing a security control. Storing the password as a plain secret in the app avoids Key Vault altogether.
Why can the same DefaultAzureCredential code run on a developer's laptop and in Azure?
Show answer
It tries a chain of credential sources: environment variables, workload identity, managed identity, then developer sign-ins such as the Azure CLI. It uses the managed identity in Azure and the developer's sign-in locally.
Exam Tips
Remember for the exam
- SecretClient with DefaultAzureCredential works locally and in Azure without code changes.
- Cache secrets in memory and reload when a downstream service rejects the credential.
- Platform references: app settings (App Service, Functions), keyvaultref secrets (Container Apps), CSI driver (AKS).
- A 403 with the right role often means the vault firewall or private networking blocks the caller.