I have an azure function that connects to a keyvault using a managed identity. This is working without issue in Azure.
I have to make changes to the function but cannot get it working from visual studio 2022.
When i run code to get the DefaultAzureCredential (code below) i get an error:
var credential = new DefaultAzureCredential(new DefaultAzureCredentialOptions { ManagedIdentityClientId = userAssignedManagedIdentityClientID });
Error:
DefaultAzureCredential failed to retrieve a token from the included credentials. See the troubleshooting guide for more information. https://aka.ms/azsdk/net/identity/defaultazurecredential/troubleshoot
Its not a keyvault permissions issue, as i have not even been able to get a token from azure, before trying to access the keyvault.
What I have tried:
- I have run az login, from both powershell on my desktop and from powershell console within Visual studio.
- Sign into visual studio with an account that has access to all the resources in azure.
- Within Visual Studio > Tools > Options > Azure Service Authentication i have signed in with an account that has access to the resources.
Refer this SO answer by Dasari Kamali. For retrieving secret value in Azure Function via Visual Studio.
I agree with Gaurav Mantri try implementing :
var credential = new DefaultAzureCredential();in your code:-My user who is logged in to Visual Studio with PowerShell terminal and Profile having Key vault administrator role assigned at the Key vault level like below: -
I have enabled RBAC based access for Key vault, You can enable Access policy-based authorization and assign correct keyvault role to the user.
My Function1.cs code:-
Output:-
Visual Studio settings:-