azure-identity-py non-hero scenarios
These scenarios are intentionally separate from hero flows in SKILL.md. They cover secondary/advanced patterns typically used after the primary end-to-end path is working.
Async Credentials
Async credentials are in azure.identity.aio. Always close them or use async with:
python
from azure.identity.aio import DefaultAzureCredential
from azure.storage.blob.aio import BlobServiceClient
async def main():
# Preferred: use async context manager for both credential and client
async with DefaultAzureCredential() as credential:
async with BlobServiceClient(
account_url="https://<account>.blob.core.windows.net",
credential=credential,
) as client:
# ... async operations
passThe async
get_bearer_token_provideris atazure.identity.aio.get_bearer_token_provider.
Sovereign Clouds
Use AzureAuthorityHosts or the AZURE_AUTHORITY_HOST env var:
python
from azure.identity import DefaultAzureCredential, AzureAuthorityHosts
# Azure Government
credential = DefaultAzureCredential(authority=AzureAuthorityHosts.AZURE_GOVERNMENT)
# Azure China
credential = DefaultAzureCredential(authority=AzureAuthorityHosts.AZURE_CHINA)| Constant | Authority |
|---|---|
AzureAuthorityHosts.AZURE_PUBLIC_CLOUD | login.microsoftonline.com (default) |
AzureAuthorityHosts.AZURE_GOVERNMENT | login.microsoftonline.us |
AzureAuthorityHosts.AZURE_CHINA | login.chinacloudapi.cn |
Persistent Token Caching
Opt-in disk-based caching with TokenCachePersistenceOptions:
python
from azure.identity import DefaultAzureCredential, TokenCachePersistenceOptions
credential = DefaultAzureCredential(
cache_persistence_options=TokenCachePersistenceOptions()
)
# Allow unencrypted fallback (NOT recommended for production)
credential = DefaultAzureCredential(
cache_persistence_options=TokenCachePersistenceOptions(allow_unencrypted_storage=True)
)Storage: Windows (DPAPI), macOS (Keychain), Linux (Keyring).
Multi-Tenant Support
Allow token acquisition for additional tenants beyond the configured one:
python
from azure.identity import ClientSecretCredential
credential = ClientSecretCredential(