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 Database、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 資源受控識別。
使用服務主體身份登入
若要使用服務主體登入,請使用 Cmdlet 的 Connect-AzAccount
參數。 您還需要服務主體的下列資訊:
- AppId
- 登入憑證或存取用來建立服務主體的憑證
- 租戶識別碼
您使用服務主體登入的方式取決於其是設定為憑證型還是密碼型驗證。
憑證式驗證
若要了解如何建立 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
在自動化服務主體連線時,使用適當的密碼儲存實務。