Use Nested App Authentication (NAA) with MSAL.js
masterExtensions requiring Microsoft Entra ID authentication can use the NAA bridge to acquire tokens via @azure/msal-browser (v5+). This bridge allows the Azure DevOps host frame to handle the authentication flow, bypassing cross-origin iframe limitations.
Prerequisites
- Register a SPA app in the Azure Portal.
- Add the redirect URI:
https://dev.azure.com/_public/_MsalPopup. - Configure the required API permissions/scopes.
Implementation Steps
- Call
await SDK.enableNestedAppAuth()before creating the MSAL application. - Use
createNestablePublicClientApplicationfrom@azure/msal-browser. - Do not set
redirectUriin your MSAL configuration; the bridge provides it automatically. - Handle
InteractionRequiredAuthErrorby falling back toacquireTokenPopupto handle user consent or interaction requirements.
import * as SDK from "azure-devops-extension-sdk";
import { createNestablePublicClientApplication, InteractionRequiredAuthError } from "@azure/msal-browser";
await SDK.init();
await SDK.enableNestedAppAuth();
const pca = await createNestablePublicClientApplication({
auth: { clientId: "your-entra-client-id" },
});
try {
const result = await pca.acquireTokenSilent({
scopes: ["https://graph.microsoft.com/.default"],
});
} catch (err) {
if (err instanceof InteractionRequiredAuthError) {
const result = await pca.acquireTokenPopup({
scopes: ["https://graph.microsoft.com/.default"],
});
} else {
throw err;
}
}