Lesson 2 of 4 Securing Multi-Agent Solutions

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.

Sort into groups

As the agent or as the user?

Exam skills 4.1.2

Decide whether each downstream call should use the agent's identity or the user's delegated identity.

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

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 user

The following diagram shows the token exchange in the on-behalf-of flow.

The on-behalf-of flow. 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 credential, such as a federated credential based on its managed identity, to Microsoft Entra ID and receives a new token for the downstream API that carries the user's identity. The downstream API therefore applies the user's own permissions, so the agent cannot see more than the user can.
The downstream API sees the user, not the agent.
Put in order

The on-behalf-of exchange

Exam skills 4.1.2

Arrange the steps of the on-behalf-of flow from the user to a downstream API.

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

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.

SituationFlow
Downstream must apply the signed-in user's permissionsOn-behalf-of (delegated)
Background agent with no user presentManaged identity or client credentials (application)
Third-party SaaS data owned by the userOAuth 2.0 authorization code with consent
Legacy API with no token supportAPI key in Key Vault, rotated
Scenario questions

Choosing a flow for each tool

Exam skills 4.1.2

Choose the authentication flow for each tool.

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

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.

Check yourself

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.