| title | Enable SPA application options by using Azure Active Directory B2C |
|---|---|
| description | This article discusses several ways to enable the use of SPA applications. |
| author | kengaderdus |
| manager | CelesteDG |
| ms.service | azure-active-directory |
| ms.topic | reference |
| ms.date | 01/11/2024 |
| ms.author | kengaderdus |
| ms.subservice | b2c |
| ms.custom | b2c-support |
[!INCLUDE active-directory-b2c-end-of-sale-notice-b]
This article describes how to customize and enhance the Azure Active Directory B2C (Azure AD B2C) authentication experience for your single-page application (SPA).
Before you start, familiarize yourself with the following article: Configure authentication in a sample web application.
[!INCLUDE active-directory-b2c-app-integration-custom-domain]
To use a custom domain and your tenant ID in the authentication URL, follow the guidance in Enable custom domains. Find your Microsoft Authentication Library (MSAL) configuration object and change the authorities and knownAuthorities to use your custom domain name and tenant ID.
The following JavaScript code shows the MSAL configuration object before the change:
const msalConfig = {
auth: {
...
authority: "https://fabrikamb2c.b2clogin.com/fabrikamb2c.onmicrosoft.com/B2C_1_susi",
knownAuthorities: ["fabrikamb2c.b2clogin.com"],
...
},
...
}The following JavaScript code shows the MSAL configuration object after the change:
const msalConfig = {
auth: {
...
authority: "https://custom.domain.com/00000000-0000-0000-0000-000000000000/B2C_1_susi",
knownAuthorities: ["custom.domain.com"],
...
},
...
}[!INCLUDE active-directory-b2c-app-integration-login-hint]
-
If you're using a custom policy, add the required input claim, as described in Set up direct sign-in.
-
Create an object to store the login_hint, and pass this object into the MSAL loginPopup() method.
let loginRequest = { loginHint: "[email protected]" } myMSALObj.loginPopup(loginRequest);
[!INCLUDE active-directory-b2c-app-integration-domain-hint]
-
Check the domain name of your external identity provider. For more information, see Redirect sign-in to a social provider.
-
Create an object to store extraQueryParameters, and pass this object into the MSAL loginPopup() method.
let loginRequest = { extraQueryParameters: {domain_hint: 'facebook.com'} } myMSALObj.loginPopup(loginRequest);
[!INCLUDE active-directory-b2c-app-integration-ui-locales]
-
Create an object to store extraQueryParameters, and pass this object into the MSAL loginPopup() method.
let loginRequest = { extraQueryParameters: {ui_locales: 'en-us'} } myMSALObj.loginPopup(loginRequest);
[!INCLUDE active-directory-b2c-app-integration-custom-parameters]
-
Configure the ContentDefinitionParameters element.
-
Create an object to store extraQueryParameters, and pass this object into the MSAL loginPopup() method.
let loginRequest = { extraQueryParameters: {campaignId: 'germany-promotion'} } myMSALObj.loginPopup(loginRequest);
[!INCLUDE active-directory-b2c-app-integration-id-token-hint]
-
In your custom policy, define an ID token hint technical profile.
-
Create an object to store extraQueryParameters, and pass this object into the MSAL loginPopup() method.
let loginRequest = { extraQueryParameters: {id_token_hint: 'id-token-hint-value'} } myMSALObj.loginPopup(loginRequest);
After logout, the user is redirected to the URI specified in the post_logout_redirect_uri parameter, regardless of the reply URLs that have been specified for the application. However, if a valid id_token_hint is passed and the Require ID Token in logout requests is turned on, Azure AD B2C verifies that the value of post_logout_redirect_uri matches one of the application's configured redirect URIs before performing the redirect. If no matching reply URL was configured for the application, an error message is displayed and the user is not redirected.
To support a secured logout redirect URI, follow the steps below:
-
Create a globally accessible variable to store the
id_token.let id_token = "";
-
In the MSAL
handleResponsefunction, parse theid_tokenfrom theauthenticationResultobject into theid_tokenvariable.function handleResponse(response) { if (response !== null) { setAccount(response.account); id_token = response.idToken; } else { selectAccount(); } }
-
In the
signOutfunction, add theid_token_hintparameter to the logoutRequest object.function signOut() { const logoutRequest = { postLogoutRedirectUri: msalConfig.auth.redirectUri, //set id_token_hint to the id_token value idTokenHint : id_token, mainWindowRedirectUri: msalConfig.auth.redirectUri }; myMSALObj.logoutPopup(logoutRequest); }
In the above example, the post_logout_redirect_uri passed into the logout request will be in the format: https://your-app.com/. This URL must be added to the Application Registration's reply URL's.
Single logout in Azure AD B2C uses OpenId Connect front-channel logout to make logout requests to all applications the user has signed into through Azure AD B2C.
These logout requests are made from the Azure AD B2C logout page, in a hidden Iframe. The Iframes make HTTP requests to all the front-channel logout endpoints registered for the apps that Azure AD B2C has recorded as being logged in.
Your logout endpoint for each application must call the MSAL logout() method. You must also explicitly configure MSAL to run within an Iframe in this scenario by setting allowRedirectInIframe to true.
The following code sample sets allowRedirectInIframe to true:
const msalConfig = {
auth: {
clientId: "enter_client_id_here",
.....
},
cache: {
cacheLocation: "..",
....
},
system: {
allowRedirectInIframe: true
};
}
async function logoutSilent(MSAL) {
return MSAL.logout({
onRedirectNavigate: (url) => {
return false;
}Learn more about MSAL.js configuration options.