npx skills add ...
npx skills add microsoft/agent-skills --skill azure-identity-py
Azure Identity SDK for Python authentication with Microsoft Entra ID. Use for DefaultAzureCredential, managed identity, service principals, and token caching. Triggers: "azure-identity", "DefaultAzureCredential", "authentication", "managed identity", "service principal", "credential".
This repo is now called microsoft/skills. Both names install the same content, but the install count here only covers this one.
npx skills add microsoft/agent-skills --skill azure-identity-py
Authentication library for Azure SDK clients using Microsoft Entra ID.
Use this skill when:
DefaultAzureCredential for local dev + Azure deploymentManagedIdentityCredential for Azure-hosted workloadsget_token()For VS Code or broker-based desktop auth:
azure-identity supports Python 3.9+.
🔑 Two rules apply to every code sample below:
- Prefer
DefaultAzureCredential. It works locally (Azure CLI / VS Code / Developer CLI) and in Azure (managed identity, workload identity) with no code change. Avoid connection strings, account/API keys — they bypass Entra audit and rotation.
- Local dev:
DefaultAzureCredentialworks as-is.- Production: set
AZURE_TOKEN_CREDENTIALS=prod(orAZURE_TOKEN_CREDENTIALS=<specific_credential>) to constrain the credential chain to production-safe credentials.- Wrap credentials and clients in context managers when they own token caches / transports:
- Sync:
with DefaultAzureCredential() as credential:- Async:
async with DefaultAzureCredential() as credential:(fromazure.identity.aio)Snippets may abbreviate this setup, but production code should always follow both rules.
The recommended credential for most scenarios. Tries multiple authentication methods in order:
See DefaultAzureCredential overview for the current credential chain order and defaults.
| Parameter | Default | Effect |
|---|---|---|
exclude_environment_credential | False | Skip env-var-based auth |
exclude_workload_identity_credential | False | Skip Kubernetes workload identity |
exclude_managed_identity_credential | False | Skip managed identity |
exclude_shared_token_cache_credential | False | Skip shared token cache |
exclude_visual_studio_code_credential | False | Skip VS Code credential |
exclude_cli_credential | False | Skip Azure CLI |
exclude_powershell_credential | False | Skip Azure PowerShell |
exclude_developer_cli_credential | False | Skip Azure Developer CLI |
exclude_interactive_browser_credential | True | Skip interactive browser |
exclude_broker_credential | False | Skip WAM broker |
Helper that wraps a credential into a callable returning a bearer token string. Essential for OpenAI SDK and other non-Azure-SDK clients:
| Credential | Use Case |
|---|---|
DefaultAzureCredential | Most scenarios — auto-detects environment |
ChainedTokenCredential | Custom credential chain with explicit ordering |
| Credential | Use Case |
|---|---|
EnvironmentCredential | Auth via AZURE_CLIENT_SECRET / AZURE_CLIENT_CERTIFICATE_PATH env vars |
ManagedIdentityCredential | Azure VMs, App Service, Functions, AKS, Arc, Service Fabric |
WorkloadIdentityCredential | Kubernetes with Microsoft Entra Workload ID |
| Credential | Use Case |
|---|---|
ClientSecretCredential | Service principal with client secret |
CertificateCredential | Service principal with PEM/PKCS12 certificate |
ClientAssertionCredential | Service principal with signed JWT assertion |
AzurePipelinesCredential | Azure Pipelines with workload identity federation |
OnBehalfOfCredential | Middle-tier on-behalf-of flow (delegated user identity) |
| Credential | Use Case |
|---|---|
InteractiveBrowserCredential | Interactive browser OAuth sign-in |
DeviceCodeCredential | Headless/SSH device code flow |
AuthorizationCodeCredential | Previously obtained authorization code |
| Credential | Use Case |
|---|---|
AzureCliCredential | az login |
AzureDeveloperCliCredential | azd auth login |
AzurePowerShellCredential | Connect-AzAccount |
VisualStudioCodeCredential | VS Code Azure Resources extension |
For Azure-hosted resources (VMs, App Service, Functions, AKS):
Note: The class is
CertificateCredential, NOTClientCertificateCredential.
Custom credential chain:
For Azure Kubernetes Service with workload identity:
For headless devices (IoT, SSH, CLI tools):
For interactive OAuth browser sign-in:
For middle-tier services propagating user identity:
For Azure DevOps pipelines with workload identity federation:
Async credentials are in azure.identity.aio. Always close them or use async with:
The async
get_bearer_token_provideris atazure.identity.aio.get_bearer_token_provider.
Use AzureAuthorityHosts or the AZURE_AUTHORITY_HOST env var:
| Constant | Authority |
|---|---|
AzureAuthorityHosts.AZURE_PUBLIC_CLOUD | login.microsoftonline.com (default) |
AzureAuthorityHosts.AZURE_GOVERNMENT | login.microsoftonline.us |
AzureAuthorityHosts.AZURE_CHINA | login.chinacloudapi.cn |
Opt-in disk-based caching with TokenCachePersistenceOptions:
Storage: Windows (DPAPI), macOS (Keychain), Linux (Keyring).
Allow token acquisition for additional tenants beyond the configured one:
Enable authentication logging for debugging:
| Environment | Recommended Credential |
|---|---|
| Local Development | DefaultAzureCredential (uses Azure CLI) |
| Azure App Service | DefaultAzureCredential (uses Managed Identity) |
| Azure Functions | DefaultAzureCredential (uses Managed Identity) |
| Azure Kubernetes Service | WorkloadIdentityCredential |
| Azure VMs | DefaultAzureCredential (uses Managed Identity) |
| CI/CD Pipeline | EnvironmentCredential or AzurePipelinesCredential |
| Desktop App | InteractiveBrowserCredential |
| CLI / Headless Tool | DeviceCodeCredential |
| Middle-tier Service | OnBehalfOfCredential |
azure.xxx sync clients with azure.xxx.aio async clients in the same call path. Choose one mode per module.with DefaultAzureCredential() as credential:) when they own token caches / HTTP transports you want cleaned up; for async, use async with on credentials from azure.identity.aio.DefaultAzureCredential for code that runs locally. Use a specific token credential for code that runs in Azure.get_bearer_token_provider for non-Azure-SDK clients (OpenAI, REST APIs)ChainedTokenCredential when you need a custom credential orderAZURE_CLIENT_ID for user-assigned managed identities (object ID and resource ID are also valid identifiers)DefaultAzureCredential authenticationCertificateCredential (not ClientCertificateCredential — that name doesn't exist)cache_persistence_options for long-running services to reduce token requests| Resource | URL |
|---|---|
| PyPI Package | https://pypi.org/project/azure-identity/ |
| API Reference | https://learn.microsoft.com/python/api/azure-identity |
| GitHub Source | https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/identity/azure-identity |
| Credential Chains | https://aka.ms/azsdk/python/identity/credential-chains |
| File | Contents |
|---|---|
| references/capabilities.md | Additional non-hero capabilities, operation-group coverage, and production checklists. |
| references/non-hero-scenarios.md | Dedicated non-hero examples for secondary/advanced scenarios. |