Search code examples
c#azureazure-managed-identityazure-identity

User Assigned Managed Identity: No User Assigned or Delegated Managed Identity found for specified ClientId/ResourceId/PrincipalId


I am trying to get access token using the below code.

private static async Task<string> GetJwtTokenUsingManagedIdentity(string clientId, string tennantId)
{
    string resource = "https://management.azure.com/.default";
    var credential = new DefaultAzureCredential(new DefaultAzureCredentialOptions
    {
        ManagedIdentityClientId = clientId,
    });

    var tokenRequestContext = new Azure.Core.TokenRequestContext(new[] { resource });

    var token = await credential.GetTokenAsync(tokenRequestContext);

    return token.Token;
}

But I am getting error No User Assigned or Delegated Managed Identity found for specified ClientId/ResourceId/PrincipalId. I am passing the clientId of user assigned managed Identity. Any idea what i might be missing?

On my local environment i am getting the token but based on my local account.

Below is the screenshot of Managed Identity where role has been assigned. enter image description here


Solution

  • I created a User managed identity in the Azure Portal:

    enter image description here

    Note that: In order to use the User managed identity, you need to deploy your code to any of the Azure resource (web app, function app, VMs etc) but make sure to add the User managed identity to the same resource wherein your code resides.

    Make sure to add the User managed identity to the Function app:

    enter image description here

    In my case I have created isolated Function app and deployed the below code in it:

    namespace rukfunctionapp
    {
        public class Function1
        {
            private readonly ILogger<Function1> _logger;
    
            public Function1(ILogger<Function1> logger)
            {
                _logger = logger;
            }
    
            [Function("Function1")]
            public async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequest req)
            {
                _logger.LogInformation("C# HTTP trigger function processed a request.");
    
                string clientId = "ClientID";  // Replace with your actual UMI client ID
                string tenantId = "TenantID";  // Replace with your actual tenant ID
                string jwtToken = await GetJwtTokenUsingManagedIdentity(clientId, tenantId);
    
                // Return the token in the response
                return new OkObjectResult($"JWT Token: {jwtToken}");
            }
    
            
            private static async Task<string> GetJwtTokenUsingManagedIdentity(string clientId, string tenantId)
            {
                string resource = "https://management.azure.com/.default";  
                var credential = new DefaultAzureCredential(new DefaultAzureCredentialOptions
                {
                    ManagedIdentityClientId = clientId,  
                });
    
                var tokenRequestContext = new Azure.Core.TokenRequestContext(new[] { resource });
    
                
                var token = await credential.GetTokenAsync(tokenRequestContext);
    
                return token.Token;  
            }
        }
    }
    

    enter image description here

    Now when I run the function in the Function app, I am able to successfully generate the token:

    enter image description here

    Decoded token:

    enter image description here