Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Configure OAuth 2.0 so your Teams SDK conversational agent or bot can access APIs on behalf of a signed-in user. This guide covers Microsoft Entra ID registration, an Azure Bot OAuth connection, and runtime integration; for provisioning and deployment, see the end-to-end tutorial.
Important
This Azure Bot Token Service flow requires an Azure Bot resource, regardless of hosting location, but not Azure App Service hosting. A Developer Portal-only bot registration doesn't provide the OAuth connection settings used here.
Prerequisites
- An existing Teams SDK conversational agent or bot and access to its Azure Bot resource. If you don't have a resource, follow Create Azure Bot resource registration.
- Permission to register an application in Microsoft Entra ID and configure its credentials and delegated API permissions.
- Access to your app's configuration and handlers so you can register the OAuth flow and handle sign-in completion. The runtime guidance below targets Teams SDK 2.1.
- A test account that can consent to the required permissions, or help from a tenant administrator when admin consent is required.
User experience
When an operation requires access to their data, users see an explanation and an OAuth card or link. They open the sign-in experience, authenticate with the identity provider, and grant the requested permissions if permitted by tenant policy.
After sign-in, users can continue the protected task. They might not need to sign in again for subsequent requests. Signing out of the app's OAuth connection leaves them signed in to Teams.
Teams SSO can avoid the explicit sign-in experience, but consent or another interaction can still be required.
Developer experience
- Choose OAuth sign-in or Teams SSO to determine whether token-exchange configuration is required.
- Register the identity-provider application with the token-service redirect URI, credentials, and delegated permissions.
- Add an OAuth connection to the Azure Bot resource using the identity-provider app's settings.
- Test the connection to verify sign-in and consent with the provider.
- Use the connection in your agent or bot to register a runtime flow, handle sign-in completion, call the API, and sign out.
Implement OAuth authentication
The core setup consists of an identity-provider app, an OAuth connection on your Azure Bot resource, and a runtime flow that uses that connection. Use your existing hosting environment; no App Service plan is required. Apply runtime changes through your app's existing build and deployment process.
Choose OAuth sign-in or Teams SSO
Teams single sign-on (SSO) builds on OAuth; it isn't a separate authorization protocol.
| Experience | Configuration |
|---|---|
| Explicit sign-in through an OAuth card or link | Follow this article. Leave Token Exchange URL blank. The user signs in to the identity provider and grants the requested permissions. |
| Teams SSO, with a sign-in prompt when needed | Follow Configure your app in Microsoft Entra ID. SSO also requires an exposed API scope, authorized Teams clients, an application ID URI for Token Exchange URL, and app manifest configuration. |
The Bot Auth Quickstart sample includes SSO. Complete its SSO configuration if you use that sample; a successful OAuth connection test alone doesn't verify Teams SSO.
Register the identity-provider application
The bot identity authenticates communication between your agent or bot and the bot service. The identity-provider application authorizes access to an API on behalf of the user. Keep track of which application's credentials each setting requires.
For the steps below, register a separate identity-provider app and leave the existing bot identity unchanged. If you already have an app configured for this purpose, verify its redirect URI, credentials, account audience, and permissions instead of creating another registration.
- Open Microsoft Entra ID in the Azure portal.
- Select App registrations > New registration.
- Enter a name and select the Supported account types for the users you want to support.
- For Redirect URI, select Web and enter
https://token.botframework.com/.auth/web/redirect. - Select Register.
- On Overview, record the Application (client) ID and Directory (tenant) ID.
- Select Certificates & secrets > Client secrets > New client secret.
- Enter a description, choose an expiration, and select Add.
- Copy the secret Value, not the secret ID, and store it securely. The value isn't displayed again after you leave the page.
- Select API permissions > Add a permission > Microsoft Graph > Delegated permissions.
- Add only the permissions your agent or bot needs. For example, use
User.Readto read the signed-in user's profile. Arrange admin consent if the permission or tenant policy requires it.
Note
Regional and sovereign-cloud deployments can require different redirect and OAuth URLs. Use the matching endpoints in Supported OAuth URLs. The redirect URI belongs to the token service, not your app's messaging endpoint.
Add an OAuth connection to the Azure Bot resource
Open your Azure Bot resource in the Azure portal, not the Microsoft Entra app registration.
Select Settings > Configuration > Add OAuth Connection Settings.
Configure the connection:
Field Value Name A connection name, such as BotTeamsOAuth. Use this exact name in your agent or bot configuration.Service Provider Azure Active Directory v2, the Microsoft Entra ID v2 provider. Client id The application (client) ID of the identity-provider application you registered. Client secret The secret Value from that application. Don't enter the secret ID or credentials from a different application. Token Exchange URL Leave blank for explicit OAuth sign-in. For Teams SSO, use the application ID URI as described in the SSO configuration guidance. Tenant ID For a single-tenant identity-provider app, use its directory (tenant) ID. For a multitenant identity-provider app, use commonto support its configured account audience. This field is separate from your app runtime's tenant ID.Scopes A case-sensitive, space-separated list of required delegated permissions, such as User.Read. Match the permissions configured for the identity-provider app.Select Save.
For other identity providers, select the appropriate Service Provider and supply its required fields. The Microsoft Entra-specific fields in this procedure don't apply to every provider.
Caution
Keep secrets and access tokens out of source control, screenshots, chat messages, and logs. Replace expiring credentials before they expire, and update the OAuth connection when you replace its client secret.
Test the connection
- Open the connection you saved.
- Select Test Connection.
- Sign in with a test account supported by the identity-provider application.
- Review the requested permissions and grant consent if allowed.
- Confirm that Test Connection to <your-connection-name> Succeeded appears.
This test verifies the identity-provider connection. It doesn't verify your agent's or bot's messaging endpoint, app installation, or Teams SSO token exchange.
Use the connection in your agent or bot
Use the exact Name saved on the Azure Bot resource when registering the connection in your runtime. The following configuration names are conventions used by the Bot Auth Quickstart sample, not settings that automatically register a flow in every Teams SDK app:
| Bot Auth Quickstart implementation | Connection-name setting |
|---|---|
| .NET | OAuth:ConnectionName in BotAuthQuickstart/appsettings.json. |
| TypeScript (Node.js) | CONNECTION_NAME in the sample's environment configuration. |
| Python | CONNECTION_NAME in the sample's environment configuration. |
Keep your agent or bot runtime's client ID, credential, and tenant ID associated with the bot identity. The identity-provider application's client ID and secret belong in the OAuth connection. Some SSO configurations reuse an app registration; don't assume the two identities are always the same.
Integrate with Teams SDK 2.1
For an existing conversational agent or bot built with Teams SDK 2.1, follow the linked runtime examples below. In each section, select your language and the SDK 2.1 (current) tab. Replace the example connection name, such as graph, with your saved connection name, such as BotTeamsOAuth.
- Register the OAuth flow during app initialization. Use
AddOAuthFlowwithAddTeamsBotApplicationin .NET,app.addOAuthFlowin TypeScript, orapp.add_oauth_flowin Python. Retain or retrieve that flow object for the handlers below. Read any sample-specific connection-name setting in your code and pass its value to the registration API. - Start sign-in from a message handler using the flow's
SignInAsync(.NET),signIn(TypeScript), orsign_in(Python). If a token is returned, continue the protected operation. Otherwise, the SDK sends an OAuth card; return from the handler instead of calling the protected API without a token. - Handle sign-in completion on that flow using
OnSignInComplete(.NET),onSignInComplete(TypeScript), or@flow.on_signin(Python). Interactive sign-in completes on a later turn, not by waiting inside the original message handler. Use the completion callback to continue with the authenticated user; the SDK doesn't automatically resume an agent's task or tool call. For pending-work storage and cleanup, see Design guidelines and best practices. - Use the token with the API client. Build the Graph client or authorized HTTP request from the token returned by the selected flow or its completion callback. Use a token for its intended API and granted delegated permissions; don't reuse another connection's token. Never send access tokens to chat or logs.
- Implement sign-out using the flow's
SignOutAsync(.NET),signOut(TypeScript), orsign_out(Python). Sign-out clears the user's cached token for that connection; it doesn't sign the user out of Teams or every identity provider.
Execute the agent's authenticated API call in application code, such as a tool handler, using the selected flow's token for the current user.
For a Teams app using the default public-cloud OAuth endpoint, include token.botframework.com in the app manifest's validDomains. Test sign-in, a protected API call, and sign-out from a personal chat in Teams with your agent's or bot's authenticated HTTPS messaging endpoint configured. A successful portal connection test or local Playground message test doesn't validate this runtime integration. If you also enable Teams SSO, verify its token exchange in Teams.
For an agent adaptation, also verify the following behaviors in Teams:
- While signed out, request a task that needs delegated access. Complete sign-in and confirm that the task resumes for the correct user, conversation, and OAuth connection.
- Cancel or fail sign-in and confirm that no protected API call runs, the user receives feedback, and abandoned pending work is cleared or expires.
- Test with two users and confirm that tokens and pending tasks aren't shared between them. After sign-out, request another protected operation and confirm that the app obtains a valid token through the configured flow before proceeding.
Handle errors
The identity provider can report errors during connection testing or user sign-in. The following codes are documented in the Microsoft identity platform authorization code flow. They aren't Teams SDK error codes, and the portal or SDK might wrap the provider's response. No fixed HTTP status is asserted here because the visible response depends on the failing endpoint and how it is surfaced.
| Status code | Error code | Description | Developer action |
|---|---|---|---|
| Provider-dependent | invalid_client |
Client authentication failed. | Verify the OAuth connection's identity-provider client ID and secret value. Replace invalid or expired credentials and test again. |
| Provider-dependent | invalid_scope |
The requested scope is invalid. | Correct the OAuth connection's scopes to match the provider's supported delegated permissions. |
| Provider-dependent | consent_required |
The requested permissions require consent. | Start interactive sign-in with the intended permissions and arrange administrator consent when required. |
| Provider-dependent | interaction_required |
Authentication requires user interaction. | Start the SDK's interactive sign-in flow and handle completion on a later turn instead of repeatedly retrying silently. |
Use the following checks when a provider code isn't available or the failure occurs elsewhere in the flow.
| Symptom | What to check |
|---|---|
| The redirect URI is rejected. | Verify that the identity-provider application's Web redirect URI exactly matches the token-service redirect URI for your cloud and region. |
| Test Connection fails. | Verify the identity-provider client ID, secret value, secret expiration, tenant setting, and configured scopes. Use the error displayed by the provider to identify the failing setting, then test again. |
| The user can't grant consent. | Verify the app's supported account types and delegated permissions. Ask a tenant administrator to grant consent when required. |
| The connection test succeeds, but the agent or bot can't get a user token. | Verify that your app uses the exact connection name from its own Azure Bot resource and that its runtime credentials match the registered bot identity. |
| Sign-in succeeds, but the agent's task doesn't resume. | Check the completion handler in SDK integration and the pending-work handling in best practices. |
| OAuth works, but Teams SSO doesn't. | Verify the exposed API scope, authorized Teams clients, Token Exchange URL, and webApplicationInfo using the SSO configuration guidance. |
| Teams can't reach the agent or bot. | Verify your app's public HTTPS messaging endpoint and Teams channel configuration. A successful connection test doesn't test message delivery. |
Handle cancellation and sign-in failures with a user-facing explanation and a way to retry.
Code sample
The Bot Auth Quickstart demonstrates sign-in, Microsoft Graph profile retrieval, and sign-out. It includes Teams SSO configuration beyond the explicit OAuth setup in this article. Use its authentication flow as a foundation for a Teams SDK agent; the command-based sample doesn't demonstrate agent orchestration or automatic task resumption.
| Sample name | Description | .NET | TypeScript | Python |
|---|---|---|---|---|
| Bot Auth Quickstart | Authenticate a user and retrieve their profile from Microsoft Graph. | View | View | View |
The .NET sample uses Teams SDK 2.1. The TypeScript and Python implementations use legacy SDK 2.0 authentication patterns; Python pins SDK 2.0.15. Don't mix their default-connection configuration with SDK 2.1 flow registration. For version-specific configuration and startup instructions, see Prepare the bot sample code. Use the linked SDK 2.1 runtime examples for a current implementation in your existing app.
Design guidelines and best practices
- Request only the delegated permissions needed for the user's operation. Explain why sign-in is needed before starting the flow, and handle tenant policies that require administrator consent.
- Follow the credential separation described in runtime configuration, and use the exact saved connection name when registering the SDK flow.
- Use a token only for its intended API and granted permissions. Let the token service manage acquisition, storage, and refresh rather than exposing tokens to users or adding a separate refresh-token store.
- If an agent must resume a pending request, store the pending work before starting sign-in. Scope it to the user, conversation, and connection because the original message isn't available on the completion turn. Retrieve and clear it after success, and expire or clear abandoned work after cancellation or failure.
- Keep access tokens and credentials out of model prompts, conversation history, and tool results. Return only the API data needed for the user's task, subject to your app's data-handling rules. Authentication establishes access; it doesn't replace application authorization or confirmation for consequential actions.
- Provide clear sign-out feedback and explain its scope: signing out of a connection doesn't revoke all consent at the identity provider.
Limitations and known issues
- This guide covers user-delegated access through the Azure Bot Token Service, not app-only access or agent identity authentication. The runtime APIs apply to Teams SDK, not Microsoft 365 Agents SDK or declarative agents.
- An Azure Bot resource is required for this connection flow, regardless of hosting location. Core OAuth configuration doesn't require Azure App Service hosting.
- The procedure uses Microsoft Entra ID and public-cloud endpoints. Other providers have different fields; regional and sovereign-cloud deployments require their supported OAuth endpoints.
- A successful portal connection test doesn't validate runtime integration, Teams installation, or SSO. Profile retrieval through interactive OAuth fallback isn't proof of successful Teams SSO token exchange.
- Teams SSO requires additional API, client preauthorization, token-exchange, and manifest configuration. When using separate bot and authentication registrations, follow Configure SSO with a separate authentication app.