使用 PowerShell 將受控識別存取權指派給應用程式角色
Azure 資源的受控識別會在 Microsoft Entra ID 中提供 Azure 服務身分識別。 它們不需要程式碼中的認證即可運作。 Azure 服務會使用此身分識別向支援 Microsoft Entra 驗證的服務進行驗證。 應用程式角色提供角色型存取控制的形式,並允許服務實作授權規則。
注意
應用程式收到的權杖會由基礎結構快取,這表示受控識別角色的任何變更都可能需要相當長的時間才會生效。 如需詳細資訊,請參閱 使用受控識別進行授權 的限制。
在本文中,您將瞭解如何使用 Microsoft Graph PowerShell SDK,將受控識別指派給另一個應用程式所公開的應用程式角色。
必要條件
- 如果您不熟悉 Azure 資源的受控識別,請參閱概 觀一節 。 請務必檢閱 系統指派和使用者指派的受控識別 之間的差異。
- 如果您還沒有 Azure 帳戶, 請先註冊免費帳戶 ,再繼續進行。
- 若要執行範例腳本,您有兩個選項:
- 使用 Azure Cloud Shell ,您可以使用程式碼區塊右上角的 [試用] 按鈕來開啟 。
- 安裝最新版的 Microsoft Graph PowerShell SDK ,在本機執行腳本。
將受控識別存取權指派給另一個應用程式的應用程式角色
在 Azure 資源上啟用受控識別, 例如 Azure VM 。
尋找受控識別服務主體的物件識別碼。
針對系統指派的受控識別 ,您可以在資源的 [身分識別] 頁面上Azure 入口網站找到物件識別碼 。 您也可以使用下列 PowerShell 腳本來尋找物件識別碼。 您將需要您在步驟 1 中建立的資源資源識別碼,其可在資源的 [屬性 ] 頁面上的 [Azure 入口網站中使用。
$resourceIdWithManagedIdentity = '/subscriptions/{my subscription ID}/resourceGroups/{my resource group name}/providers/Microsoft.Compute/virtualMachines/{my virtual machine name}' (Get-AzResource -ResourceId $resourceIdWithManagedIdentity).Identity.PrincipalId
針對使用者指派的受控識別 ,您可以在資源的 [概觀] 頁面上的 [Azure 入口網站中找到受控識別的物件識別碼。 您也可以使用下列 PowerShell 腳本來尋找物件識別碼。 您將需要使用者指派受控識別的資源識別碼。
$userManagedIdentityResourceId = '/subscriptions/{my subscription ID}/resourceGroups/{my resource group name}/providers/Microsoft.ManagedIdentity/userAssignedIdentities/{my managed identity name}' (Get-AzResource -ResourceId $userManagedIdentityResourceId).Properties.PrincipalId
建立新的應用程式註冊,以代表受控識別將傳送要求的服務。 如果向受控識別公開應用程式角色授與的 API 或服務已在您的 Microsoft Entra 租使用者中有服務主體,請略過此步驟。 例如,如果您想要將受控識別存取權授與 Microsoft Graph API,您可以略過此步驟。
尋找服務應用程式服務主體的物件識別碼。 您可以使用 Azure 入口網站找到此專案。 移至 Microsoft Entra ID 並開啟 [企業應用程式 ] 頁面,然後尋找應用程式並尋找 [物件識別碼 ]。 您也可以使用下列 PowerShell 腳本,依服務主體的顯示名稱來尋找服務主體的物件識別碼:
$serverServicePrincipalObjectId = (Get-MgServicePrincipal -Filter "DisplayName eq '$applicationName'").Id
注意
應用程式的顯示名稱不是唯一的,因此您應該確認您取得正確的應用程式服務主體。
將 應用程式角色 新增至您在步驟 3 中建立的應用程式。 您可以使用 Azure 入口網站 或使用 Microsoft Graph 來建立角色。 例如,您可以在 Graph 總管上執行下列查詢來新增應用程式角色:
PATCH /applications/{id}/ { "appRoles": [ { "allowedMemberTypes": [ "User", "Application" ], "description": "Read reports", "id": "1e250995-3081-451e-866c-0f6efef9c638", "displayName": "Report reader", "isEnabled": true, "value": "report.read" } ] }
將應用程式角色指派給受控識別。 您需要下列資訊來指派應用程式角色:
managedIdentityObjectId
:您在步驟 2 中找到之受控識別服務主體的物件識別碼。serverServicePrincipalObjectId
:您在步驟 4 中找到的伺服器應用程式服務主體物件識別碼。appRoleId
:伺服器應用程式公開的應用程式角色識別碼,您在範例中步驟 5 中產生的應用程式角色識別碼為0566419e-bb95-4d9d-a4f8-ed9a0f147fa6
。
執行下列 PowerShell 命令以新增角色指派:
New-MgServicePrincipalAppRoleAssignment ` -ServicePrincipalId $serverServicePrincipalObjectId ` -PrincipalId $managedIdentityObjectId ` -ResourceId $serverServicePrincipalObjectId ` -AppRoleId $appRoleId
完成腳本
此範例腳本示範如何將 Azure Web 應用程式的受控識別指派給應用程式角色。
# Install the module.
# Install-Module Microsoft.Graph -Scope CurrentUser
# Your tenant ID (in the Azure portal, under Azure Active Directory > Overview).
$tenantID = '<tenant-id>'
# The name of your web app, which has a managed identity that should be assigned to the server app's app role.
$webAppName = '<web-app-name>'
$resourceGroupName = '<resource-group-name-containing-web-app>'
# The name of the server app that exposes the app role.
$serverApplicationName = '<server-application-name>' # For example, MyApi
# The name of the app role that the managed identity should be assigned to.
$appRoleName = '<app-role-name>' # For example, MyApi.Read.All
# Look up the web app's managed identity's object ID.
$managedIdentityObjectId = (Get-AzWebApp -ResourceGroupName $resourceGroupName -Name $webAppName).identity.principalid
Connect-MgGraph -TenantId $tenantId -Scopes 'Application.Read.All','Application.ReadWrite.All','AppRoleAssignment.ReadWrite.All','Directory.AccessAsUser.All','Directory.Read.All','Directory.ReadWrite.All'
# Look up the details about the server app's service principal and app role.
$serverServicePrincipal = (Get-MgServicePrincipal -Filter "DisplayName eq '$serverApplicationName'")
$serverServicePrincipalObjectId = $serverServicePrincipal.Id
$appRoleId = ($serverServicePrincipal.AppRoles | Where-Object {$_.Value -eq $appRoleName }).Id
# Assign the managed identity access to the app role.
New-MgServicePrincipalAppRoleAssignment `
-ServicePrincipalId $serverServicePrincipalObjectId `
-PrincipalId $managedIdentityObjectId `
-ResourceId $serverServicePrincipalObjectId `
-AppRoleId $appRoleId
下一步
- 適用于 Azure 資源的受控識別概觀
- 若要在 Azure VM 上啟用受控識別,請參閱 使用 PowerShell 為 Azure VM 上的 Azure 資源設定受控識別。