Choosing Authentication Flows: On-Behalf-Of, OAuth 2.0 and API Keys
This lesson explains the authentication flows named in the skills outline and when to use each, since every hop of a request from a user through an orchestrator, agents and tools must carry an identity that decides whose permissions apply and what audit logs show.
Learning outcomes
After completing this lesson, you will be able to:
- Decide whether a call should run as the agent or as the user.
- Describe how the on-behalf-of flow implements delegated access.
- Choose OAuth 2.0 flows or API keys for third-party tools.
Acting as the agent or as the user
Every downstream call is made either as the agent (application permissions: the agent's managed identity or app registration holds the rights) or as the user (delegated permissions: the call carries the user's identity and is limited to what both the application and the user may do). Acting as the agent suits background work and data that every user may see. Acting as the user, often called user impersonation, suits data whose visibility differs by user, because the downstream system enforces the user's own permissions and audit logs record the user.
As the agent or as the user?
The on-behalf-of flow
The on-behalf-of (OBO) flow implements delegated access through a chain of services. The user signs in to the client application, which calls the agent API with an access token for that API. The agent API sends the incoming token, with its own client credentials, to Microsoft Entra ID and receives a new token for the downstream API with the user's identity. Each service can repeat the exchange for the next hop. The agent API's credential can be a certificate or a federated credential based on its managed identity, avoiding secrets.
from azure.identity import OnBehalfOfCredential
def hr_token(user_assertion: str) -> str:
cred = OnBehalfOfCredential(tenant_id=TENANT_ID, client_id=AGENT_API_CLIENT_ID,
client_assertion_func=managed_identity_assertion, # no client secret
user_assertion=user_assertion) # the token the user sent
return cred.get_token("api://hr-api/.default").token # token as the userThe following diagram shows the token exchange in the on-behalf-of flow.

The on-behalf-of exchange
OAuth 2.0 for third-party tools, and API keys
OAuth 2.0 is the standard for tools in other services. The authorization code flow (with PKCE) obtains user consent and a token for the user's data in a SaaS application; refresh tokens are stored securely by the platform. The client credentials flow authenticates service to service without a user. Foundry tool connections support managed identity, OAuth identity passthrough for MCP and OpenAPI tools, and key-based authentication.
API keys carry no user identity, grant everything the key allows and are easily leaked. They are used only when an API supports nothing else, stored in Key Vault or a Foundry connection, scoped to the minimum permissions the provider offers, rotated regularly and never placed in prompts or code. The following table summarizes the choices.
| Situation | Flow |
|---|---|
| Downstream must apply the signed-in user's permissions | On-behalf-of (delegated) |
| Background agent with no user present | Managed identity or client credentials (application) |
| Third-party SaaS data owned by the user | OAuth 2.0 authorization code with consent |
| Legacy API with no token support | API key in Key Vault, rotated |
Choosing a flow for each tool
A document agent that over-shares
An agent searches SharePoint documents with an application permission that can read all sites. Employees have received summaries of documents they cannot open themselves.
The agent acts as itself, so it sees everything. Calls to SharePoint (through Microsoft Graph or a permission-aware knowledge source) should use the on-behalf-of flow with delegated permissions, so results are trimmed to what each user may access. An instruction to respect permissions cannot work, because the agent has no knowledge of each user's rights.
An agent must read only the documents the signed-in user may see. Which flow fits?
Show answer
The on-behalf-of (OBO) flow, which uses delegated permissions so downstream services apply the user's own access.
Exam Tips
Remember for the exam
- User-specific data: delegated permissions through on-behalf-of.
- No user present: managed identity or client credentials.
- API keys: last resort, in Key Vault, minimal scope, rotated.