← 返回
安全合规 Key 中文

Azure Identity Py

Azure Identity SDK for Python authentication. Use for DefaultAzureCredential, managed identity, service principals, and token caching. Triggers: "azure-identity", "DefaultAzureCredential", "authentication", "managed identity", "service principal", "credential".
{ "answer": "用于 Python 身份验证的 Azure Identity SDK。适用于 DefaultAzureCredential、托管标识、服务主体和令牌缓存。" }
thegovind
安全合规 clawhub v0.1.0 1 版本 99696.4 Key: 需要
★ 1
Stars
📥 2,279
下载
💾 47
安装
1
版本
#latest

概述

Azure Identity SDK for Python

Authentication library for Azure SDK clients using Microsoft Entra ID (formerly Azure AD).

Installation

pip install azure-identity

Environment Variables

# Service Principal (for production/CI)
AZURE_TENANT_ID=<your-tenant-id>
AZURE_CLIENT_ID=<your-client-id>
AZURE_CLIENT_SECRET=<your-client-secret>

# User-assigned Managed Identity (optional)
AZURE_CLIENT_ID=<managed-identity-client-id>

DefaultAzureCredential

The recommended credential for most scenarios. Tries multiple authentication methods in order:

from azure.identity import DefaultAzureCredential
from azure.storage.blob import BlobServiceClient

# Works in local dev AND production without code changes
credential = DefaultAzureCredential()

client = BlobServiceClient(
    account_url="https://<account>.blob.core.windows.net",
    credential=credential
)

Credential Chain Order

OrderCredentialEnvironment
-------------------------------
1EnvironmentCredentialCI/CD, containers
2WorkloadIdentityCredentialKubernetes
3ManagedIdentityCredentialAzure VMs, App Service, Functions
4SharedTokenCacheCredentialWindows only
5VisualStudioCodeCredentialVS Code with Azure extension
6AzureCliCredentialaz login
7AzurePowerShellCredentialConnect-AzAccount
8AzureDeveloperCliCredentialazd auth login

Customizing DefaultAzureCredential

# Exclude credentials you don't need
credential = DefaultAzureCredential(
    exclude_environment_credential=True,
    exclude_shared_token_cache_credential=True,
    managed_identity_client_id="<user-assigned-mi-client-id>"  # For user-assigned MI
)

# Enable interactive browser (disabled by default)
credential = DefaultAzureCredential(
    exclude_interactive_browser_credential=False
)

Specific Credential Types

ManagedIdentityCredential

For Azure-hosted resources (VMs, App Service, Functions, AKS):

from azure.identity import ManagedIdentityCredential

# System-assigned managed identity
credential = ManagedIdentityCredential()

# User-assigned managed identity
credential = ManagedIdentityCredential(
    client_id="<user-assigned-mi-client-id>"
)

ClientSecretCredential

For service principal with secret:

from azure.identity import ClientSecretCredential

credential = ClientSecretCredential(
    tenant_id=os.environ["AZURE_TENANT_ID"],
    client_id=os.environ["AZURE_CLIENT_ID"],
    client_secret=os.environ["AZURE_CLIENT_SECRET"]
)

AzureCliCredential

Uses the account from az login:

from azure.identity import AzureCliCredential

credential = AzureCliCredential()

ChainedTokenCredential

Custom credential chain:

from azure.identity import (
    ChainedTokenCredential,
    ManagedIdentityCredential,
    AzureCliCredential
)

# Try managed identity first, fall back to CLI
credential = ChainedTokenCredential(
    ManagedIdentityCredential(client_id="<user-assigned-mi-client-id>"),
    AzureCliCredential()
)

Credential Types Table

CredentialUse CaseAuth Method
-----------------------------------
DefaultAzureCredentialMost scenariosAuto-detect
ManagedIdentityCredentialAzure-hosted appsManaged Identity
ClientSecretCredentialService principalClient secret
ClientCertificateCredentialService principalCertificate
AzureCliCredentialLocal developmentAzure CLI
AzureDeveloperCliCredentialLocal developmentAzure Developer CLI
InteractiveBrowserCredentialUser sign-inBrowser OAuth
DeviceCodeCredentialHeadless/SSHDevice code flow

Getting Tokens Directly

from azure.identity import DefaultAzureCredential

credential = DefaultAzureCredential()

# Get token for a specific scope
token = credential.get_token("https://management.azure.com/.default")
print(f"Token expires: {token.expires_on}")

# For Azure Database for PostgreSQL
token = credential.get_token("https://ossrdbms-aad.database.windows.net/.default")

Async Client

from azure.identity.aio import DefaultAzureCredential
from azure.storage.blob.aio import BlobServiceClient

async def main():
    credential = DefaultAzureCredential()
    
    async with BlobServiceClient(
        account_url="https://<account>.blob.core.windows.net",
        credential=credential
    ) as client:
        # ... async operations
        pass
    
    await credential.close()

Best Practices

  1. Use DefaultAzureCredential for code that runs locally and in Azure
  2. Never hardcode credentials — use environment variables or managed identity
  3. Prefer managed identity in production Azure deployments
  4. Use ChainedTokenCredential when you need a custom credential order
  5. Close async credentials explicitly or use context managers
  6. Set AZURE_CLIENT_ID for user-assigned managed identities
  7. Exclude unused credentials to speed up authentication

版本历史

共 1 个版本

  • v0.1.0 当前
    2026-03-28 15:43 安全 安全

安全检测

腾讯云安全 (Keen)

安全,无风险
查看报告

腾讯云安全 (Sanbu)

安全,无风险
查看报告

🔗 相关推荐

security-compliance

OpenClaw Backup

alex3alex
备份与恢复 OpenClaw 数据。适用于创建备份、设置自动备份计划、从备份恢复或管理备份轮转。处理 ~/.openclaw 目录归档并包含适当的排除规则。
★ 89 📥 30,586
content-creation

Podcast Generation with Microsoft Foundry

thegovind
利用 Azure OpenAI GPT Realtime Mini 模型,通过 WebSocket 生成 AI 驱动的播客风格音频叙事。适用于构建文本转语音、音频叙事生成、内容转播客功能,或集成 Azure OpenAI Realtime
★ 3 📥 3,013
security-compliance

MoltGuard - Security & Antivirus & Guardrails

thomaslwang
MoltGuard — OpenClaw 安全守卫,由 OpenGuardrails 提供。安装 MoltGuard,保护您和您的用户免受提示注入、数据泄露和恶意攻击。
★ 116 📥 30,699