CodexBloom - Programming Q&A Platform

Azure Function App with Managed Identity scenarios to Access Key Vault: 'Forbidden' scenarios

👀 Views: 31 đŸ’Ŧ Answers: 1 📅 Created: 2025-08-27
azure function-app key-vault managed-identity C#

I'm testing a new approach and I tried several approaches but none seem to work. I'm working with a 'Forbidden' behavior when my Azure Function App tries to access Azure Key Vault using a Managed Identity. I've set up the Function App with a system-assigned managed identity, and I've also granted it `Get` permissions for secrets in the Key Vault access policies. Here's how I set it up: 1. In the Azure portal, I enabled the system-assigned managed identity for my Function App. 2. In the Key Vault's access policies, I added a new access policy for the managed identity, selecting `Get` under Secret permissions. However, when I run the following code: ```csharp public static async Task<string> GetSecretAsync(string secretName, ILogger log) { var kvUri = "https://<your-keyvault-name>.vault.azure.net/"; var credential = new DefaultAzureCredential(); var client = new SecretClient(new Uri(kvUri), credential); KeyVaultSecret secret = await client.GetSecretAsync(secretName); return secret.Value; } ``` I get the following behavior: `Azure.RequestFailedException: (403) Forbidden: This request is not authorized to perform action 'Microsoft.KeyVault/vaults/secrets/read' over the 'secrets' resource.` I've confirmed that the Key Vault URL is correct and that the secret exists. I've also tried to run the function both in the Azure portal and locally with the Azure Functions Core Tools while connecting to Azure. Still, the same scenario continues. I've double-checked the permissions and even re-created the access policy, but nothing seems to work. Any suggestions on what I might be missing or what additional configurations might be necessary to allow access to the Key Vault from my Function App? What's the best practice here? I've been using C# for about a year now. I'm open to any suggestions.