A simple .NET console app that demonstrates how to acquire tokens using Azure Managed Identity with MSAL.NET.
The app shows both:
- System-assigned managed identity
- User-assigned managed identity
- An Azure VM (Ubuntu or Windows) with Managed Identity enabled
- VS Code with the Remote - SSH extension
Open VS Code → Press Ctrl+Shift+P → type Remote-SSH: Connect to Host → enter your VM's IP:
username@your-vm-ip
Open a folder (e.g. /home/username/) in VS Code.
Open the VS Code terminal (`Ctrl+``) and run:
sudo apt update && sudo apt install -y gitgit clone <REPO-URL> managed-identity-demo
cd managed-identity-demoThis installs .NET SDK and restores all dependencies:
chmod +x setup.sh
./setup.shdotnet run --project ManagedIdentityDemoYou should see output like:
Requesting token using Managed Identity (MSAL)...
Token acquired successfully!
Token expires on: ...
Token source: IdentityProvider
Token (first 20 chars): eyJ0eXAiOiJKV1QiLCJh...
Open VS Code → Press Ctrl+Shift+P → type Remote-SSH: Connect to Host → enter your VM's IP:
username@your-vm-ip
it will ask for the password that you have set while creating the VM. Enter the password and connect.
Open a folder (e.g. C:\Users\username\) in VS Code. Or open any folder where you want to clone the repo.
The setup script installs Git if needed, but if Git is already available:
git clone <REPO-URL> managed-identity-demo
cd managed-identity-demoIf Git is not installed, download the repo as a ZIP from GitHub and extract it.
This installs .NET SDK, Git, and restores all dependencies:
.\setup.ps1If you get an execution policy error, run first:
Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Bypass
dotnet run --project ManagedIdentityDemoDemo 2 requires a user-assigned managed identity attached to your VM. Follow these steps to create one and assign it:
- Go to the User-Assigned Managed Identities page in the Azure portal
- Click + Create
- Select your Subscription and Resource group (use the same resource group as your VM)
- Select a Region (use the same region as your VM)
- Enter a Name for the identity (e.g.
my-demo-identity) - Click Review + create → Create
- Once created, open the managed identity resource
- On the Overview page, copy the Client ID (a GUID like
xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)
- Go to your Virtual Machine in the Azure portal
- In the left menu, go to Settings → Identity
- Switch to the User assigned tab
- Click + Add
- Search for and select the managed identity you created in Step 1
- Click Add
Open ManagedIdentityDemo/Program.cs and replace the existing client ID:
var userAssignedClientId = "0350a2e9-d8dd-4d61-bfb8-c48115fbfc9e";with the Client ID you copied in Step 2.
dotnet run --project ManagedIdentityDemoBoth Demo 1 (System-Assigned) and Demo 2 (User-Assigned) should now succeed.
Demo 3 calls the Microsoft Graph API using the managed identity. For this to work, the identity needs application permissions on Microsoft Graph.
- Go to your Virtual Machine in the Azure portal
- Go to Settings → Identity → System assigned tab
- Copy the Object (principal) ID
Since application permissions on Microsoft Graph can only be granted via PowerShell/CLI (not the portal UI), open Azure Cloud Shell (PowerShell) and run:
# Install the Microsoft Graph PowerShell module if not already installed
Install-Module Microsoft.Graph -Scope CurrentUser -Force
# Connect to Graph with admin consent
Connect-MgGraph -Scopes "AppRoleAssignment.ReadWrite.All"
# Set your managed identity's Object ID (from Step 1)
$miObjectId = "<YOUR-MANAGED-IDENTITY-OBJECT-ID>"
# Get the Microsoft Graph service principal
$graphSP = Get-MgServicePrincipal -Filter "displayName eq 'Microsoft Graph'" | Select-Object -First 1
# Find the Organization.Read.All app role
$appRole = $graphSP.AppRoles | Where-Object { $_.Value -eq "Organization.Read.All" }
# Grant the permission
New-MgServicePrincipalAppRoleAssignment `
-ServicePrincipalId $miObjectId `
-PrincipalId $miObjectId `
-ResourceId $graphSP.Id `
-AppRoleId $appRole.Iddotnet run --project ManagedIdentityDemoDemo 3 should now show your tenant name, tenant ID, and verified domains from Microsoft Graph.
Note: If you get a
403 Forbiddenerror, the permission grant may take a few minutes to propagate. Wait and try again.
| Step | Description |
|---|---|
| 1 | Creates an MSAL managed identity app using ManagedIdentityApplicationBuilder |
| 2 | Requests an access token for Azure Resource Manager (Demo 1 & 2) |
| 3 | MSAL calls the Azure IMDS endpoint (169.254.169.254) on the VM |
| 4 | Prints the token details |
| 5 | Acquires a second token scoped to Microsoft Graph (Demo 3) |
| 6 | Calls GET /organization on the Graph API using the token as a Bearer header |
| 7 | Parses the JSON response and prints tenant info |
The token can be used as a Bearer token in HTTP requests to Azure APIs — no passwords or secrets needed.