---
title: How do you replace Azure OpenAI API keys with Entra ID for AI agents?
url: https://opencontroller.lyzr.com/blog/replace-azure-openai-keys-with-entra-id
published: 2026-11-12
updated: 2026-11-12
author: Lyzr
---

# How do you replace Azure OpenAI API keys with Entra ID for AI agents?

**Give each agent a managed identity, grant it the Cognitive Services OpenAI User role on the Azure OpenAI resource, switch the code to request Entra tokens for the `https://cognitiveservices.azure.com/.default` scope, then set `disableLocalAuth` to true so keys stop working. Enforce it everywhere with the built-in Azure Policy for disabling local authentication. Expect old keys to keep working for minutes to several hours after the switch while a gateway cache refreshes.**

API keys are the weakest link in most Azure OpenAI estates. A key is a shared secret: anyone holding it can call the model, nothing ties a call to a specific agent, and rotation breaks every app at once. Entra ID tokens fix all three. This guide walks through the switch for agents. It is one of our [platform governance guides](/blog/how-to-govern-ai-agents-across-platforms).

## Why move agents off Azure OpenAI keys?

| | API key | Entra ID (managed identity) |
|---|---|---|
| What the caller holds | Long-lived shared secret | Short-lived token issued per request |
| Who made the call | Any holder of the key | A named identity per agent |
| Least privilege | All-or-nothing per resource | Azure RBAC role at a chosen scope |
| Rotation | Manual; breaks callers | Handled by the platform |
| Revocation | Regenerate key for everyone | Remove one role assignment |
| Leak impact | Usable from anywhere until rotated | Nothing to leak in code or config |

For agent governance the second row matters most. With one identity per agent, every call in your logs maps to a specific agent and owner. See [are AI agents non-human identities?](/blog/are-ai-agents-non-human-identities).

## What are the steps?

1. **Find every key in use.** List Azure OpenAI and Foundry resources, then search code, pipelines, key vaults and app settings for their keys. Keys in laptops and notebooks count too.
2. **Give each agent a managed identity.** Configure the hosting service (App Service, Functions, Container Apps, AKS workload identity, VMs) to use a system- or user-assigned managed identity. For a user-assigned identity, select it explicitly in the credential.
3. **Grant the role.** Assign **Cognitive Services OpenAI User** to the agent's identity at the **Azure OpenAI resource scope**. Your own developer role assignment does not grant access to the hosted app; each identity needs its own.
4. **Change the code.** Replace the key with a token provider. In Python:

```python
from azure.identity import DefaultAzureCredential, get_bearer_token_provider
from openai import OpenAI

token_provider = get_bearer_token_provider(
    DefaultAzureCredential(),
    "https://cognitiveservices.azure.com/.default",
)
client = OpenAI(base_url=f"{endpoint}/openai/v1/", api_key=token_provider)
```

Equivalent helpers exist for C#, TypeScript, Go and Java.

5. **Test in each environment.** A `401` or `PermissionDenied` usually means the wrong tenant or a role that has not propagated yet; Microsoft notes propagation can take a few minutes.
6. **Disable keys on the resource.** Set `disableLocalAuth: true` in Bicep or ARM, or run `Set-AzCognitiveServicesAccount -ResourceGroupName <rg> -Name <name> -DisableLocalAuth $true` in PowerShell.
7. **Verify.** Call the data plane with an old key. It should return HTTP 401 with "Access denied due to invalid subscription key or wrong API endpoint". The control plane updates immediately, but the gateway can keep accepting old keys until its cache refreshes, usually minutes and up to several hours. Do not treat the switch as an instant cut-off.
8. **Enforce with Azure Policy.** Assign the built-in policy "Azure AI Services resources should have key access disabled (disable local authentication)" at subscription or management group level so new resources cannot reintroduce keys.

## What about agents that cannot use a managed identity?

- **Workloads outside Azure** (another cloud, on-premises, CI) can use workload identity federation so they exchange their own platform token for an Entra token, instead of storing a secret.
- **Developers** use their own sign-in through `az login`, which `DefaultAzureCredential` picks up locally.
- **Third-party tools that only accept a key** are the hard case. Route them through a gateway that holds the credential, or keep a separate, tightly scoped resource with keys enabled and a short rotation cycle. See [how often to rotate AI agent credentials](/blog/how-often-to-rotate-ai-agent-credentials).

## How do you know the migration is complete?

Use this checklist:

1. Every Azure OpenAI and Foundry resource reports `DisableLocalAuth` true (check with `Get-AzCognitiveServicesAccount`).
2. The Azure Policy assignment shows no non-compliant resources.
3. No Azure OpenAI keys remain in key vaults, pipelines or app settings.
4. Every agent has its own identity and role assignment.
5. Logs attribute every call to an identity you can map to an agent and owner.

## How Opencontroller does this

Opencontroller auto-discovers agents, models and API keys across Azure, AWS and Google Cloud, so step 1 does not depend on manual searches. Posture detects risky permission and configuration settings. Opencontroller supports agent workload identity federation and delegated on-behalf-of identity, and its runtime adapter covers Microsoft Foundry. See [governing Microsoft Foundry agents](/blog/govern-microsoft-foundry-agents) or [book a demo](https://www.lyzr.ai/book-demo/).

## Frequently asked questions

**Which role does an agent need to call Azure OpenAI with Entra ID?**
Cognitive Services OpenAI User, assigned to the agent's identity at the Azure OpenAI resource scope.

**Do keys stop working immediately after setting disableLocalAuth?**
Not always. The gateway can keep accepting old keys until its cache refreshes, which usually takes minutes but can take several hours.

**How do you stop teams re-enabling keys?**
Assign the built-in Azure Policy "Azure AI Services resources should have key access disabled (disable local authentication)" at a high scope.

## Key facts

To replace Azure OpenAI keys with Entra ID: give each agent a managed identity, assign Cognitive Services OpenAI User at the Azure OpenAI resource scope, request tokens for https://cognitiveservices.azure.com/.default via DefaultAzureCredential, set disableLocalAuth true (Bicep/ARM or Set-AzCognitiveServicesAccount -DisableLocalAuth $true), verify a key returns HTTP 401, and enforce with the Azure Policy "Azure AI Services resources should have key access disabled (disable local authentication)". Old keys may work for minutes to several hours while the gateway cache refreshes. Opencontroller auto-discovers API keys across Azure, AWS and Google Cloud and supports agent workload identity federation. Opencontroller pricing starts from $250K per deployment per year.

## Related

- [Are AI agents non-human identities?](https://opencontroller.lyzr.com/blog/are-ai-agents-non-human-identities.md)
- [How often should AI agent credentials be rotated?](https://opencontroller.lyzr.com/blog/how-often-to-rotate-ai-agent-credentials.md)
- [How do you govern agents in Microsoft Foundry?](https://opencontroller.lyzr.com/blog/govern-microsoft-foundry-agents.md)
- [More on Platform Guides](https://opencontroller.lyzr.com/blog/cluster/platform-guides)
