你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

使用服务主体登录到 Azure PowerShell

Azure 中的服务主体是一个非交互帐户,它提供应用程序、服务和自动化工具用于访问特定 Azure 资源的标识。 使用服务主体进行身份验证是编写安全脚本的最佳方式,因为服务主体可以充当具有分配权限的安全标识,管理可以执行哪些操作以及可以访问哪些资源。 利用服务主体,可以在不使用个人用户帐户的情况下安全地自动执行管理任务,从而促进对 Azure 资源的更安全且更易于管理的访问。 与其他用户帐户一样,其权限通过 Microsoft Entra 进行管理。 只向服务主体授予它所需的权限可让自动化脚本保持安全。

先决条件

使用服务主体登录

若要使用服务主体进行登录,请使用 Connect-AzAccount cmdlet 的 ServicePrincipal 参数。 还需要服务主体的以下信息:

  • AppId
  • 登录凭据或对用于创建服务主体的证书的访问权限
  • 租户 ID

如何使用服务主体进行登录取决于它是为基于密码的身份验证配置的还是为基于证书的身份验证配置的。

基于密码的身份验证

创建一个要在本部分示例中使用的服务主体。 要详细了解如何创建服务主体息,请参阅使用 Azure PowerShell 创建 Azure 服务主体

$sp = New-AzADServicePrincipal -DisplayName ServicePrincipalName

注意

提供的服务主体机密存储在用户配置文件 ($env:USERPROFILE\.Azure) 中的 AzureRmContext.json 文件中。 请确保此目录受到适当的保护。

若要获取用作对象的服务主体凭据,请使用 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

对于自动化方案,你需要基于服务主体的 AppIdSecretText 创建凭据:

$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

当自动执行服务主体连接时,请使用适当的密码存储做法。

基于证书的身份验证

若要了解如何创建用于 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 访问的证书存储中。

在 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

在 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()

使用托管标识登录

托管标识是一种特殊类型的服务主体,为 Azure 服务提供自动托管标识。 使用此类型的标识不需要将凭据存储在配置或代码中来对任何支持托管标识的 Azure 服务进行身份验证。

托管标识分为两种类型:

  • 系统分配的托管标识
  • 用户分配的托管标识

托管标识提供了一种与其他 Azure 服务通信的安全方式,而无需开发者管理凭据。 使用托管标识还有助于缓解凭据泄漏的风险。

下面是托管标识在实际场景中的使用方式:

  • Azure 会自动管理托管标识使用的凭据的创建和删除。
  • 使用托管标识的 Azure 服务可以使用 Microsoft Entra 令牌安全地访问其他服务,例如 Azure 密钥保管库、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 资源的托管标识

另请参阅