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
- A Flows custom app that runs inside CDF.
- A Microsoft Entra ID app registration for the other tenant or service, with the single-page application platform.
- CDF credentials from
connectToHostApp. Use the relay only for the additional Microsoft Entra ID sign-in, not for CDF.
How the relay works
-
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=...). -
Include
prompt=select_accounton 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. -
After sign-in, Microsoft Entra ID redirects the popup to
{app-origin}/apphosting/oauth-relay#code=...&state=.... -
The relay posts the fragment to
window.openerand does not store it:The relay usespostMessageto send the fragment to the window that opened the popup (window.opener). ThepostMessagetarget origin is the relay’s own origin. If there is no opener, the page does nothing. -
Your app checks
event.origin, readscodefrom 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.
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 ishttps://9f446ebf4f46.apps.api.cogniteappsdata.com. You can copy it from the browser or compute it from the app’s externalId and project.
- Copy from the browser
- Compute the hash
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./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 ID → App 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:
oauthRelayDevPlugin() to the plugins array in vite.config.ts.
Handle the relay response
The relay posts the fragment to your app withpostMessage. 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.
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 inlocalStorage. Attach the access token to the requests you send to the other service.
Refresh the access token
If the token response includes arefresh_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
- Flows Auth API: Get a CDF token with
connectToHostApp. - Content Security Policy: Allow
login.microsoftonline.cominmanifest.json. - Run your app locally: Use the development server and CDF iframe.