Secure Azure Functions with Managed Identity and Key Vault: A Developer Guide

BEYONDTHEBYTES · AZURE DEVELOPER GUIDE

Secure Azure Functions with Managed Identity and Azure Key Vault: A Developer Guide

Azure Functions often need credentials, connection strings, certificates or API keys to communicate with other services. Keeping these values in source code or plain configuration files creates unnecessary risk. This guide explains how to use Microsoft Entra managed identities and Azure Key Vault to protect application secrets in a .NET Azure Functions project.

Why avoid hard-coded secrets?

A secret committed to Git can remain in repository history even after it is removed from the latest version. Shared configuration files can also expose credentials to developers or deployment systems that do not need them. A safer design gives the Function App an identity and grants that identity access only to the secrets it requires.

How the authentication flow works

Azure Function App → Managed Identity → Microsoft Entra ID → Azure Key Vault → Secret value

The Function App requests a token using its managed identity. Microsoft Entra ID issues a token for the requested resource, and Key Vault checks whether that identity has permission to read the requested secret. No client secret needs to be stored in the Function’s code.

Step 1: Enable a managed identity

In the Azure portal, open your Function App, select Identity, enable the System assigned identity and save. Azure creates a service principal for that Function App in your tenant. You can also use a user-assigned managed identity when the same identity must be shared by multiple Azure resources.

You can enable a system-assigned identity with Azure CLI:

az functionapp identity assign \
  --name YOUR_FUNCTION_APP \
  --resource-group YOUR_RESOURCE_GROUP

Step 2: Store a secret in Azure Key Vault

Create or use an existing Key Vault, then add a secret. For example, a third-party service API key could be stored under the name PartnerApiKey. Do not paste real production credentials into scripts, tickets or source control.

az keyvault secret set \
  --vault-name YOUR_KEY_VAULT \
  --name PartnerApiKey \
  --value "REPLACE-WITH-A-SECRET-VALUE"

Step 3: Grant the Function identity access

For a Key Vault using Azure role-based access control, assign the Key Vault Secrets User role to the Function App identity at the narrowest practical scope. This role permits reading secret values. Avoid granting broad administrator or secrets-management permissions to an application that only needs to read secrets.

Before testing, confirm that the vault is using the intended permission model and that network rules allow the Function App to reach it. Role assignments can take a short time to become effective.

Step 4: Read the secret from C#

For a .NET isolated Azure Functions project, add the Azure Identity and Azure Security Key Vault Secrets packages. Use DefaultAzureCredential, which can use the Function App’s managed identity when deployed in Azure and developer credentials during local development.

using Azure.Identity;
using Azure.Security.KeyVault.Secrets;

var keyVaultUrl = Environment.GetEnvironmentVariable("KeyVaultUrl")
    ?? throw new InvalidOperationException("KeyVaultUrl is not configured.");

var client = new SecretClient(
    new Uri(keyVaultUrl),
    new DefaultAzureCredential());

KeyVaultSecret secret = await client.GetSecretAsync("PartnerApiKey");

string apiKey = secret.Value;

// Use apiKey to authenticate with the partner service.
// Do not write the secret value to logs.

Set KeyVaultUrl as a non-secret application setting, for example https://YOUR_KEY_VAULT.vault.azure.net/. The URL identifies the vault; the managed identity supplies authentication.

Step 5: Configure local development

When running locally, DefaultAzureCredential can use a developer identity such as Azure CLI sign-in, depending on the installed credential providers. Sign in with an account that has appropriate Key Vault access:

az login
az account show

Do not assume that local developer permissions match the Function App’s production permissions. Test the deployed app using its managed identity and least-privilege role assignment.

Alternative: Key Vault references in Function App settings

For simple configuration scenarios, Azure App Service and Azure Functions support Key Vault references in application settings. Instead of retrieving the secret in application code, configure an app setting with a Key Vault reference. The platform resolves the reference using the Function App identity, subject to the supported identity and vault configuration. This can keep application code simpler, while direct SDK access is useful when your code needs to retrieve or refresh secrets explicitly.

Production checklist

  • Prefer managed identity over stored client secrets when the Azure service supports it.
  • Grant only the Key Vault permissions the Function actually needs.
  • Keep secret values out of source control, logs, exception messages and telemetry.
  • Use separate vaults or access boundaries for development, test and production.
  • Plan secret rotation and understand how your application refreshes cached values.
  • Review Key Vault firewall, private endpoint and Function networking requirements.
  • Monitor authentication failures and Key Vault access without recording secret contents.
  • Test deployment using the real Function App identity, not only a developer account.

Common troubleshooting checks

403 Forbidden: Check the vault permission model, role assignment scope, identity selected by the Function App and whether the role assignment has propagated.

Credential unavailable: Confirm that the managed identity is enabled on the deployed Function App. For local development, sign in with an account that has access to the vault.

Network timeout: Review Key Vault firewall rules, private endpoint DNS and outbound connectivity from the Function App.

Secret not found: Verify the vault URL, secret name and whether the secret is enabled and within its validity period.

Conclusion

Managed identity and Azure Key Vault provide a practical foundation for securing secrets used by Azure Functions. Enable an identity, grant narrowly scoped access, retrieve secrets through supported SDKs or Key Vault references, and test the deployed configuration. This removes the need to embed application credentials in code and makes access easier to audit and manage.

BeyondTheBytes — practical Azure, Dynamics 365, Dataverse and software development guides.