Lesson 1 of 3 Securing Foundry Solutions

Authenticating Without Keys Using Microsoft Entra ID and Managed Identities

This lesson explains how keyless authentication with Microsoft Entra ID, which Microsoft recommends for Foundry, works for developers, applications and service-to-service calls. API keys are shared secrets that are hard to rotate and audit.

Learning outcomes

After completing this lesson, you will be able to:

  • Explain how a client obtains and uses an Entra ID token.
  • Distinguish system-assigned from user-assigned managed identities.
  • Describe how unavoidable keys are stored in Azure Key Vault or project connections.

Microsoft Entra ID tokens

An API key proves only that the caller possesses the key. It does not say who the caller is, it grants the same full access to everyone who holds it, and it remains valid until someone rotates it. If a key is committed to a repository or copied into a script, anyone who finds it can use the service, and the logs cannot show who did. Keyless authentication replaces the shared secret with an identity: each caller, whether a developer, an application or another Azure service, signs in to Microsoft Entra ID and receives a short-lived token that names it.

This change has three benefits. Access is granted per identity through role assignments, so each caller receives only the permissions it needs and can be removed individually. Tokens expire after a short period, typically about an hour, so a leaked token is far less dangerous than a leaked key. Every request is attributed to an identity in the logs, which supports auditing.

With keyless authentication, a client obtains an access token from Microsoft Entra ID for the service's scope and sends it as a bearer token. The azure-identity library's DefaultAzureCredential tries several credential sources in order, including environment variables, workload identity, managed identity, and the developer's Azure CLI or Visual Studio Code sign-in, so the same code runs locally and in Azure.

from azure.identity import DefaultAzureCredential, get_bearer_token_provider
from azure.ai.projects import AIProjectClient
from openai import OpenAI

credential = DefaultAzureCredential()

# Foundry SDK: authenticates with the project endpoint
project = AIProjectClient(endpoint=project_endpoint, credential=credential)

# OpenAI client on the v1 API: a token provider replaces the API key
client = OpenAI(
    base_url=f"https://{resource}.openai.azure.com/openai/v1/",
    api_key=get_bearer_token_provider(credential, "https://ai.azure.com/.default"),
)

Once all clients use Entra ID, local authentication is disabled on the Foundry resource (disableLocalAuth: true), so keys stop working even if they leak. Azure Policy can require this setting across subscriptions.

The following diagram shows how the same code authenticates in each environment, and why a leaked key no longer works.

DefaultAzureCredential tries credential sources in order, such as environment variables or workload identity, a managed identity in Azure and a developer's Azure CLI or Visual Studio Code sign-in, so the same code runs locally and in Azure. It gets a token from Microsoft Entra ID and calls Foundry with it. With local authentication disabled, a leaked API key is rejected.
DefaultAzureCredential uses the first credential source that is available, so code needs no change between a laptop and Azure. Disabling local authentication makes every key useless.

Managed identities

A managed identity is an Entra ID identity that Azure manages for a resource, with no secret for the developer to store. A system-assigned identity is tied to one resource and deleted with it; a user-assigned identity is a separate resource that can be attached to several resources and survives their deletion, which suits scale-out applications and pipelines. The hosting service (App Service, Container Apps, Functions, AKS with workload identity) acquires tokens for it automatically.

Service-to-service calls also use managed identities. The Foundry resource's identity reads from Azure AI Search and Storage for agent tools and evaluations; Azure AI Search's identity calls the Foundry embedding deployment for integrated vectorization and reads blobs for indexers. Each identity receives a role on the target resource.

The following screenshot shows the Identity page of a Foundry resource with its system-assigned managed identity turned on. Role assignments for this identity let the resource reach other services, such as a storage account or a search index, with no stored secret.

Identity page of a Foundry resource with the system-assigned managed identity status set to On
A system-assigned identity is deleted with its resource; a user-assigned identity can be shared.
Sort into groups

System- or user-assigned?

Exam skills 1.3.4

Choose the kind of managed identity for each situation.

Log in to try this activity Interactive activities are free with an account.

When keys remain

Some third-party services reached through tools accept only keys. Such keys are stored in Azure Key Vault or in a project connection, read at runtime by a managed identity, rotated regularly and never written to code, configuration files or source control.

Scenario questions

Remove the keys

Exam skills 1.3.4

Practise keyless authentication and handling the keys that remain.

Log in to try this activity Interactive activities are free with an account.
Worked example

Removing keys from an application

A web app on Azure App Service calls a Foundry model and Azure AI Search with keys stored in app settings. The security team requires that keys be eliminated and that the app work during key rotations without changes.

Enable a managed identity on the App Service, assign it Foundry User on the Foundry resource and Search Index Data Reader on the search service, change the code to use DefaultAzureCredential, remove the key settings and disable local authentication on both services. Moving the keys to Key Vault would protect them but would not eliminate them.

Check yourself

Why use DefaultAzureCredential in application code?

Show answer

The same code uses a developer's sign-in locally and the managed identity in Azure, so no keys are needed in either place.

Exam Tips

Remember for the exam

  • DefaultAzureCredential: same code with developer sign-in locally and managed identity in Azure.
  • Disable local (key) authentication once all clients use Entra ID.
  • User-assigned identity: shared across resources and independent of their lifecycle.