Skip to main content
Use the App Hosting OAuth relay when your Flows custom app needs an additional sign-in from within the app. For example, pull data from a source authenticated by a different Microsoft Entra ID tenant than Cognite Data Fusion (CDF). connectToHostApp returns CDF credentials only, so the user must sign in with a different Microsoft Entra ID account, typically in that other tenant. You can run the flow more than once for multiple additional accounts. You implement the popup in your app code and decide when it runs. App Hosting serves the relay as a static, unauthenticated page at /apphosting/oauth-relay on the app’s origin; the path stays the same across versions. The authorization code stays in the browser: it isn’t sent to CDF or App Hosting, isn’t persisted beyond the browser cache, and may be accessible across sessions.

Prerequisites

How the relay works

  1. Your app opens a popup at the Microsoft Entra ID authorize URL with response_mode=fragment, so Microsoft Entra ID returns the authorization code in the URL fragment (#code=...).
  2. Include prompt=select_account on that authorization request so Microsoft Entra ID shows the account picker. Without it, Microsoft Entra ID may reuse the CDF account, and the app won’t get a token for the other tenant.
  3. After sign-in, Microsoft Entra ID redirects the popup to {app-origin}/apphosting/oauth-relay#code=...&state=....
  4. The relay posts the fragment to window.opener and does not store it:
    The relay uses postMessage to send the fragment to the window that opened the popup (window.opener). The postMessage target origin is the relay’s own origin. If there is no opener, the page does nothing.
  5. Your app checks event.origin, reads code from the fragment, and exchanges it for tokens with PKCE (Proof Key for Code Exchange). You store those tokens and attach them to calls to the other service. See Store the token and call the other service.
The relay reads window.location.hash. If you omit response_mode=fragment, the code lands in the query string and the app receives an empty fragment.

Find the deployed app origin

You need the app origin to register the redirect URI in Microsoft Entra ID. The origin stays the same for every version of the app in a project. For example, the origin is https://9f446ebf4f46.apps.api.cogniteappsdata.com. You can copy it from the browser or compute it from the app’s externalId and project.
Open the deployed app in CDF and copy the origin from the address bar. Keep https:// and the host, and drop everything after the first / that follows the host.
Use that origin when registering the redirect URI. The redirect URI is the origin plus /apphosting/oauth-relay.

Register the redirect URI

In the Azure portal, add the redirect URIs to the Single-page application platform. Don’t use the Web platform because it doesn’t return the CORS headers required to exchange the code from the iframe.
1

Open Authentication

Go to Microsoft Entra IDApp registrations → your registration → Authentication.
2

Select a platform to add a redirect URI

Under the Redirect URI configuration tab, select + Add a platform or + Add Redirect URI. In the right side panel, select Single-page application.
3

Add a URI for each origin

Add the redirect URI for local and one for the deployed app. The path is always /apphosting/oauth-relay.Don’t register an app asset such as callback.html on the deployed origin. App assets require the CDF session cookie, so the popup receives a 401 response and the opener can’t read it.
4

Update application authentication

After you add the URI, select Confirm to update authentication and save the changes.

Serve the relay locally

App Hosting serves /apphosting/oauth-relay in production. For local development, serve the same path from the Vite dev server:
Add oauthRelayDevPlugin() to the plugins array in vite.config.ts.

Handle the relay response

The relay posts the fragment to your app with postMessage. Handle the message event and read the authorization code from the fragment. Then POST the code and the PKCE verifier to the Microsoft Entra ID token endpoint to get tokens.
Allow the identity provider in manifest.json. If you call Microsoft Graph (Microsoft’s API for user and organization data, for example the /me endpoint), also allow https://graph.microsoft.com. See Content Security Policy.

Store the token and call the other service

The relay doesn’t store the authorization code or the tokens. After you exchange the code, keep the access token (and refresh token, if returned) in your app, for example, in memory or in localStorage. Attach the access token to the requests you send to the other service.

Refresh the access token

If the token response includes a refresh_token, redeem it before the access token expires. Microsoft Entra ID single-page application (SPA) registrations rotate refresh tokens, so replace the stored refresh token after each successful response.

Further reading

Last modified on September 2, 2026