CodexBloom - Programming Q&A Platform

Azure Key Vault: how to to Retrieve Secrets Using Managed Identity in Azure Function

πŸ‘€ Views: 15 πŸ’¬ Answers: 1 πŸ“… Created: 2025-07-02
azure key-vault azure-function C#

I'm integrating two systems and I'm currently trying to access Azure Key Vault secrets from an Azure Function using a Managed Identity, but I'm working with a `Microsoft.Azure.Services.AppAuthentication.AzureServiceTokenProviderException`... The full behavior message states: `Unable to obtain a token from the managed identity endpoint. behavior: Invalid URI: The format of the URI could not be determined.` My Azure Function is set up with a system-assigned managed identity and I've granted it the 'Get' permission on the Key Vault's access policies. The Key Vault URI is configured correctly in the application settings as `https://my-key-vault.vault.azure.net/`. Here’s the code I’m using to retrieve the secret: ```csharp using Microsoft.Azure.KeyVault; using Microsoft.Azure.Services.AppAuthentication; public static async Task<string> GetSecret(string secretName) { var azureServiceTokenProvider = new AzureServiceTokenProvider(); var keyVaultClient = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(azureServiceTokenProvider.KeyVaultTokenCallback)); var secret = await keyVaultClient.GetSecretAsync("https://my-key-vault.vault.azure.net/secrets/" + secretName); return secret.Value; } ``` I’ve double-checked that the Azure Function is assigned the correct identity and that it has permission to access the Key Vault. I've also ensured that the Key Vault firewall allows access from the Azure Function's IP, but I’m still exploring with this exception. I've tried redeploying the Azure Function and even reconfiguring the Key Vault access policies but nothing seems to work. Any insights on what could be missing or misconfigured? This is happening in both development and production on Debian. Hoping someone can shed some light on this.