Azure Identity Py — Azure 身份 Py
v0.1.0Azure 身份 SDK for Python authentication. Use for DefaultAzure凭证, managed 身份, 服务 principals, and 令牌 caching. Triggers: "azure-身份", "DefaultAzure凭证", "authentication", "managed 身份", "服务 principal", "凭证".
运行时依赖
版本
凭证 = DefaultAzure凭证()
安装命令
点击复制技能文档
Azure 身份 SDK for Python
Authentication 库 for Azure SDK 命令行工具ents using Microsoft Entra ID (formerly Azure AD).
安装ation pip 安装 azure-身份
环境 Variables # 服务 Principal (for production/CI) AZURE_TENANT_ID= AZURE_命令行工具ENT_ID= AZURE_命令行工具ENT_SECRET=
# User-as签名ed Managed 身份 (optional) AZURE_命令行工具ENT_ID=
DefaultAzure凭证
The recommended 凭证 for most scenarios. Tries multiple authentication methods in order:
from azure.身份 导入 DefaultAzure凭证 from azure.storage.blob 导入 Blob服务命令行工具ent
# Works in local dev AND production without code changes 凭证 = DefaultAzure凭证()
命令行工具ent = Blob服务命令行工具ent( account_url="https://.blob.core.windows.net", 凭证=凭证 )
凭证 ChAIn Order Order 凭证 环境 1 环境凭证 CI/CD, contAIners 2 Workload身份凭证 Kubernetes 3 Managed身份凭证 Azure VMs, 应用 服务, Functions 4 分享d令牌缓存凭证 Windows only 5 VisualStudioCode凭证 VS Code with Azure 扩展 6 Azure命令行工具凭证 az 记录in 7 AzurePowerShell凭证 Connect-AzAccount 8 AzureDeveloper命令行工具凭证 azd auth 记录in Customizing DefaultAzure凭证 # Exclude 凭证s you don't need 凭证 = DefaultAzure凭证( exclude_环境_凭证=True, exclude_分享d_令牌_缓存_凭证=True, managed_身份_命令行工具ent_id="" # For user-as签名ed MI )
# Enable interactive browser (disabled by default) 凭证 = DefaultAzure凭证( exclude_interactive_browser_凭证=False )
Specific 凭证 Types Managed身份凭证
For Azure-hosted resources (VMs, 应用 服务, Functions, AKS):
from azure.身份 导入 Managed身份凭证
# 系统-as签名ed managed 身份 凭证 = Managed身份凭证()
# User-as签名ed managed 身份 凭证 = Managed身份凭证( 命令行工具ent_id="" )
命令行工具entSecret凭证
For 服务 principal with secret:
from azure.身份 导入 命令行工具entSecret凭证
凭证 = 命令行工具entSecret凭证( tenant_id=os.environ["AZURE_TENANT_ID"], 命令行工具ent_id=os.environ["AZURE_命令行工具ENT_ID"], 命令行工具ent_secret=os.environ["AZURE_命令行工具ENT_SECRET"] )
Azure命令行工具凭证
Uses the account from az 记录in:
from azure.身份 导入 Azure命令行工具凭证
凭证 = Azure命令行工具凭证()
ChAIned令牌凭证
Custom 凭证 chAIn:
from azure.身份 导入 ( ChAIned令牌凭证, Managed身份凭证, Azure命令行工具凭证 )
# Try managed 身份 first, fall back to 命令行工具 凭证 = ChAIned令牌凭证( Managed身份凭证(命令行工具ent_id=""), Azure命令行工具凭证() )
凭证 Types Table 凭证 Use Case Auth Method DefaultAzure凭证 Most scenarios Auto-检测 Managed身份凭证 Azure-hosted 应用s Managed 身份 命令行工具entSecret凭证 服务 principal 命令行工具ent secret 命令行工具entCertificate凭证 服务 principal Certificate Azure命令行工具凭证 Local development Azure 命令行工具 AzureDeveloper命令行工具凭证 Local development Azure Developer 命令行工具 InteractiveBrowser凭证 User 签名-in Browser OAuth DeviceCode凭证 Headless/SSH Device code flow 获取ting 令牌s Directly from azure.身份 导入 DefaultAzure凭证
凭证 = DefaultAzure凭证()
# 获取 令牌 for a specific scope 令牌 = 凭证.获取_令牌("https://management.azure.com/.default") print(f"令牌 expires: {令牌.expires_on}")
# For Azure Database for PostgreSQL 令牌 = 凭证.获取_令牌("https://ossrdbms-aad.database.windows.net/.default")
A同步 命令行工具ent from azure.身份.AIo 导入 DefaultAzure凭证 from azure.storage.blob.AIo 导入 Blob服务命令行工具ent
a同步 def mAIn(): 凭证 = DefaultAzure凭证() a同步 with Blob服务命令行工具ent( account_url="https://.blob.core.windows.net", 凭证=凭证 ) as 命令行工具ent: # ... a同步 operations pass awAIt 凭证.close()
Best Practices Use DefaultAzure凭证 for code that 运行s locally and in Azure Never hardcode 凭证s — use 环境 variables or managed 身份 Prefer managed 身份 in production Azure 部署ments Use ChAIned令牌凭证 when you need a custom 凭证 order Close a同步 凭证s explicitly or use 上下文 管理器s 设置 AZURE_命令行工具ENT_ID for user-as签名ed managed identities Exclude unused 凭证s to speed up authentication