Description
Library name and version
Azure.Identity 1.13.1
Describe the bug
Azure.Identity version 1.13.0 and 1.13.1 failing with ChainedTokenCredential
My code is using ChainedTokenCredential with ManagedIdentityCredential for deployed application and VisualStudioCredential for working locally. This has been working fine until 1.12.1 but stopped working in 1.13.0 and 1.13.1
While working locally, it is trying to use the ManagedIdentityCredential first and when it fails, the error is coming out to the surface instead of trying to use the VisualStudioCredential
Working: 1.12.1
Not working: 1.13.0 and 1.13.1
Expected behavior
We should be able to fetch the secret as ChainedTokenCredential should be successful like in version 1.12.1
Actual behavior
Exception:
Azure.Identity.AuthenticationFailedException: 'The ChainedTokenCredential failed due to an unhandled exception: ManagedIdentityCredential authentication failed: [Managed Identity] Authentication unavailable. Either the requested identity has not been assigned to this resource, or other errors could be present. Ensure the identity is correctly assigned and check the inner exception for more details. For more information, visit https://aka.ms/msal-managed-identity.
Status: BadRequest
Content:
{"error":"invalid_request","error_description":"Identity not found"}
Headers:
Server: IMDS/150.870.65.1475
x-ms-request-id: e00d2743-fdbf-4f84-a2f2-763ba9ec69ac
Date: Mon, 11 Nov 2024 06:34:38 GMT
[Managed Identity] Error Code: invalid_request Error Description: Identity not found
See the troubleshooting guide for more information. https://aka.ms/azsdk/net/identity/managedidentitycredential/troubleshoot'
Reproduction Steps
Create a simple .Net console app.
Add nuget packages:
Azure.Identity
Azure.Core
Azure.Security.KeyVault.Secrets
Add the following code:
// See https://aka.ms/new-console-template for more information
using Azure.Core;
using Azure.Identity;
using Azure.Security.KeyVault.Secrets;
Console.WriteLine("Hello, World!");
// Values from app registration
var clientId = "<<ClientID>>";
var tenantId = "<<TenantID>>";
var clientSecret = GetClientSecretFromKeyvault();
// using Azure.Identity;
var options = new ClientSecretCredentialOptions
{
AuthorityHost = AzureAuthorityHosts.AzurePublicCloud,
};
// https://learn.microsoft.com/dotnet/api/azure.identity.clientsecretcredential
var clientSecretCredential = new ClientSecretCredential(
tenantId, clientId, clientSecret, options);
static TokenCredential GetTokenCredential()
{
//ManagedIdentityCredential for Azure
//Visual Studio Credential for local development
return new ChainedTokenCredential(
new ManagedIdentityCredential(),
new VisualStudioCredential()
);
}
static string GetClientSecretFromKeyvault()
{
// Create a new secret client using the default credential from Azure.Identity using environment variables previously set,
// including AZURE_CLIENT_ID, AZURE_CLIENT_SECRET, and AZURE_TENANT_ID.
SecretClient client = GetSecretClient();
// Retrieve a secret using the secret client.
var secret = client.GetSecret("HelloSecret");
return secret.Value.Value;
}
static SecretClient GetSecretClient()
{
return new SecretClient
(vaultUri: new Uri("https://<<keyvaultname>>.vault.azure.net/"),
credential: GetTokenCredential()
);
}
Environment
Environmet: Local, using visual studio 22 version 17.11.5, using in windows
App Type: .Net 8 Console App
Metadata
Metadata
Assignees
Labels
Type
Projects
Status
Untriaged