使用服務主體登入 Azure PowerShell

Azure 中的服務主體是非互動式帳戶,可提供應用程式、服務和自動化工具用來存取特定 Azure 資源的身分識別。 使用服務主體進行驗證是撰寫安全腳本的最佳方式,因為它們會作為安全性身分識別,其具有指派的許可權,可控管可執行哪些動作,以及可以存取哪些資源。 服務主體可協助在不使用個人用戶帳戶的情況下安全地自動化管理工作,以利更安全且可管理存取 Azure 資源。 如同其他用戶帳戶,您可以使用 Microsoft Entra 管理其許可權。 藉由只授與服務主體所需的許可權,您的自動化腳本會保持安全。

必要條件

使用服務主體登入

若要使用服務主體登入,請使用 Cmdlet 的 Connect-AzAccount ServicePrincipal 參數。 您也需要服務主體的下列資訊:

  • AppId
  • 登入認證或用來建立服務主體的憑證存取權
  • 租用戶識別碼

使用服務主體登入的方式取決於其設定為密碼型或憑證型驗證。

密碼型驗證

建立要與本節範例搭配使用的服務主體。 如需建立服務主體的詳細資訊,請參閱 使用 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

針對自動化案例,您必須從服務主體 的 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、App Service 或 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 資源的受控識別。

另請參閱