Share via


在 Azure 虛擬機上設定受控識別 (VM)

Azure 資源的受控識別是 Microsoft Entra ID 的功能。 每個支援適用於 Azure 資源的受控識別 Azure 服務均受限於其本身的時間表。 開始之前,請務必檢閱 資源受控識別的可用性 狀態和 已知問題

資源的受控身分識別會在 Microsoft Entra ID 中為 Azure 服務提供自動受控身分識別。 您可以使用此身分識別來向任何支援 Microsoft Entra 驗證的服務進行驗證,不需要任何您程式碼中的認證。

在本文中,您將瞭解如何使用 Azure 入口網站,為 Azure 虛擬機啟用和停用系統與使用者指派的受控識別。

必要條件

  • 如果您不熟悉 Azure 資源的受控識別,請參閱概 觀一節
  • 如果您還沒有 Azure 帳戶,請先註冊免費帳戶,再繼續進行。

系統指派的受控識別

在本節中,您將瞭解如何使用 Azure 入口網站 啟用和停用 VM 的系統指派受控識別。

在建立虛擬機器時啟用系統指派的受身分控識別

若要在 VM 建立期間在 VM 上啟用系統指派的受控識別,您的帳戶需要 虛擬機參與者 角色指派。 不需要其他的 Microsoft Entra 目錄角色指派。

  • 在 [身分識別] 區段中的 [管理] 索引標籤下,將 [受控服務識別] 切換為 [開啟]。

顯示如何在 VM 建立期間啟用系統指派身分識別的螢幕快照。

請參閱下列快速入門以建立 VM:

在現有虛擬機器上啟用系統指派的受身分控識別

提示

本文中的步驟可能會根據您從開始的入口網站稍有不同。

若要在原本未布建的 VM 上啟用系統指派的受控識別,您的帳戶需要 虛擬機參與者 角色指派。 不需要其他的 Microsoft Entra 目錄角色指派。

  1. 使用與包含 VM 的 Azure 訂用帳戶相關聯的帳戶登入 Azure 入口網站

  2. 流覽至所需的虛擬機,然後選取 [ 身分識別]。

  3. 在 [系統指派] 底下選取 [狀態],然後按兩下 [儲存]:

    顯示 [身分識別(預覽)] 頁面的螢幕快照,並將 [系統指派] 狀態設定為 [開啟]。

從 VM 移除系統指派的受控識別

若要從 VM 移除系統指派的受控識別,您的帳戶需要 虛擬機參與者 角色指派。 不需要其他的 Microsoft Entra 目錄角色指派。

如果您有不再需要系統指派受控識別的虛擬機:

  1. 使用與包含 VM 的 Azure 訂用帳戶相關聯的帳戶登入 Azure 入口網站

  2. 流覽至所需的虛擬機,然後選取 [ 身分識別]。

  3. 在 [系統指派] 底下,選取 [狀態],然後選取 [關閉],然後按兩下 [儲存]:

    組態頁面的螢幕快照。

使用者指派的受控識別

在本節中,您將瞭解如何使用 Azure 入口網站,從 VM 新增和移除使用者指派的受控識別。

在建立 VM 期間指派使用者指派的身分識別

若要將使用者指派的身分識別指派給 VM,您的帳戶需要 虛擬機參與者受控識別操作員 角色指派。 不需要其他的 Microsoft Entra 目錄角色指派。

目前,Azure 入口網站 不支援在建立 VM 期間指派使用者指派的受控識別。 相反地,請參閱下列其中一個 VM 建立快速入門文章來先建立 VM,然後繼續進行下一節,以取得將使用者指派的受控識別指派給 VM 的詳細數據:

將使用者指派的受控識別指派給現有的 VM

若要將使用者指派的身分識別指派給 VM,您的帳戶需要 虛擬機參與者受控識別操作員 角色指派。 不需要其他的 Microsoft Entra 目錄角色指派。

  1. 使用與包含 VM 的 Azure 訂用帳戶相關聯的帳戶登入 Azure 入口網站

  2. 流覽至所需的 VM,然後按兩下 [身分識別]、 [使用者指派 ] 和 [ +新增]。

    此螢幕快照顯示已選取 [使用者指派] 的 [身分識別] 頁面,並醒目提示 [新增] 按鈕。

  3. 按兩下您要新增至 VM 的使用者指派身分識別,然後按兩下[ 新增]。

    顯示將使用者指派的受控識別新增至 VM 的螢幕快照。

從 VM 移除使用者指派的受控識別

若要從 VM 移除使用者指派的身分識別,您的帳戶需要 虛擬機參與者 角色指派。 不需要其他的 Microsoft Entra 目錄角色指派。

  1. 使用與包含 VM 的 Azure 訂用帳戶相關聯的帳戶登入 Azure 入口網站

  2. 瀏覽至所需的 VM,然後選取 [識別]、[使用者指派]、您要刪除的使用者指派受控識別名稱,然後按兩下 [移除] (在確認窗格中按兩下 [是] 。

    顯示如何從 VM 移除使用者指派受控識別的螢幕快照

下一步

在本文中,您會使用 Azure CLI,瞭解如何在 Azure VM 上針對 Azure 資源作業執行下列受控識別:

  • 在 Azure VM 上啟用和停用系統指派的受控識別
  • 在 Azure VM 上新增和移除使用者指派的受控識別

如果您還沒有 Azure 帳戶,請先註冊免費帳戶,再繼續進行。

必要條件

系統指派的受控識別

在本節中,您將瞭解如何使用 Azure CLI 在 Azure VM 上啟用和停用系統指派的受控識別。

在建立 Azure VM 期間啟用系統指派的受控識別

若要建立已啟用系統指派受控識別的 Azure VM,您的帳戶需要 虛擬機參與者 角色指派。 不需要其他的 Microsoft Entra 目錄角色指派。

  1. 使用 az group create,為您的 VM 及其相關資源建立內含專案和部署的資源群組。 如果您已經有想要改用的資源群組,則可以略過此步驟:

    az group create --name myResourceGroup --location westus
    
  2. 使用 az vm create 建立 VM。 下列範例會建立名為 myVM 的 VM,其中包含系統指派的受控識別,如 參數所要求 --assign-identity ,且具有指定的 --role--scope--admin-username--admin-password 參數會指定虛擬機器登入的系統管理使用者名稱和密碼帳戶。 根據您的環境更新這些值:

    az vm create --resource-group myResourceGroup --name myVM --image win2016datacenter --generate-ssh-keys --assign-identity --role contributor --scope mySubscription --admin-username azureuser --admin-password myPassword12
    

在現有的 Azure VM 上啟用系統指派的受控識別

若要在 VM 上啟用系統指派的受控識別,您的帳戶需要 虛擬機參與者 角色指派。 不需要其他的 Microsoft Entra 目錄角色指派。

  1. 如果您要在本機主控台中使用 Azure CLI,請先使用 az login 登入 Azure。 使用與包含 VM 的 Azure 訂用帳戶相關聯的帳戶。

    az login
    
  2. 使用 az vm identity assign 搭配 identity assign 命令,將系統指派的身分識別啟用至現有的 VM:

    az vm identity assign -g myResourceGroup -n myVm
    

從 Azure VM 停用系統指派的身分識別

若要在 VM 上停用系統指派的受控識別,您的帳戶需要 虛擬機參與者 角色指派。 不需要其他的 Microsoft Entra 目錄角色指派。

如果您的虛擬機不再需要系統指派的身分識別,但仍需要使用者指派的身分識別,請使用下列命令:

az vm update -n myVM -g myResourceGroup --set identity.type='UserAssigned' 

如果您有不再需要系統指派身分識別且沒有使用者指派身分識別的虛擬機,請使用下列命令:

注意

此值 none 會區分大小寫。 它必須是小寫。

az vm update -n myVM -g myResourceGroup --set identity.type="none"

使用者指派的受控識別

在本節中,您將瞭解如何使用 Azure CLI 從 Azure VM 新增和移除使用者指派的受控識別。 如果您在與 VM 不同的 RG 中建立使用者指派的受控識別。 您必須使用受控識別的 URL,將它指派給 VM。 例如:

--identities "/subscriptions/<SUBID>/resourcegroups/<RESROURCEGROUP>/providers/Microsoft.ManagedIdentity/userAssignedIdentities/<USER_ASSIGNED_ID_NAME>"

在建立 Azure VM 期間指派使用者指派的受控識別

若要在建立期間將使用者指派的身分識別指派給 VM,您的帳戶需要 虛擬機參與者受控識別操作員 角色指派。 不需要其他的 Microsoft Entra 目錄角色指派。

  1. 如果您已經有想要使用的資源群組,則可以略過此步驟。 使用 az group create 建立資源群組,以包含和部署使用者指派的受控識別。 請務必將 和 <LOCATION> 參數值取代<RESOURCE GROUP>為您自己的值。 :

    az group create --name <RESOURCE GROUP> --location <LOCATION>
    
  2. 使用 az identity create 建立使用者指派的受控識別。 -g 參數會指定建立使用者指派受控識別的資源群組,而 -n 參數會指定其名稱。

    重要

    當您建立使用者指派的受控識別時,名稱必須以字母或數字開頭,而且可能包含英數位元、連字元(-) 和底線的組合(_)。 若要讓虛擬機器或虛擬機器擴展集的指派正常運作,名稱長度上限為 24 個字元。 如需詳細資訊,請參閱 常見問題和已知問題

    az identity create -g myResourceGroup -n myUserAssignedIdentity
    

    回應包含所建立之使用者指派受控識別的詳細數據,如下所示。 在下列步驟中使用指派給使用者指派的受控識別的資源標識碼值。

    {
        "clientId": "00001111-aaaa-2222-bbbb-3333cccc4444",
        "clientSecretUrl": "https://control-westcentralus.identity.azure.net/subscriptions/<SUBSCRIPTON ID>/resourcegroups/<RESOURCE GROUP>/providers/Microsoft.ManagedIdentity/userAssignedIdentities/<myUserAssignedIdentity>/credentials?tid=5678&oid=9012&aid=00001111-aaaa-2222-bbbb-3333cccc4444",
        "id": "/subscriptions/<SUBSCRIPTON ID>/resourcegroups/<RESOURCE GROUP>/providers/Microsoft.ManagedIdentity/userAssignedIdentities/<USER ASSIGNED IDENTITY NAME>",
        "location": "westcentralus",
        "name": "<USER ASSIGNED IDENTITY NAME>",
        "principalId": "aaaaaaaa-bbbb-cccc-1111-222222222222",
        "resourceGroup": "<RESOURCE GROUP>",
        "tags": {},
        "tenantId": "aaaabbbb-0000-cccc-1111-dddd2222eeee",
        "type": "Microsoft.ManagedIdentity/userAssignedIdentities"    
    }
    
  3. 使用 az vm create 建立 VM。 下列範例會建立與新的使用者指派身分識別相關聯的 VM,如 參數所 --assign-identity 指定,具有指定的 --role--scope。 請務必將 <RESOURCE GROUP><VM NAME>、、、<PASSWORD><USER NAME><USER ASSIGNED IDENTITY NAME><ROLE><SUBSCRIPTION> 參數值取代為您自己的值。

    az vm create --resource-group <RESOURCE GROUP> --name <VM NAME> --image <SKU linux image>  --admin-username <USER NAME> --admin-password <PASSWORD> --assign-identity <USER ASSIGNED IDENTITY NAME> --role <ROLE> --scope <SUBSCRIPTION> 
    

將使用者指派的受控識別指派給現有的 Azure VM

若要將使用者指派的身分識別指派給 VM,您的帳戶需要 虛擬機參與者受控識別操作員 角色指派。 不需要其他的 Microsoft Entra 目錄角色指派。

  1. 使用 az identity create 建立使用者指派的身分識別。 參數 -g 會指定建立使用者指派身分識別的資源群組,而 -n 參數會指定其名稱。 請務必將 和 <USER ASSIGNED IDENTITY NAME> 參數值取代<RESOURCE GROUP>為您自己的值:

    重要

    目前不支援在名稱中建立具有特殊字元(亦即底線)的使用者指派受控識別。 請使用英數位元。 因此請定期回來查看是否有更新。 如需詳細資訊,請參閱常見問題集和已知問題

    az identity create -g <RESOURCE GROUP> -n <USER ASSIGNED IDENTITY NAME>
    

    回應包含所建立之使用者指派受控識別的詳細數據,如下所示。

    {
      "clientId": "00001111-aaaa-2222-bbbb-3333cccc4444",
      "clientSecretUrl": "https://control-westcentralus.identity.azure.net/subscriptions/<SUBSCRIPTON ID>/resourcegroups/<RESOURCE GROUP>/providers/Microsoft.ManagedIdentity/userAssignedIdentities/<USER ASSIGNED IDENTITY NAME>/credentials?tid=5678&oid=9012&aid=00001111-aaaa-2222-bbbb-3333cccc4444",
      "id": "/subscriptions/<SUBSCRIPTON ID>/resourcegroups/<RESOURCE GROUP>/providers/Microsoft.ManagedIdentity/userAssignedIdentities/<USER ASSIGNED IDENTITY NAME>",
      "location": "westcentralus",
      "name": "<USER ASSIGNED IDENTITY NAME>",
      "principalId": "aaaaaaaa-bbbb-cccc-1111-222222222222",
      "resourceGroup": "<RESOURCE GROUP>",
      "tags": {},
      "tenantId": "aaaabbbb-0000-cccc-1111-dddd2222eeee",
      "type": "Microsoft.ManagedIdentity/userAssignedIdentities"    
    }
    
  2. 使用 az vm identity assign 將使用者指派的身分識別指派給 VM。 請務必將 和 <VM NAME> 參數值取代<RESOURCE GROUP>為您自己的值。 <USER ASSIGNED IDENTITY NAME>是使用者指派的受控識別資源name屬性,如上一個步驟中所建立。 如果您在與 VM 不同的 RG 中建立使用者指派的受控識別。 您必須使用受控識別的 URL。

    az vm identity assign -g <RESOURCE GROUP> -n <VM NAME> --identities <USER ASSIGNED IDENTITY>
    

從 Azure VM 移除使用者指派的受控識別

若要將使用者指派的身分識別移除至 VM,您的帳戶需要 虛擬機參與者 角色指派。

如果這是指派給虛擬機的唯一使用者指派受控識別, UserAssigned 將會從識別類型值中移除。 請務必將 和 <VM NAME> 參數值取代<RESOURCE GROUP>為您自己的值。 <USER ASSIGNED IDENTITY>將會是使用者指派的身name分識別屬性,您可以在虛擬機的身分識別區段中使用 az vm identity show找到:

az vm identity remove -g <RESOURCE GROUP> -n <VM NAME> --identities <USER ASSIGNED IDENTITY>

如果您的 VM 沒有系統指派的受控識別,而且您想要從中移除所有使用者指派的身分識別,請使用下列命令:

注意

此值 none 會區分大小寫。 它必須是小寫。

az vm update -n myVM -g myResourceGroup --set identity.type="none" identity.userAssignedIdentities=null

如果您的 VM 同時具有系統指派和使用者指派的身分識別,您可以切換為僅使用系統指派來移除所有使用者指派的身分識別。 使用下列命令:

az vm update -n myVM -g myResourceGroup --set identity.type='SystemAssigned' identity.userAssignedIdentities=null 

下一步

在本文中,您會使用 PowerShell,瞭解如何在 Azure VM 上針對 Azure 資源作業執行下列受控識別。

注意

建議您使用 Azure Az PowerShell 模組來與 Azure 互動。 請參閱安裝 Azure PowerShell 以開始使用。 若要了解如何移轉至 Az PowerShell 模組,請參閱將 Azure PowerShell 從 AzureRM 移轉至 Az

必要條件

系統指派的受控識別

在本節中,我們將瞭解如何使用 Azure PowerShell 啟用和停用系統指派的受控識別。

在建立 Azure VM 期間啟用系統指派的受控識別

若要建立已啟用系統指派受控識別的 Azure VM,您的帳戶需要 虛擬機參與者 角色指派。 不需要其他的 Microsoft Entra 目錄角色指派。

  1. 請參閱下列其中一個 Azure VM 快速入門,只完成必要章節(「登入 Azure」、「建立資源群組」、「建立網路群組」、「建立 VM」)。

    當您到達 [建立 VM] 區段時,請稍微修改 New-AzVMConfig Cmdlet 語法。 請務必新增 -IdentityType SystemAssigned 參數,以布建已啟用系統指派身分識別的 VM,例如:

    $vmConfig = New-AzVMConfig -VMName myVM -IdentityType SystemAssigned ...
    

在現有的 Azure VM 上啟用系統指派的受控識別

若要在原本未布建的 VM 上啟用系統指派的受控識別,您的帳戶需要 虛擬機參與者 角色指派。 不需要其他的 Microsoft Entra 目錄角色指派。

  1. 使用 Cmdlet 擷 Get-AzVM 取 VM 屬性。 然後,若要啟用系統指派的受控識別,請使用 -IdentityType Update-AzVM Cmdlet 上的 參數:

    $vm = Get-AzVM -ResourceGroupName myResourceGroup -Name myVM
    Update-AzVM -ResourceGroupName myResourceGroup -VM $vm -IdentityType SystemAssigned
    

將 VM 系統指派的身分識別新增至群組

在 VM 上啟用系統指派的身分識別之後,您可以將它新增至群組。 下列程式會將 VM 的系統指派身分識別新增至群組。

  1. 擷取並記 ObjectIDId VM 服務主體的 [傳回值] 字段中所指定的 :

    Get-AzADServicePrincipal -displayname "myVM"
    
  2. 擷取並記 ObjectIDId 群組的 [傳回值] 字段所指定的 :

    Get-AzADGroup -searchstring "myGroup"
    
  3. 將 VM 的服務主體新增至群組:

    New-MgGroupMember -GroupId "<Id of group>" -DirectoryObjectId "<Id of VM service principal>" 
    

從 Azure VM 停用系統指派的受控識別

若要在 VM 上停用系統指派的受控識別,您的帳戶需要 虛擬機參與者 角色指派。 不需要其他的 Microsoft Entra 目錄角色指派。

如果您的虛擬機不再需要系統指派的受控識別,但仍需要使用者指派的受控識別,請使用下列 Cmdlet:

  1. 使用 Cmdlet 擷 Get-AzVM 取 VM 屬性,並將 參數設定 -IdentityTypeUserAssigned

    $vm = Get-AzVM -ResourceGroupName myResourceGroup -Name myVM
    Update-AzVm -ResourceGroupName myResourceGroup -VM $vm -IdentityType "UserAssigned" -IdentityID "/subscriptions/<SUBSCRIPTION ID>/resourcegroups/<RESROURCE GROUP>/providers/Microsoft.ManagedIdentity/userAssignedIdentities/<USER ASSIGNED IDENTITY NAME>..."
    

如果您的虛擬機不再需要系統指派的受控識別,而且它沒有使用者指派的受控識別,請使用下列命令:

$vm = Get-AzVM -ResourceGroupName myResourceGroup -Name myVM
Update-AzVm -ResourceGroupName myResourceGroup -VM $vm -IdentityType None

使用者指派的受控識別

在本節中,您將瞭解如何使用 Azure PowerShell 從 VM 新增和移除使用者指派的受控識別。

在建立期間將使用者指派的受控識別指派給 VM

若要將使用者指派的身分識別指派給 VM,您的帳戶需要 虛擬機參與者受控識別操作員 角色指派。 不需要其他的 Microsoft Entra 目錄角色指派。

  1. 請參閱下列其中一個 Azure VM 快速入門,只完成必要章節(「登入 Azure」、「建立資源群組」、「建立網路群組」、「建立 VM」)。

    當您到達 [建立 VM] 區段時,請稍微修改 New-AzVMConfig Cmdlet 語法。 -IdentityType UserAssigned新增 和 -IdentityID 參數,以使用者指派的身分識別布建 VM。 以您自己的值取代 <VM NAME><SUBSCRIPTION ID><RESROURCE GROUP><USER ASSIGNED IDENTITY NAME> 。 例如:

    $vmConfig = New-AzVMConfig -VMName <VM NAME> -IdentityType UserAssigned -IdentityID "/subscriptions/<SUBSCRIPTION ID>/resourcegroups/<RESROURCE GROUP>/providers/Microsoft.ManagedIdentity/userAssignedIdentities/<USER ASSIGNED IDENTITY NAME>..."
    

將使用者指派的受控識別指派給現有的 Azure VM

若要將使用者指派的身分識別指派給 VM,您的帳戶需要 虛擬機參與者受控識別操作員 角色指派。 不需要其他的 Microsoft Entra 目錄角色指派。

  1. 使用 New-AzUserAssignedIdentity Cmdlet 建立使用者指派的受控識別。 Id請注意輸出中的 ,因為您將在下一個步驟中需要此資訊。

    重要

    建立使用者指派的受控識別僅支援英數位元、底線和連字元 (0-9 或 a-z 或 A-Z、_ 或 -) 字元。 此外,將指派給 VM/VMSS 的名稱應該限製為 3 到 128 個字元,才能正常運作。 如需詳細資訊,請參閱常見問題集和已知問題

    New-AzUserAssignedIdentity -ResourceGroupName <RESOURCEGROUP> -Name <USER ASSIGNED IDENTITY NAME>
    
  2. 使用 Cmdlet 擷 Get-AzVM 取 VM 屬性。 然後,若要將使用者指派的受控識別指派給 Azure VM,請使用 -IdentityType Update-AzVM Cmdlet 上的 -IdentityID 參數。 參數的值-IdentityIdId 您在上一個步驟中注意到的值。 將、 <SUBSCRIPTION ID><RESROURCE GROUP><USER ASSIGNED IDENTITY NAME> 取代<VM NAME>為您自己的值。

    警告

    若要保留先前指派給 VM 的任何使用者指派受控識別,請查詢 Identity VM 物件的 屬性(例如 , $vm.Identity。 如果傳回任何使用者指派的受控識別,請在下列命令中包含它們,以及您想要指派給 VM 的新使用者指派受控識別。

    $vm = Get-AzVM -ResourceGroupName <RESOURCE GROUP> -Name <VM NAME>
    Update-AzVM -ResourceGroupName <RESOURCE GROUP> -VM $vm -IdentityType UserAssigned -IdentityID "/subscriptions/<SUBSCRIPTION ID>/resourcegroups/<RESROURCE GROUP>/providers/Microsoft.ManagedIdentity/userAssignedIdentities/<USER ASSIGNED IDENTITY NAME>"
    

從 Azure VM 移除使用者指派的受控識別

若要將使用者指派的身分識別移除至 VM,您的帳戶需要 虛擬機參與者 角色指派。

如果您的 VM 有多個使用者指派的受控識別,您可以使用下列命令來移除最後一個受控識別。 請務必將 和 <VM NAME> 參數值取代<RESOURCE GROUP>為您自己的值。 <USER ASSIGNED IDENTITY NAME>是使用者指派的受控識別名稱屬性,應該保留在 VM 上。 這項資訊可使用查詢來搜尋 Identity VM 物件的 屬性。 例如: $vm.Identity

$vm = Get-AzVm -ResourceGroupName myResourceGroup -Name myVm
Update-AzVm -ResourceGroupName myResourceGroup -VirtualMachine $vm -IdentityType UserAssigned -IdentityID <USER ASSIGNED IDENTITY NAME>

如果您的 VM 沒有系統指派的受控識別,而且您想要從中移除所有使用者指派的受控識別,請使用下列命令:

$vm = Get-AzVm -ResourceGroupName myResourceGroup -Name myVm
Update-AzVm -ResourceGroupName myResourceGroup -VM $vm -IdentityType None

如果您的 VM 同時具有系統指派和使用者指派的受控識別,您可以切換為只使用系統指派的受控識別來移除所有使用者指派的受控識別。

$vm = Get-AzVm -ResourceGroupName myResourceGroup -Name myVm
Update-AzVm -ResourceGroupName myResourceGroup -VirtualMachine $vm -IdentityType "SystemAssigned"

下一步

在本文中,您會使用 Azure Resource Manager 部署範本,瞭解如何在 Azure VM 上針對 Azure 資源作業執行下列受控識別:

必要條件

Azure 資源管理員範本

如同 Azure 入口網站 和腳本,Azure Resource Manager 範本可讓您部署 Azure 資源群組所定義的新或修改的資源。 有數個選項可用於範本編輯和部署,包括本機和入口網站型:

無論您選擇的選項為何,在初始部署和重新部署期間,範本語法都相同。 在新的或現有的 VM 上啟用系統或使用者指派的受控識別,會以相同方式完成。 此外,根據預設,Azure Resource Manager 會對 部署執行累加式更新

系統指派的受控識別

在本節中,您將使用 Azure Resource Manager 範本來啟用和停用系統指派的受控識別。

在建立 Azure VM 或現有 VM 期間啟用系統指派的受控識別

若要在 VM 上啟用系統指派的受控識別,您的帳戶需要 虛擬機參與者 角色指派。 不需要其他的 Microsoft Entra 目錄角色指派。

  1. 無論您是在本機登入 Azure 或透過 Azure 入口網站登入,都請使用與包含虛擬機器的 Azure 訂用帳戶相關聯的帳戶。

  2. 若要啟用系統指派的受控識別,請將範本載入到編輯器、在 resources 區段中找出感興趣的 Microsoft.Compute/virtualMachines 資源,然後在與 "type": "Microsoft.Compute/virtualMachines" 屬性相同的層級上新增 "identity" 屬性。 使用下列語法:

    "identity": {
        "type": "SystemAssigned"
    },
    
  3. 當您完成時,應該將下列區段新增至 resource 範本的 區段,其應該如下所示:

     "resources": [
         {
             //other resource provider properties...
             "apiVersion": "2018-06-01",
             "type": "Microsoft.Compute/virtualMachines",
             "name": "[variables('vmName')]",
             "location": "[resourceGroup().location]",
             "identity": {
                 "type": "SystemAssigned",
                 }                        
         }
     ]
    

指派 VM 系統指派受控識別的角色

在 VM 上啟用系統指派的受控識別之後,您可能會想要將角色授與它,例如 讀取者 存取建立所在的資源群組。 您可以在使用 Azure Resource Manager 範本指派 Azure 角色一文中找到詳細資訊,以協助您完成此步驟。

從 Azure VM 停用系統指派的受控識別

若要從 VM 移除系統指派的受控識別,您的帳戶需要 虛擬機參與者 角色指派。 不需要其他的 Microsoft Entra 目錄角色指派。

  1. 無論您是在本機登入 Azure 或透過 Azure 入口網站登入,都請使用與包含虛擬機器的 Azure 訂用帳戶相關聯的帳戶。

  2. 將範本 載入編輯器 ,並在 區段中找出 Microsoft.Compute/virtualMachines 感興趣的 resources 資源。 如果您的 VM 只有系統指派的受控識別,您可以將身分識別類型變更為 None來停用它。

    Microsoft.Compute/virtualMachines API 2018-06-01 版

    如果您的 VM 同時具有系統與使用者指派的受控識別,請從身分識別類型中移除 SystemAssigned ,並保留UserAssigneduserAssignedIdentities字典值。

    Microsoft.Compute/virtualMachines API 2018-06-01 版

    apiVersion如果您的 是 2017-12-01 且您的 VM 同時具有系統與使用者指派的受控識別,請從身分識別類型中移除SystemAssigned,並保留UserAssignedidentityIds使用者指派的受控識別數位。

下列範例示範如何從沒有使用者指派受控識別的 VM 中移除系統指派的受控識別:

{
    "apiVersion": "2018-06-01",
    "type": "Microsoft.Compute/virtualMachines",
    "name": "[parameters('vmName')]",
    "location": "[resourceGroup().location]",
    "identity": {
        "type": "None"
    }
}

使用者指派的受控識別

在本節中,您會使用 Azure Resource Manager 範本,將使用者指派的受控識別指派給 Azure VM。

注意

若要使用 Azure Resource Manager 範本建立使用者指派的受控識別,請參閱 建立使用者指派的受控識別

將使用者指派的受控識別指派給 Azure VM

若要將使用者指派的身分識別指派給 VM,您的帳戶需要 受控識別操作員 角色指派。 不需要其他的 Microsoft Entra 目錄角色指派。

  1. 在 元素底 resources 下,新增下列專案,將使用者指派的受控識別指派給您的 VM。 請務必將 取代 <USERASSIGNEDIDENTITY> 為您建立的使用者指派受控識別名稱。

    Microsoft.Compute/virtualMachines API 2018-06-01 版

    apiVersion如果您的 是 2018-06-01,則使用者指派的受控識別會以字典格式儲存userAssignedIdentities,而且<USERASSIGNEDIDENTITYNAME>值必須儲存在範本區段中定義的variables變數中。

     {
         "apiVersion": "2018-06-01",
         "type": "Microsoft.Compute/virtualMachines",
         "name": "[variables('vmName')]",
         "location": "[resourceGroup().location]",
         "identity": {
             "type": "userAssigned",
             "userAssignedIdentities": {
                 "[resourceID('Microsoft.ManagedIdentity/userAssignedIdentities/',variables('<USERASSIGNEDIDENTITYNAME>'))]": {}
             }
         }
     }
    

    Microsoft.Compute/virtualMachines API 2017-12-01 版

    apiVersion如果您的 是 2017-12-01,則使用者指派的受控識別會儲存在數位中identityIds,而且<USERASSIGNEDIDENTITYNAME>值必須儲存在範本區段中定義的variables變數中。

    {
        "apiVersion": "2017-12-01",
        "type": "Microsoft.Compute/virtualMachines",
        "name": "[variables('vmName')]",
        "location": "[resourceGroup().location]",
        "identity": {
            "type": "userAssigned",
            "identityIds": [
                "[resourceID('Microsoft.ManagedIdentity/userAssignedIdentities/',variables('<USERASSIGNEDIDENTITYNAME>'))]"
            ]
        }
    }
    
  2. 當您完成時,應該將下列區段新增至 resource 範本的 區段,其應該如下所示:

    Microsoft.Compute/virtualMachines API 2018-06-01 版

      "resources": [
         {
             //other resource provider properties...
             "apiVersion": "2018-06-01",
             "type": "Microsoft.Compute/virtualMachines",
             "name": "[variables('vmName')]",
             "location": "[resourceGroup().location]",
             "identity": {
                 "type": "userAssigned",
                 "userAssignedIdentities": {
                    "[resourceID('Microsoft.ManagedIdentity/userAssignedIdentities/',variables('<USERASSIGNEDIDENTITYNAME>'))]": {}
                 }
             }
         }
     ] 
    

    Microsoft.Compute/virtualMachines API 2017-12-01 版

    "resources": [
         {
             //other resource provider properties...
             "apiVersion": "2017-12-01",
             "type": "Microsoft.Compute/virtualMachines",
             "name": "[variables('vmName')]",
             "location": "[resourceGroup().location]",
             "identity": {
                 "type": "userAssigned",
                 "identityIds": [
                    "[resourceID('Microsoft.ManagedIdentity/userAssignedIdentities/',variables('<USERASSIGNEDIDENTITYNAME>'))]"
                 ]
             }
         }
    ]
    

從 Azure VM 移除使用者指派的受控識別

若要從 VM 移除使用者指派的身分識別,您的帳戶需要 虛擬機參與者 角色指派。 不需要其他的 Microsoft Entra 目錄角色指派。

  1. 無論您是在本機登入 Azure 或透過 Azure 入口網站登入,都請使用與包含虛擬機器的 Azure 訂用帳戶相關聯的帳戶。

  2. 將範本 載入編輯器 ,並在 區段中找出 Microsoft.Compute/virtualMachines 感興趣的 resources 資源。 如果您有只有使用者指派受控識別的 VM,您可以將身分識別類型變更為 None來停用它。

    下列範例示範如何從沒有系統指派受控識別的 VM 中移除所有使用者指派的受控識別:

     {
       "apiVersion": "2018-06-01",
       "type": "Microsoft.Compute/virtualMachines",
       "name": "[parameters('vmName')]",
       "location": "[resourceGroup().location]",
       "identity": {
           "type": "None"
           },
     }
    

    Microsoft.Compute/virtualMachines API 2018-06-01 版

    若要從 VM 移除單一使用者指派的受控識別,請從 useraAssignedIdentities 字典中移除它。

    如果您有系統指派的受控識別,請將它保留在 type 值下方的值 identity 中。

    Microsoft.Compute/virtualMachines API 2017-12-01 版

    若要從 VM 移除單一使用者指派的受控識別,請從 identityIds 數位中移除它。

    如果您有系統指派的受控識別,請將它保留在 type 值下方的值 identity 中。

下一步

在本文中,您將瞭解如何使用 CURL 對 Azure Resource Manager REST 端點進行呼叫,以針對 Azure VM 上的 Azure 資源作業執行下列受控識別:

  • 在 Azure VM 上啟用和停用系統指派的受控識別
  • 在 Azure VM 上新增和移除使用者指派的受控識別

如果您還沒有 Azure 帳戶,請先註冊免費帳戶,再繼續進行。

必要條件

系統指派的受控識別

在本節中,您將瞭解如何使用 CURL 在 Azure VM 上啟用和停用系統指派的受控識別,以呼叫 Azure Resource Manager REST 端點。

在建立 Azure VM 期間啟用系統指派的受控識別

若要建立已啟用系統指派受控識別的 Azure VM,您的帳戶需要 虛擬機參與者 角色指派。 不需要其他的 Microsoft Entra 目錄角色指派。

  1. 使用 az group create,為您的 VM 及其相關資源建立內含專案和部署的資源群組。 如果您已經有想要改用的資源群組,則可以略過此步驟:

    az group create --name myResourceGroup --location westus
    
  2. 為您的 VM 建立 網路介面

     az network nic create -g myResourceGroup --vnet-name myVnet --subnet mySubnet -n myNic
    
  3. 擷取持有人存取令牌,您將會在授權標頭的下一個步驟中使用 ,以系統指派的受控識別來建立 VM。

    az account get-access-token
    
  4. 使用 Azure Cloud Shell,使用 CURL 建立 VM 來呼叫 Azure Resource Manager REST 端點。 下列範例會使用系統指派的受控識別來建立名為 myVM 的 VM,如 值 "identity":{"type":"SystemAssigned"}在要求主體中識別。 將 取代 <ACCESS TOKEN> 為您在上一個步驟中所要求的持有人存取令牌 <SUBSCRIPTION ID> 和適合您環境的值。

    curl 'https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachines/myVM?api-version=2018-06-01' -X PUT -d '{"location":"westus","name":"myVM","identity":{"type":"SystemAssigned"},"properties":{"hardwareProfile":{"vmSize":"Standard_D2_v2"},"storageProfile":{"imageReference":{"sku":"2016-Datacenter","publisher":"MicrosoftWindowsServer","version":"latest","offer":"WindowsServer"},"osDisk":{"caching":"ReadWrite","managedDisk":{"storageAccountType":"StandardSSD_LRS"},"name":"myVM3osdisk","createOption":"FromImage"},"dataDisks":[{"diskSizeGB":1023,"createOption":"Empty","lun":0},{"diskSizeGB":1023,"createOption":"Empty","lun":1}]},"osProfile":{"adminUsername":"azureuser","computerName":"myVM","adminPassword":"<SECURE PASSWORD STRING>"},"networkProfile":{"networkInterfaces":[{"id":"/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Network/networkInterfaces/myNic","properties":{"primary":true}}]}}}' -H "Content-Type: application/json" -H "Authorization: Bearer <ACCESS TOKEN>"
    
    PUT https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachines/myVM?api-version=2018-06-01 HTTP/1.1
    

    要求標頭

    要求標頭 描述
    Content-Type 必要。 設定為 application/json
    授權 必要。 設定為有效的 Bearer 存取令牌。

    要求本文

      {
        "location":"westus",
        "name":"myVM",
        "identity":{
           "type":"SystemAssigned"
        },
        "properties":{
           "hardwareProfile":{
              "vmSize":"Standard_D2_v2"
           },
           "storageProfile":{
              "imageReference":{
                 "sku":"2016-Datacenter",
                 "publisher":"MicrosoftWindowsServer",
                 "version":"latest",
                 "offer":"WindowsServer"
              },
              "osDisk":{
                 "caching":"ReadWrite",
                 "managedDisk":{
                    "storageAccountType":"StandardSSD_LRS"
                 },
                 "name":"myVM3osdisk",
                 "createOption":"FromImage"
              },
              "dataDisks":[
                 {
                    "diskSizeGB":1023,
                    "createOption":"Empty",
                    "lun":0
                 },
                 {
                    "diskSizeGB":1023,
                    "createOption":"Empty",
                    "lun":1
                 }
              ]
           },
           "osProfile":{
              "adminUsername":"azureuser",
              "computerName":"myVM",
              "adminPassword":"myPassword12"
           },
           "networkProfile":{
              "networkInterfaces":[
                 {
                    "id":"/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Network/networkInterfaces/myNic",
                    "properties":{
                       "primary":true
                    }
                 }
              ]
           }
        }
     }  
    

在現有的 Azure VM 上啟用系統指派的身分識別

若要在原本未布建的 VM 上啟用系統指派的受控識別,您的帳戶需要 虛擬機參與者 角色指派。 不需要其他的 Microsoft Entra 目錄角色指派。

  1. 擷取持有人存取令牌,您將會在授權標頭的下一個步驟中使用 ,以系統指派的受控識別來建立 VM。

    az account get-access-token
    
  2. 使用下列 CURL 命令呼叫 Azure Resource Manager REST 端點,以透過名為 myVM 的 VM 值{"identity":{"type":"SystemAssigned"},在 VM 上啟用系統指派的受控識別,如要求本文中所識別。 將 取代 <ACCESS TOKEN> 為您在上一個步驟中所要求的持有人存取令牌 <SUBSCRIPTION ID> 和適合您環境的值。

    重要

    若要確保您不會刪除指派給 VM 的任何現有使用者指派受控識別,您必須使用此 CURL 命令列出使用者指派的受控識別: curl 'https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/<RESOURCE GROUP>/providers/Microsoft.Compute/virtualMachines/<VM NAME>?api-version=2018-06-01' -H "Authorization: Bearer <ACCESS TOKEN>"。 如果您有任何使用者指派的受控識別指派給回應中值中所 identity 識別的 VM,請跳至步驟 3,示範如何在 VM 上啟用系統指派的受控識別時保留使用者指派的受控識別。

    curl 'https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachines/myVM?api-version=2018-06-01' -X PATCH -d '{"identity":{"type":"SystemAssigned"}}' -H "Content-Type: application/json" -H "Authorization:Bearer <ACCESS TOKEN>"
    
    PATCH https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachines/myVM?api-version=2018-06-01 HTTP/1.1
    

    要求標頭

    要求標頭 描述
    Content-Type 必要。 設定為 application/json
    授權 必要。 設定為有效的 Bearer 存取令牌。

    要求本文

     {  
        "identity":{  
           "type":"SystemAssigned"
        }
     }
    
  3. 若要在具有現有使用者指派受控識別的 VM 上啟用系統指派的受控識別,您必須將 新增 SystemAssignedtype 值。

    例如,如果您的 VM 具有使用者指派的受控識別 ID1ID2 指派給它,而且您想要將系統指派的受控識別新增至 VM,請使用下列 CURL 呼叫。 將和 <SUBSCRIPTION ID> 取代<ACCESS TOKEN>為適合您環境的值。

    API 版本 2018-06-01 會將使用者指派的受控識別儲存在 userAssignedIdentities 字典格式的值中,而不是 identityIds API 版本 2017-12-01中使用的數位格式值。

    API 版本 2018-06-01

    curl 'https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachines/myVM?api-version=2018-06-01' -X PATCH -d '{"identity":{"type":"SystemAssigned, UserAssigned", "userAssignedIdentities":{"/subscriptions/<<SUBSCRIPTION ID>>/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/ID1":{},"/subscriptions/<SUBSCRIPTION ID>/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/ID2":{}}}}' -H "Content-Type: application/json" -H "Authorization:Bearer <ACCESS TOKEN>"
    
    PATCH https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachines/myVM?api-version=2018-06-01 HTTP/1.1
    

    要求標頭

    要求標頭 描述
    Content-Type 必要。 設定為 application/json
    授權 必要。 設定為有效的 Bearer 存取令牌。

    要求本文

     {  
        "identity":{  
           "type":"SystemAssigned, UserAssigned",
           "userAssignedIdentities":{  
              "/subscriptions/<<SUBSCRIPTION ID>>/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/ID1":{  
    
              },
              "/subscriptions/<SUBSCRIPTION ID>/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/ID2":{  
    
              }
           }
        }
     }
    

    API 版本 2017-12-01

    curl 'https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachines/myVM?api-version=2017-12-01' -X PATCH -d '{"identity":{"type":"SystemAssigned, UserAssigned", "identityIds":["/subscriptions/<<SUBSCRIPTION ID>>/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/ID1","/subscriptions/<SUBSCRIPTION ID>/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/ID2"]}}' -H "Content-Type: application/json" -H "Authorization:Bearer <ACCESS TOKEN>"
    
    PATCH https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachines/myVM?api-version=2017-12-01 HTTP/1.1
    

    要求標頭

    要求標頭 描述
    Content-Type 必要。 設定為 application/json
    授權 必要。 設定為有效的 Bearer 存取令牌。

    要求本文

     {  
        "identity":{  
           "type":"SystemAssigned, UserAssigned",
           "identityIds":[  
              "/subscriptions/<<SUBSCRIPTION ID>>/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/ID1",
              "/subscriptions/<SUBSCRIPTION ID>/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/ID2"
           ]
        }
     }
    

從 Azure VM 停用系統指派的受控識別

若要在 VM 上停用系統指派的受控識別,您的帳戶需要 虛擬機參與者 角色指派。 不需要其他的 Microsoft Entra 目錄角色指派。

  1. 擷取持有人存取令牌,您將會在授權標頭的下一個步驟中使用 ,以系統指派的受控識別來建立 VM。

    az account get-access-token
    
  2. 使用 CURL 更新 VM 以呼叫 Azure Resource Manager REST 端點,以停用系統指派的受控識別。 下列範例會停用系統指派的受控識別,如要求主體中所識別,其值 {"identity":{"type":"None"}} 來自名為 myVM 的 VM。 將 取代 <ACCESS TOKEN> 為您在上一個步驟中所要求的持有人存取令牌 <SUBSCRIPTION ID> 和適合您環境的值。

    重要

    若要確保您不會刪除指派給 VM 的任何現有使用者指派受控識別,您必須使用此 CURL 命令列出使用者指派的受控識別: curl 'https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/<RESOURCE GROUP>/providers/Microsoft.Compute/virtualMachines/<VM NAME>?api-version=2018-06-01' -H "Authorization: Bearer <ACCESS TOKEN>"。 如果您已將任何使用者指派的受控識別指派給回應中的值所 identity 識別的 VM,請跳至步驟 3,以示範如何在 VM 上停用系統指派的受控識別時保留使用者指派的受控識別。

    curl 'https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachines/myVM?api-version=2018-06-01' -X PATCH -d '{"identity":{"type":"None"}}' -H "Content-Type: application/json" -H "Authorization:Bearer <ACCESS TOKEN>"
    
    PATCH https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachines/myVM?api-version=2018-06-01 HTTP/1.1
    

    要求標頭

    要求標頭 描述
    Content-Type 必要。 設定為 application/json
    授權 必要。 設定為有效的 Bearer 存取令牌。

    要求本文

     {  
        "identity":{  
           "type":"None"
        }
     }
    

    若要從具有使用者指派受控識別的虛擬機中移除系統指派的受控識別,請在使用 API 2018-06-01 版時UserAssigned,從 {"identity":{"type:" "}} 值中移除 SystemAssigned 值和userAssignedIdentities字典值。 如果您使用 API 2017-12-01 版或更早版本,請保留 identityIds 數位。

使用者指派的受控識別

在本節中,您會瞭解如何使用 CURL 在 Azure VM 上新增和移除使用者指派的受控識別,以呼叫 Azure Resource Manager REST 端點。

在建立 Azure VM 期間指派使用者指派的受控識別

若要將使用者指派的身分識別指派給 VM,您的帳戶需要 虛擬機參與者受控識別操作員 角色指派。 不需要其他的 Microsoft Entra 目錄角色指派。

  1. 擷取持有人存取令牌,您將會在授權標頭的下一個步驟中使用 ,以系統指派的受控識別來建立 VM。

    az account get-access-token
    
  2. 為您的 VM 建立 網路介面

     az network nic create -g myResourceGroup --vnet-name myVnet --subnet mySubnet -n myNic
    
  3. 擷取持有人存取令牌,您將會在授權標頭的下一個步驟中使用 ,以系統指派的受控識別來建立 VM。

    az account get-access-token
    
  4. 使用這裡找到的指示建立使用者指派的受控識別: 建立使用者指派的受控識別

  5. 使用 CURL 建立 VM 以呼叫 Azure Resource Manager REST 端點。 下列範例會使用使用者指派的受控識別ID1,在資源群組 myResourceGroup 中建立名為 myVM 的 VM,如 值"identity":{"type":"UserAssigned"}在要求本文中識別。 將 取代 <ACCESS TOKEN> 為您在上一個步驟中所要求的持有人存取令牌 <SUBSCRIPTION ID> 和適合您環境的值。

    API 版本 2018-06-01

    curl 'https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachines/myVM?api-version=2018-06-01' -X PUT -d '{"location":"westus","name":"myVM","identity":{"type":"UserAssigned","identityIds":["/subscriptions/<SUBSCRIPTION ID>/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/ID1"]},"properties":{"hardwareProfile":{"vmSize":"Standard_D2_v2"},"storageProfile":{"imageReference":{"sku":"2016-Datacenter","publisher":"MicrosoftWindowsServer","version":"latest","offer":"WindowsServer"},"osDisk":{"caching":"ReadWrite","managedDisk":{"storageAccountType":"StandardSSD_LRS"},"name":"myVM3osdisk","createOption":"FromImage"},"dataDisks":[{"diskSizeGB":1023,"createOption":"Empty","lun":0},{"diskSizeGB":1023,"createOption":"Empty","lun":1}]},"osProfile":{"adminUsername":"azureuser","computerName":"myVM","adminPassword":"myPassword12"},"networkProfile":{"networkInterfaces":[{"id":"/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Network/networkInterfaces/myNic","properties":{"primary":true}}]}}}' -H "Content-Type: application/json" -H "Authorization: Bearer <ACCESS TOKEN>"
    
    PUT https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachines/myVM?api-version=2018-06-01 HTTP/1.1
    

    要求標頭

    要求標頭 描述
    Content-Type 必要。 設定為 application/json
    授權 必要。 設定為有效的 Bearer 存取令牌。

    要求本文

     {  
        "location":"westus",
        "name":"myVM",
        "identity":{  
           "type":"UserAssigned",
           "identityIds":[  
              "/subscriptions/<SUBSCRIPTION ID>/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/ID1"
           ]
        },
        "properties":{  
           "hardwareProfile":{  
              "vmSize":"Standard_D2_v2"
           },
           "storageProfile":{  
              "imageReference":{  
                 "sku":"2016-Datacenter",
                 "publisher":"MicrosoftWindowsServer",
                 "version":"latest",
                 "offer":"WindowsServer"
              },
              "osDisk":{  
                 "caching":"ReadWrite",
                 "managedDisk":{  
                    "storageAccountType":"StandardSSD_LRS"
                 },
                 "name":"myVM3osdisk",
                 "createOption":"FromImage"
              },
              "dataDisks":[  
                 {  
                    "diskSizeGB":1023,
                    "createOption":"Empty",
                    "lun":0
                 },
                 {  
                    "diskSizeGB":1023,
                    "createOption":"Empty",
                    "lun":1
                 }
              ]
           },
           "osProfile":{  
              "adminUsername":"azureuser",
              "computerName":"myVM",
              "adminPassword":"myPassword12"
           },
           "networkProfile":{  
              "networkInterfaces":[  
                 {  
                    "id":"/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Network/networkInterfaces/myNic",
                    "properties":{  
                       "primary":true
                    }
                 }
              ]
           }
        }
     }
    
    

    API 版本 2017-12-01

    curl 'https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachines/myVM?api-version=2017-12-01' -X PUT -d '{"location":"westus","name":"myVM","identity":{"type":"UserAssigned","identityIds":["/subscriptions/<SUBSCRIPTION ID>/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/ID1"]},"properties":{"hardwareProfile":{"vmSize":"Standard_D2_v2"},"storageProfile":{"imageReference":{"sku":"2016-Datacenter","publisher":"MicrosoftWindowsServer","version":"latest","offer":"WindowsServer"},"osDisk":{"caching":"ReadWrite","managedDisk":{"storageAccountType":"StandardSSD_LRS"},"name":"myVM3osdisk","createOption":"FromImage"},"dataDisks":[{"diskSizeGB":1023,"createOption":"Empty","lun":0},{"diskSizeGB":1023,"createOption":"Empty","lun":1}]},"osProfile":{"adminUsername":"azureuser","computerName":"myVM","adminPassword":"myPassword12"},"networkProfile":{"networkInterfaces":[{"id":"/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Network/networkInterfaces/myNic","properties":{"primary":true}}]}}}' -H "Content-Type: application/json" -H "Authorization: Bearer <ACCESS TOKEN>"
    
    PUT https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachines/myVM?api-version=2017-12-01 HTTP/1.1
    

    要求標頭

    要求標頭 描述
    Content-Type 必要。 設定為 application/json
    授權 必要。 設定為有效的 Bearer 存取令牌。

    要求本文

     {
        "location":"westus",
        "name":"myVM",
        "identity":{
           "type":"UserAssigned",
           "identityIds":[
              "/subscriptions/<SUBSCRIPTION ID>/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/ID1"
           ]
        },
        "properties":{
           "hardwareProfile":{
              "vmSize":"Standard_D2_v2"
           },
           "storageProfile":{
              "imageReference":{
                 "sku":"2016-Datacenter",
                 "publisher":"MicrosoftWindowsServer",
                 "version":"latest",
                 "offer":"WindowsServer"
              },
              "osDisk":{
                 "caching":"ReadWrite",
                 "managedDisk":{
                    "storageAccountType":"StandardSSD_LRS"
                 },
                 "name":"myVM3osdisk",
                 "createOption":"FromImage"
              },
              "dataDisks":[
                 {
                    "diskSizeGB":1023,
                    "createOption":"Empty",
                    "lun":0
                 },
                 {
                    "diskSizeGB":1023,
                    "createOption":"Empty",
                    "lun":1
                 }
              ]
           },
           "osProfile":{
              "adminUsername":"azureuser",
              "computerName":"myVM",
              "adminPassword":"myPassword12"
           },
           "networkProfile":{
              "networkInterfaces":[
                 {
                    "id":"/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Network/networkInterfaces/myNic",
                    "properties":{
                       "primary":true
                    }
                 }
              ]
           }
        }
     }
    

將使用者指派的受控識別指派給現有的 Azure VM

若要將使用者指派的身分識別指派給 VM,您的帳戶需要 虛擬機參與者受控識別操作員 角色指派。 不需要其他的 Microsoft Entra 目錄角色指派。

  1. 擷取持有人存取令牌,您將會在授權標頭的下一個步驟中使用 ,以系統指派的受控識別來建立 VM。

    az account get-access-token
    
  2. 使用這裡找到的指示建立使用者指派的受控識別: 建立使用者指派的受控識別

  3. 若要確保您不會刪除指派給 VM 的現有使用者或系統指派的受控識別,您必須使用下列 CURL 命令列出指派給 VM 的識別類型。 如果您已將受控識別指派給虛擬機擴展集,則會列在 值底下 identity

    curl 'https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/<RESOURCE GROUP>/providers/Microsoft.Compute/virtualMachines/<VM NAME>?api-version=2018-06-01' -H "Authorization: Bearer <ACCESS TOKEN>" 
    
    GET https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/<RESOURCE GROUP>/providers/Microsoft.Compute/virtualMachines/<VM NAME>?api-version=2018-06-01 HTTP/1.1
    

    要求標頭

    要求標頭 描述
    授權 必要。 設定為有效的 Bearer 存取令牌。

    如果您有任何指派給 VM 的使用者或系統指派受控識別,如回應中的 identity 值所識別,請跳至步驟 5,以示範如何在 VM 上新增使用者指派的受控識別時保留系統指派的受控識別。

  4. 如果您沒有指派給 VM 的任何使用者指派受控識別,請使用下列 CURL 命令呼叫 Azure Resource Manager REST 端點,將第一個使用者指派的受控識別指派給 VM。

    下列範例會將使用者指派的受控識別ID1指派給資源群組 myResourceGroup 中名為 myVM 的 VM。 將 取代 <ACCESS TOKEN> 為您在上一個步驟中所要求的持有人存取令牌 <SUBSCRIPTION ID> 和適合您環境的值。

    API 版本 2018-06-01

    curl 'https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachines/myVM?api-version=2018-06-01' -X PATCH -d '{"identity":{"type":"UserAssigned", "userAssignedIdentities":{"/subscriptions/<SUBSCRIPTION ID>/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/ID1":{}}}}' -H "Content-Type: application/json" -H "Authorization:Bearer <ACCESS TOKEN>"
    
    PATCH https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachines/myVM?api-version=2018-06-01 HTTP/1.1
    

    要求標頭

    要求標頭 描述
    Content-Type 必要。 設定為 application/json
    授權 必要。 設定為有效的 Bearer 存取令牌。

    要求本文

     {
        "identity":{
           "type":"UserAssigned",
           "userAssignedIdentities":{
              "/subscriptions/<SUBSCRIPTION ID>/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/ID1":{
    
              }
           }
        }
     }
    

    API 版本 2017-12-01

    curl 'https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachines/myVM?api-version=2017-12-01' -X PATCH -d '{"identity":{"type":"userAssigned", "identityIds":["/subscriptions/<SUBSCRIPTION ID>/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/ID1"]}}' -H "Content-Type: application/json" -H "Authorization:Bearer <ACCESS TOKEN>"
    
    PATCH https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachines/myVM?api-version=2017-12-01 HTTP/1.1
    

    要求標頭

    要求標頭 描述
    Content-Type 必要。 設定為 application/json
    授權 必要。 設定為有效的 Bearer 存取令牌。

    要求本文

     {
        "identity":{
           "type":"userAssigned",
           "identityIds":[
              "/subscriptions/<SUBSCRIPTION ID>/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/ID1"
           ]
        }
     }
    
  5. 如果您有指派給 VM 的現有使用者指派或系統指派的受控識別:

    API 版本 2018-06-01

    將使用者指派的受控識別新增至 userAssignedIdentities 字典值。

    例如,如果您有系統指派的受控識別和目前指派給 VM 的使用者指派受控識別 ID1 ,而且想要將使用者指派的受控識別 ID2 新增至 VM:

    curl  'https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachines/myVM?api-version=2018-06-01' -X PATCH -d '{"identity":{"type":"SystemAssigned, UserAssigned", "userAssignedIdentities":{"/subscriptions/<SUBSCRIPTION ID>/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/ID1":{},"/subscriptions/<SUBSCRIPTION ID>/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/ID2":{}}}}' -H "Content-Type: application/json" -H "Authorization:Bearer <ACCESS TOKEN>"
    
    PATCH https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachines/myVM?api-version=2018-06-01 HTTP/1.1
    

    要求標頭

    要求標頭 描述
    Content-Type 必要。 設定為 application/json
    授權 必要。 設定為有效的 Bearer 存取令牌。

    要求本文

     {
        "identity":{
           "type":"SystemAssigned, UserAssigned",
           "userAssignedIdentities":{
              "/subscriptions/<SUBSCRIPTION ID>/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/ID1":{
    
              },
              "/subscriptions/<SUBSCRIPTION ID>/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/ID2":{
    
              }
           }
        }
     }
    

    API 版本 2017-12-01

    在新增使用者指派的受控識別時,保留您想要保留在數位值中的 identityIds 使用者指派受控識別。

    例如,如果您有系統指派的受控識別和目前指派給 VM 的使用者指派受控識別 ID1 ,而且想要將使用者指派的受控識別 ID2 新增至 VM:

    curl  'https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachines/myVM?api-version=2017-12-01' -X PATCH -d '{"identity":{"type":"SystemAssigned,UserAssigned", "identityIds":["/subscriptions/<SUBSCRIPTION ID>/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/ID1","/subscriptions/<SUBSCRIPTION ID>/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/ID2"]}}' -H "Content-Type: application/json" -H "Authorization:Bearer <ACCESS TOKEN>"
    
    PATCH https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachines/myVM?api-version=2017-12-01 HTTP/1.1
    

    要求標頭

    要求標頭 描述
    Content-Type 必要。 設定為 application/json
    授權 必要。 設定為有效的 Bearer 存取令牌。

    要求本文

     {
        "identity":{
           "type":"SystemAssigned,UserAssigned",
           "identityIds":[
              "/subscriptions/<SUBSCRIPTION ID>/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/ID1",
              "/subscriptions/<SUBSCRIPTION ID>/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/ID2"
           ]
        }
     }
    

從 Azure VM 移除使用者指派的受控識別

若要將使用者指派的身分識別移除至 VM,您的帳戶需要 虛擬機參與者 角色指派。

  1. 擷取持有人存取令牌,您將會在授權標頭的下一個步驟中使用 ,以系統指派的受控識別來建立 VM。

    az account get-access-token
    
  2. 若要確保您不會刪除您想要繼續指派給 VM 或移除系統指派受控識別的任何現有使用者指派受控識別,您必須使用下列 CURL 命令列出受控識別:

    curl 'https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/<RESOURCE GROUP>/providers/Microsoft.Compute/virtualMachines/<VM NAME>?api-version=2018-06-01' -H "Authorization: Bearer <ACCESS TOKEN>"
    
    GET https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/<RESOURCE GROUP>/providers/Microsoft.Compute/virtualMachines/<VM NAME>?api-version=2018-06-01 HTTP/1.1
    

    要求標頭

    要求標頭 描述
    Content-Type 必要。 設定為 application/json
    授權 必要。 設定為有效的 Bearer 存取令牌。

    如果您已將受控識別指派給 VM,則會列在 值中的回應中 identity

    例如,如果您有使用者指派的受控識別 ID1ID2 指派給 VM,而且您只想保留 ID1 指派並保留系統指派的身分識別:

    API 版本 2018-06-01

    將 新增 null 至您要移除的使用者指派受控識別:

    curl 'https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachines/myVM?api-version=2018-06-01' -X PATCH -d '{"identity":{"type":"SystemAssigned, UserAssigned", "userAssignedIdentities":{"/subscriptions/<SUBSCRIPTION ID>/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/ID2":null}}}' -H "Content-Type: application/json" -H "Authorization:Bearer <ACCESS TOKEN>"
    
    PATCH https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachines/myVM?api-version=2018-06-01 HTTP/1.1
    

    要求標頭

    要求標頭 描述
    Content-Type 必要。 設定為 application/json
    授權 必要。 設定為有效的 Bearer 存取令牌。

    要求本文

     {
        "identity":{
           "type":"SystemAssigned, UserAssigned",
           "userAssignedIdentities":{
              "/subscriptions/<SUBSCRIPTION ID>/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/ID2":null
           }
        }
     }
    

    API 版本 2017-12-01

    只保留您要保留在陣列中的 identityIds 使用者指派受控識別:

    curl 'https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachines/myVM?api-version=2017-12-01' -X PATCH -d '{"identity":{"type":"SystemAssigned, UserAssigned", "identityIds":["/subscriptions/<SUBSCRIPTION ID>/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/ID1"]}}' -H "Content-Type: application/json" -H "Authorization:Bearer <ACCESS TOKEN>"
    
    PATCH https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachines/myVM?api-version=2017-12-01 HTTP/1.1
    

    要求標頭

    要求標頭 描述
    Content-Type 必要。 設定為 application/json
    授權 必要。 設定為有效的 Bearer 存取令牌。

    要求本文

     {
        "identity":{
           "type":"SystemAssigned, UserAssigned",
           "identityIds":[
              "/subscriptions/<SUBSCRIPTION ID>/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/ID1"
           ]
        }
     }
    

如果您的 VM 同時具有系統指派和使用者指派的受控識別,您可以使用下列命令切換為只使用系統指派的受控識別來移除所有使用者指派的受控識別:

curl 'https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachines/myVM?api-version=2018-06-01' -X PATCH -d '{"identity":{"type":"SystemAssigned"}}' -H "Content-Type: application/json" -H "Authorization:Bearer <ACCESS TOKEN>"
PATCH https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachines/myVM?api-version=2018-06-01 HTTP/1.1

要求標頭

要求標頭 描述
Content-Type 必要。 設定為 application/json
授權 必要。 設定為有效的 Bearer 存取令牌。

要求本文

{
   "identity":{
      "type":"SystemAssigned"
   }
}

如果您的 VM 只有使用者指派的受控識別,而且您想要將其全部移除,請使用下列命令:

curl 'https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachines/myVM?api-version=2018-06-01' -X PATCH -d '{"identity":{"type":"None"}}' -H "Content-Type: application/json" -H Authorization:"Bearer <ACCESS TOKEN>"
PATCH https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachines/myVM?api-version=2018-06-01 HTTP/1.1

要求標頭

要求標頭 描述
Content-Type 必要。 設定為 application/json
授權 必要。 設定為有效的 Bearer 存取令牌。

要求本文

{
   "identity":{
      "type":"None"
   }
}

下一步

如需如何使用 REST 建立、列出或刪除使用者指派的受控識別的相關信息,請參閱:

在本文中,您將瞭解如何使用 Azure SDK 為 Azure VM 啟用和移除 Azure 資源的受控識別。

必要條件

  • 如果您不熟悉適用於 Azure 資源的受控識別功能,請參閱此概觀。 如果您沒有 Azure 帳戶,請先註冊免費帳戶,再繼續進行。

Azure 資源受控識別支援的 Azure SDK

Azure 透過一系列 Azure SDK 支援多個程式設計平台。 其中許多都已更新來支援 Azure 資源的受控識別,還提供對應的範例來示範用法。 新增其他支援時,會更新此清單:

SDK 範例
.NET 從已啟用 Azure 資源的受控識別的 VM 管理資源
Java 從已啟用 Azure 資源的受控識別的 VM 管理記憶體
Node.js 建立已啟用系統指派受控識別的 VM
Python 建立已啟用系統指派受控識別的 VM
Ruby 建立已啟用系統指派身分識別的 Azure VM

下一步

  • 請參閱設定 Azure VM 身分識別底下的相關文章,以瞭解如何使用 Azure 入口網站、PowerShell、CLI 和資源範本。