Skip to content

Managed Identity Support in MSAL

Neha Bhargava edited this page Mar 14, 2023 · 22 revisions

Managed Identity

A common challenge for developers is the management of secrets, credentials, certificates, and keys used to secure communication between services. Managed identities eliminate the need for developers to manage these credentials.

How to use managed identity in MSAL

System assigned managed identity

IManagedIdentityApplication mi = ManagedIdentityApplicationBuilder.Create()
                .WithExperimentalFeatures()
                .Build();

AuthenticationResult result = await mi.AcquireTokenManagedIdentity(resource) // resource to acquire token for. For example https://management.azure.com
    .ExecuteAsync()
    .ConfigureAwait(false);

User assigned managed identity

IManagedIdentityApplication mi = ManagedIdentityApplicationBuilder.Create(userAssignedId) // userAssignedId can be client id or resource id (if client id is not generated) for the user assigned managed identity
                .WithExperimentalFeatures()
                .Build();

AuthenticationResult result = await mi.AcquireTokenManagedIdentity(resource) // resource to acquire token for. For example https://management.azure.com
    .ExecuteAsync()
    .ConfigureAwait(false);

For MSAL logging see: aka.ms/msal-net-logging

Supported by MSAL

MSAL supports the following sources for managed identity

Supported:

  • App services
  • IMDS (VMs)
  • Azure Arc
  • Cloud Shell
  • Service Fabric

Troubleshooting

For failed requests, the error response contains a correlation id that can be used for further investigation. The MSAL's correlation id generated in MSAL or passed in to MSAL is different than the one returned in server error response as MSAL cannot pass the correlation id to managed identity token acquisition endpoints.

MsalServiceException Error Code: managed_identity_failed_response Error Message: An unexpected error occurred while fetching the AAD token

This exception might mean that the scope added is either not supported or is in wrong format. An example of expected scope is https://management.azure.com/.default

Getting started with MSAL.NET

Acquiring tokens

Web Apps / Web APIs / daemon apps

Desktop/Mobile apps

Advanced topics

FAQ

Other resources

Clone this wiki locally