使用 REST API 呼叫在 Azure VM 上設定 Azure 資源的受控識別
Azure 資源的受控識別是 Microsoft Entra ID 的功能。 每個支援適用於 Azure 資源的受控識別 Azure 服務均受限於其本身的時間表。 開始之前,請務必檢閱 資源受控識別的可用性 狀態和 已知問題 。
Azure 資源的受控識別會在 Microsoft Entra ID 中提供自動受控系統識別的 Azure 服務。 您可以使用此身分識別來向任何支援 Microsoft Entra 驗證的服務進行驗證,不需要任何您程式碼中的認證。
在本文中,您將瞭解如何使用 CURL 對 Azure Resource Manager REST 端點進行呼叫,以針對 Azure VM 上的 Azure 資源作業執行下列受控識別:
- 在 Azure VM 上啟用和停用系統指派的受控識別
- 在 Azure VM 上新增和移除使用者指派的受控識別
如果您還沒有 Azure 帳戶, 請先註冊免費帳戶 ,再繼續進行。
必要條件
- 如果您不熟悉 Azure 資源的受控識別,請參閱 什麼是 Azure 資源的受控識別? 。 若要瞭解系統指派和使用者指派的受控識別類型,請參閱 受控識別類型 。
在 Azure Cloud Shell 中使用 Bash 環境。 如需詳細資訊,請參閱 Azure Cloud Shell 中的 Bash 快速入門。
若要在本地執行 CLI 參考命令,請安裝 Azure CLI。 若您在 Windows 或 macOS 上執行,請考慮在 Docker 容器中執行 Azure CLI。 如需詳細資訊,請參閱〈如何在 Docker 容器中執行 Azure CLI〉。
如果您使用的是本機安裝,請使用 az login 命令,透過 Azure CLI 來登入。 請遵循您終端機上顯示的步驟,完成驗證程序。 如需其他登入選項,請參閱使用 Azure CLI 登入。
出現提示時,請在第一次使用時安裝 Azure CLI 延伸模組。 如需擴充功能詳細資訊,請參閱使用 Azure CLI 擴充功能。
執行 az version 以尋找已安裝的版本和相依程式庫。 若要升級至最新版本,請執行 az upgrade。
系統指派的受控識別
在本節中,您將瞭解如何使用 CURL 在 Azure VM 上啟用和停用系統指派的受控識別,以呼叫 Azure Resource Manager REST 端點。
在建立 Azure VM 期間啟用系統指派的受控識別
若要建立已啟用系統指派受控識別的 Azure VM,您的帳戶需要 虛擬機器參與者 角色指派。 不需要其他的 Microsoft Entra 目錄角色指派。
使用 az group create ,為您的 VM 及其相關資源建立內含專案和部署的資源群組 。 如果您已經有想要改用的資源群組,則可以略過此步驟:
az group create --name myResourceGroup --location westus
為您的 VM 建立 網路介面 :
az network nic create -g myResourceGroup --vnet-name myVnet --subnet mySubnet -n myNic
擷取持有人存取權杖,您將會在授權標頭的下一個步驟中使用 ,以系統指派的受控識別來建立 VM。
az account get-access-token
使用 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 目錄角色指派。
擷取持有人存取權杖,您將會在授權標頭的下一個步驟中使用 ,以系統指派的受控識別來建立 VM。
az account get-access-token
使用下列 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" } }
若要在具有現有使用者指派受控識別的 VM 上啟用系統指派的受控識別,您必須將 新增
SystemAssigned
至type
值。例如,如果您的 VM 具有使用者指派的受控識別
ID1
並ID2
指派給它,而且您想要將系統指派的受控識別新增至 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 目錄角色指派。
擷取持有人存取權杖,您將會在授權標頭的下一個步驟中使用 ,以系統指派的受控識別來建立 VM。
az account get-access-token
使用 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 目錄角色指派。
擷取持有人存取權杖,您將會在授權標頭的下一個步驟中使用 ,以系統指派的受控識別來建立 VM。
az account get-access-token
為您的 VM 建立 網路介面 :
az network nic create -g myResourceGroup --vnet-name myVnet --subnet mySubnet -n myNic
擷取持有人存取權杖,您將會在授權標頭的下一個步驟中使用 ,以系統指派的受控識別來建立 VM。
az account get-access-token
使用這裡找到的指示建立使用者指派的受控識別: 建立使用者指派的受控識別 。
使用 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 目錄角色指派。
擷取持有人存取權杖,您將會在授權標頭的下一個步驟中使用 ,以系統指派的受控識別來建立 VM。
az account get-access-token
使用這裡找到的指示建立使用者指派的受控識別: 建立使用者指派的受控識別 。
若要確保您不會刪除指派給 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 上新增使用者指派的受控識別時保留系統指派的受控識別。如果您沒有指派給 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" ] } }
如果您有指派給 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,您的帳戶需要 虛擬機器參與者 角色指派。
擷取持有人存取權杖,您將會在授權標頭的下一個步驟中使用 ,以系統指派的受控識別來建立 VM。
az account get-access-token
若要確保您不會刪除您想要繼續指派給 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
。例如,如果您有使用者指派的受控識別
ID1
並ID2
指派給 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 建立、列出或刪除使用者指派的受控識別的相關資訊,請參閱: