你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
Azure 中的托管标识为应用程序、服务和自动化工具提供了一种安全无缝的方式来访问 Azure 资源,而无需在代码或配置中存储凭据。 与需要手动凭据管理的服务主体不同,Azure 会自动处理托管标识,并且不会公开敏感机密。 使用托管标识是编写安全自动化脚本的最佳做法,因为它简化了身份验证,并最大限度地减少了凭据泄漏的风险。 托管标识还有助于安全地自动执行管理任务,而无需依赖用户标识。 托管标识的权限通过 Microsoft Entra 进行管理,确保只有对资源的必要访问权限,从而提高安全性和可维护性。
重要
从 2025 年 9 月开始,使用 Microsoft Entra ID 用户标识登录时,Azure PowerShell 将需要多重身份验证(MFA)。 此更改可增强安全性,但可能会影响依赖于用户名和密码身份验证的自动化工作流。 有关详细信息,请参阅 自动化方案中多重身份验证对 Azure PowerShell 的影响。
先决条件
使用托管标识登录
托管标识是一种特殊类型的服务主体,为 Azure 服务提供自动托管标识。 使用此类型的标识不需要将凭据存储在配置或代码中,才能向任何支持托管标识的 Azure 服务进行身份验证。
托管标识分为两种类型:
- 系统分配的托管标识
- 用户分配的管理标识
托管标识提供了一种安全的方式来与其他 Azure 服务通信,而无需开发人员管理凭据。 它们还有助于缓解凭据泄漏的风险。
下面是托管标识在实际方案中的工作方式:
- Azure 会自动管理托管标识使用的凭据的创建和删除。
- 使用托管标识启用的 Azure 服务可以使用 Microsoft Entra 令牌安全地访问其他服务,例如 Azure Key Vault、Azure SQL 数据库、Azure Blob 存储等。
- 此标识直接在 Azure 中进行管理,无需进行额外的预配。
托管标识通过避免存储和管理凭据的需要来简化安全模型,并通过降低与处理机密相关的风险,在安全云作中扮演重要角色。
系统分配的托管标识
Azure 会自动为 Azure 服务实例(例如 Azure VM、应用服务或 Azure Functions)创建系统分配的托管标识。 删除服务实例后,Azure 会自动清理与该服务关联的凭据和标识。
以下示例使用主机环境的系统分配托管标识进行连接。 如果在具有分配的托管标识的虚拟机上执行,则它允许代码使用分配的标识登录。
Connect-AzAccount -Identity
用户分配的管理标识
用户分配的托管标识是在 Microsoft Entra 中创建和管理的标识。 它可以分配给一个或多个 Azure 服务实例。 用户分配的托管标识的生命周期独立于为其分配的服务实例进行管理。
使用用户分配的托管标识时,必须指定 AccountId 和 Identity 参数,如以下示例所示。
Connect-AzAccount -Identity -AccountId <user-assigned-identity-clientId-or-resourceId>
以下命令使用托管标识进行 myUserAssignedIdentity连接。 它将用户分配的标识添加到虚拟机,然后使用用户分配标识的 ClientId 进行连接。
$identity = Get-AzUserAssignedIdentity -ResourceGroupName myResourceGroup -Name myUserAssignedIdentity
Get-AzVM -ResourceGroupName contoso -Name testvm | Update-AzVM -IdentityType UserAssigned -IdentityId $identity.Id
Connect-AzAccount -Identity -AccountId $identity.ClientId # Run on the virtual machine
Account SubscriptionName TenantId Environment
------- ---------------- -------- -----------
00000000-0000-0000-0000-000000000000 My Subscription 00000000-0000-0000-0000-000000000000 AzureCloud
有关详细信息,请参阅 在 Azure VM 上为 Azure 资源配置托管标识。
使用服务主体登录
若要使用服务主体登录,请使用 cmdlet 的 Connect-AzAccount 参数。 还需要服务主体的以下信息:
- AppId
- 登录凭据或对用于创建服务主体的证书的访问权限
- 租户 ID
使用服务主体登录的方式取决于是针对基于证书的身份验证还是基于密码的身份验证进行配置。
基于证书的身份验证
若要了解如何为 Azure PowerShell 创建服务主体,请参阅 使用 Azure PowerShell 创建 Azure 服务主体。
基于证书的身份验证要求 Azure PowerShell 基于证书指纹从本地证书存储中检索信息。
Connect-AzAccount -ApplicationId $appId -Tenant $tenantId -CertificateThumbprint <thumbprint>
使用服务主体而不是已注册的应用程序时,请指定 ServicePrincipal 参数,并提供服务主体的 AppId 作为 ApplicationId 参数的值。
Connect-AzAccount -ServicePrincipal -ApplicationId $servicePrincipalId -Tenant $tenantId -CertificateThumbprint <thumbprint>
在 Windows PowerShell 5.1 中,可以使用 PKI 模块管理并检查证书存储。 对于 PowerShell 7.x 及更高版本,此过程不同。 以下脚本演示如何将现有证书导入到 PowerShell 可访问的证书存储中。
在 PowerShell 7.x 及更高版本中导入证书
# Import a PFX
$storeName = [System.Security.Cryptography.X509Certificates.StoreName]::My
$storeLocation = [System.Security.Cryptography.X509Certificates.StoreLocation]::CurrentUser
$store = [System.Security.Cryptography.X509Certificates.X509Store]::new($storeName, $storeLocation)
$certPath = <path to certificate>
$credentials = Get-Credential -Message "Provide PFX private key password"
$flag = [System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::Exportable
$certificate = [System.Security.Cryptography.X509Certificates.X509Certificate2]::new($certPath, $credentials.Password, $flag)
$store.Open([System.Security.Cryptography.X509Certificates.OpenFlags]::ReadWrite)
$store.Add($Certificate)
$store.Close()
在 Windows PowerShell 5.1 中导入证书
# Import a PFX
$credentials = Get-Credential -Message 'Provide PFX private key password'
Import-PfxCertificate -FilePath <path to certificate> -Password $credentials.Password -CertStoreLocation cert:\CurrentUser\My
基于密码的身份验证
创建用于本部分中示例的服务主体。 有关创建服务主体的详细信息,请参阅 使用 Azure PowerShell 创建 Azure 服务主体。
$sp = New-AzADServicePrincipal -DisplayName ServicePrincipalName
注意
所提供的服务主体机密存储在 AzureRmContext.json 用户配置文件中的文件中($env:USERPROFILE\.Azure)。 确保此目录具有适当的保护。
若要将服务主体的凭据作为对象获取,请使用 Get-Credential cmdlet。 此 cmdlet 会提示输入用户名和密码。 使用服务主体的 AppId 用户名,并将其 secret 转换为密码的纯文本。
# Retrieve the plain text password for use with Get-Credential in the next command.
$sp.PasswordCredentials.SecretText
$pscredential = Get-Credential -UserName $sp.AppId
Connect-AzAccount -ServicePrincipal -Credential $pscredential -Tenant $tenantId
对于自动化方案,需要从服务主体 AppId 的凭据和 SecretText:
$SecureStringPwd = $sp.PasswordCredentials.SecretText | ConvertTo-SecureString -AsPlainText -Force
$pscredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $sp.AppId, $SecureStringPwd
Connect-AzAccount -ServicePrincipal -Credential $pscredential -Tenant $tenantId
在自动执行服务主体连接时,请使用适当的密码存储做法。