We are wondering if there's another way to authenticate the managed identity with DefaultAzureCredential?
It seems as of now there is no alternative way to do that.
managed_identity_client_id : The client ID of a user-assigned managed identity. Defaults to the value of the environment variable AZURE_CLIENT_ID, if any. If not specified, a system-assigned identity will be used.
You can refer to DefaultAzureCredential(managed_identity_client_id) and Determine client id of user-assigned managed identity at runtime?
azure - Alternative approach to specify user managed identities with DefaultAzureCredential in python SDK - Stack Overflow
How to use User-Managed Identity from Azure Cloud Function (python) in making a request to Azure KeyVault? - Stack Overflow
How to check which credential Azure python SDK class DefaultAzureCredential is using for login - Stack Overflow
How to solve 'DefaultAzureCredential failed to retrieve a token' error?
When you use a user-assigned identity, you need to specify the client_id of the MSI in ManagedIdentityCredential().
credential = ManagedIdentityCredential(client_id="xxxxxxxx")
To get the client_id, navigate to your function app -> Identity -> User assigned -> click your user-assigned identity -> copy the client_id.

Here is how you would use Managed Identity in python:
from msrestazure.azure_active_directory import MSIAuthentication
creds = MSIAuthentication()
client = SecretClient(vault_url=MyVaultUrl, credentials=creds)
For this to work however, you have to have the identity assigned to your azure function as part of the deployment. If you are using terraform, there is a provider which you can use and it accepts an identity {} block where you can specify type="UserAssigned" and identity_ids = ["id1", "id2",...]
Hi
I have deployed a docker image in my azure app service and I am using below code to connect my key vault:
credential = DefaultAzureCredential() client = SecretClient(vault_url=KV_URI, credential=credential) secret = client.get_secret(secret_name)
But I am getting this error:
ERROR:root:Error fetching secrets from key vault : DefaultAzureCredential failed to retrieve a token from the included credentials. Attempted credentials: EnvironmentCredential: EnvironmentCredential authentication unavailable. Environment variables are not fully configured. Visit https://aka.ms/azsdk/python/identity/environmentcredential/troubleshoot to troubleshoot this issue. ManagedIdentityCredential: ManagedIdentityCredential authentication unavailable, no response from the IMDS endpoint. SharedTokenCacheCredential: SharedTokenCacheCredential authentication unavailable. No accounts were found in the cache. AzureCliCredential: Please run 'az login' to set up an account AzurePowerShellCredential: PowerShell is not installed AzureDeveloperCliCredential: Azure Developer CLI could not be found. Please visit for installation instructions and then,once installed, authenticate to your Azure account using 'azd auth login'.https://aka.ms/azsdk/python/identity/environmentcredential/troubleshoothttps://aka.ms/azure-dev
I have created a user assigned managed identity, assigned that UAMI to my app service, provided 'key vault secret user' access to that UAMI.
Also, the same code and access are working on QA environment but not on UAT so I am unable to find the core problem.
Can anyone please help me with this?
Thanks