Use a Windows VM Managed Service Identity to access Resource Manager

Managed Service Identity is a feature of Azure Active Directory. Each of the Azure services that support managed service identity are subject to their own timeline. Make sure you review the availability status of managed identities for your resource and known issues before you begin.

This tutorial shows you how to enable Managed Service Identity for a Windows virtual machine (VM). You can then use that identity to access the Azure Resource Manager API. Managed Service Identities are automatically managed by Azure and enable you to authenticate to services that support Azure AD authentication without needing to insert credentials into your code. You learn how to:

  • Enable Managed Service Identity on a Windows VM
  • Grant your VM access to a Resource Group in Azure Resource Manager
  • Get an access token using the VM identity and use it to call Azure Resource Manager


If you're not familiar with the Managed Service Identity feature, see this overview. If you don't have an Azure account, sign up for a free account before you continue.

To perform the required resource creation and role management, your account needs "Owner" permissions at the appropriate scope (your subscription or resource group). If you need assistance with role assignment, see Use Role-Based Access Control to manage access to your Azure subscription resources.

Sign in to Azure

Sign in to the Azure portal at

Create a Windows virtual machine in a new resource group

For this tutorial, we create a new Windows VM. You can also enable Managed Service Identity on an existing VM.

  1. Click the Create a resource button found on the upper left-hand corner of the Azure portal.
  2. Select Compute, and then select Windows Server 2016 Datacenter.
  3. Enter the virtual machine information. The Username and Password created here is the credentials you use to login to the virtual machine.
  4. Choose the proper Subscription for the virtual machine in the dropdown.
  5. To select a new Resource Group in which to create your virtual machine, choose Create New. When complete, click OK.
  6. Select the size for the VM. To see more sizes, select View all or change the Supported disk type filter. On the Settings page, keep the defaults, and click OK.

    Alt image text

Enable Managed Service Identity on your VM

A VM Managed Service Identity enables you to get access tokens from Azure AD without you needing to put credentials into your code. Enabling Managed Service Identity on a VM, does two things: registers your VM with Azure Active Directory to create its managed identity, and it configures the identity on the VM.

  1. Select the Virtual Machine that you want to enable Managed Service Identity on. 
  2. On the left navigation bar click Configuration.
  3. You see Managed Service Identity. To register and enable the Managed Service Identity, select Yes, if you wish to disable it, choose No.
  4. Ensure you click Save to save the configuration.
    Alt image text

Grant your VM access to a resource group in Resource Manager

Using Managed Service Identity, your code can get access tokens to authenticate to resources that support Azure AD authentication. The Azure Resource Manager supports Azure AD authentication. First, we need to grant this VM’s identity access to a resource in Resource Manager, in this case the Resource Group in which the VM is contained.

  1. Navigate to the tab for Resource Groups.
  2. Select the specific Resource Group you created for your Windows VM.
  3. Go to Access control (IAM) in the left panel.
  4. Then Add a new role assignment for your Windows VM. Choose Role as Reader.
  5. In the next drop-down, Assign access to the resource Virtual Machine.
  6. Next, ensure the proper subscription is listed in the Subscription dropdown. And for Resource Group, select All resource groups.
  7. Finally, in Select choose your Windows VM in the dropdown and click Save.

    Alt image text

Get an access token using the VM identity and use it to call Azure Resource Manager

You will need to use PowerShell in this portion. If you don’t have installed, download it here.

  1. In the portal, navigate to Virtual Machines and go to your Windows virtual machine and in the Overview, click Connect.
  2. Enter in your Username and Password for which you added when you created the Windows VM.
  3. Now that you have created a Remote Desktop Connection with the virtual machine, open PowerShell in the remote session.
  4. Using Powershell’s Invoke-WebRequest, make a request to the local Managed Service Identity endpoint to get an access token for Azure Resource Manager.

       $response = Invoke-WebRequest -Uri '' -Method GET -Headers @{Metadata="true"}


    The value of the "resource" parameter must be an exact match for what is expected by Azure AD. When using the Azure Resource Manager resource ID, you must include the trailing slash on the URI.

    Next, extract the full response, which is stored as a JavaScript Object Notation (JSON) formatted string in the $response object.

    $content = $response.Content | ConvertFrom-Json

    Next, extract the access token from the response.

    $ArmToken = $content.access_token

    Finally, call Azure Resource Manager using the access token. In this example, we're also using PowerShell's Invoke-WebRequest to make the call to Azure Resource Manager, and include the access token in the Authorization header.

    (Invoke-WebRequest -Uri<SUBSCRIPTION ID>/resourceGroups/<RESOURCE GROUP>?api-version=2016-06-01 -Method GET -ContentType "application/json" -Headers @{ Authorization ="Bearer $ArmToken"}).content


    The URL is case-sensitive, so ensure if you are using the exact same case as you used earlier when you named the Resource Group, and the uppercase "G" in "resourceGroups."

    The following command returns the details of the Resource Group:


Next steps

In this tutorial, you learned how to create a user assigned identity and attach it to a Azure Virtual Machine to access the Azure Resource Manager API. To learn more about Azure Resource Manager see: