- Overview
- Scenario
- Contents
- Prerequisites
- Setup
- Registration
- Running the sample
- Explore the sample
- About the code
- More information
- Community Help and Support
- Contributing
This sample demonstrates a Node.js & Express web application that authenticates users against Azure AD B2C, with the help of Microsoft Authentication Library for Node.js (MSAL Node). In doing so, it illustrates authentication concepts such as OpenID scopes, ID Tokens, ID Token validation, user-flows and more.
- The client application uses MSAL Node to obtain an ID Token from Azure AD B2C.
- The ID Token proves that the user has successfully authenticated against Azure AD B2C.
| File/folder | Description |
|---|---|
AppCreationScripts/ |
Contains Powershell scripts to automate app registration. |
ReadmeFiles/ |
Contains illustrations and screenshots. |
App/appSettings.json |
Authentication parameters and settings |
App/cache.json |
Stores MSAL Node token cache data. |
App/app.js |
Application entry point. |
App/utils/cachePlugin.js |
Handles serializing and writing cache to disk. |
- Node.js must be installed to run this sample.
- Visual Studio Code is recommended for running and editing this sample.
- A modern web browser. This sample uses ES6 conventions and will not run on Internet Explorer.
- An Azure AD B2C tenant. For more information see: How to get an Azure AD B2C tenant
- A user account in your Azure AD B2C tenant.
From your shell or command line:
git clone https://github.com/Azure-Samples/ms-identity-javascript-nodejs-tutorial.gitor download and extract the repository .zip file.
⚠️ To avoid path length limitations on Windows, we recommend cloning into a directory near the root of your drive.
Locate the root of the sample folder. Then:
cd 1-Authentication\2-sign-in-b2c\App
npm install- Sign in to the Azure portal.
- If your account is present in more than one Azure AD B2C tenant, select your profile at the top right corner in the menu on top of the page, and then switch directory to change your portal session to the desired Azure AD B2C tenant.
Please refer to: Tutorial: Create user flows in Azure Active Directory B2C
Please refer to: Tutorial: Add identity providers to your applications in Azure Active Directory B2C
- Navigate to the Azure portal and select the Azure AD B2C service.
- Select the App Registrations blade on the left, then select New registration.
- In the Register an application page that appears, enter your application's registration information:
- In the Name section, enter a meaningful application name that will be displayed to users of the app, for example
msal-node-webapp. - Under Supported account types, select Accounts in any identity provider or organizational directory (for authenticating users with user flows).
- In the Redirect URI (optional) section, select Web in the combo-box and enter the following redirect URI:
http://localhost:4000/redirect.
- In the Name section, enter a meaningful application name that will be displayed to users of the app, for example
- Select Register to create the application.
- In the app's registration screen, find and note the Application (client) ID. You use this value in your app's configuration file(s) later in your code.
- Select Save to save your changes.
- In the app's registration screen, select the Certificates & secrets blade in the left to open the page where you can generate secrets and upload certificates.
- In the Client secrets section, select New client secret:
- Type a key description (for instance
app secret), - Select one of the available key durations (6 months, 12 months or Custom) as per your security posture.
- The generated key value will be displayed when you select the Add button. Copy and save the generated value for use in later steps.
- You'll need this key later in your code's configuration files. This key value will not be displayed again, and is not retrievable by any other means, so make sure to note it from the Azure portal before navigating to any other screen or blade.
- Type a key description (for instance
Open the project in your IDE (like Visual Studio or Visual Studio Code) to configure the code.
In the steps below, "ClientID" is the same as "Application ID" or "AppId".
- Open the
App/appSettings.jsonfile. - Find the key
clientIdand replace the existing value with the application ID (clientId) ofmsal-node-webappapp copied from the Azure portal. - Find the key
tenantIdand replace the existing value with your Azure AD B2C tenant ID. - Find the key
clientSecretand replace the existing value with the key you saved during the creation ofmsal-node-webappcopied from the Azure portal. - Find the key
redirectUriand replace the existing value with the Redirect URI formsal-node-webapp. (by defaulthttp://localhost:4000). - Find the key
postLogoutRedirectUriand replace the existing value with the base address ofmsal-node-webapp(by defaulthttp://localhost:4000). - Find the key
policies.authoritiesabd replace it with the authority strings of your policies/user-flows, e.g.https://fabrikamb2c.b2clogin.com/fabrikamb2c.onmicrosoft.com/b2c_1_susi.
ℹ️ For
redirectUriandpostLogoutRedirectUri, you can simply enter the path component of the URI instead of the full URI. This may come in handy in deployment scenarios.
- Open the
App/app.jsfile. - Find the string
ENTER_YOUR_SECRET_HEREand replace it with a secret that will be used when encrypting your app's session using the express-session package.
Locate the root of the sample folder. Then:
npm start- Open your browser and navigate to
http://localhost:4000. - Click the sign-in button on the top right corner.
- Once signed in, select the ID button to see some of the claims in your ID token.
ℹ️ Did the sample not work for you as expected? Then please reach out to us using the GitHub Issues page.
Were we successful in addressing your learning objective? Consider taking a moment to share your experience with us.
AuthProvider class is initialized in the routes/router.js. It expects two parameters, a JSON configuration object (see appSettings.json), and an optional cache plug-in (see cachePlugin.js) if you wish to save your cache to disk. Otherwise, in-memory only cache is used.
Once initialized, authProvider exposes middlewares that can be used in routes:
const express = require('express');
const msalWrapper = require('msal-express-wrapper');
// initialize wrapper
const authProvider = new msalWrapper.AuthProvider(config, cache);
// initialize router
const router = express.Router();
// authentication routes
router.get('/signin', authProvider.signIn);
router.get('/signout', authProvider.signOut);
router.get('/redirect', authProvider.handleRedirect);Under the hood, the wrapper creates an MSAL Node configuration object and initializes a ConfidentialClientApplication instance by passing it.
/**
* @param {JSON} appSettings
* @param {ICachePlugin} cache: cachePlugin
*/
constructor(appSettings: AppSettings, cache?: ICachePlugin) {
ConfigurationUtils.validateAppSettings(appSettings);
this.cryptoProvider = new CryptoProvider();
this.appSettings = appSettings;
this.msalConfig = ConfigurationUtils.getMsalConfiguration(appSettings, cache);
this.tokenValidator = new TokenValidator(this.appSettings, this.msalConfig);
this.msalClient = new ConfidentialClientApplication(this.msalConfig);
}The user clicks on the sign-in button and routes to /signin. From there, the signIn middleware takes over. First, it creates session variables:
/**
* Initiate sign in flow
* @param {Request} req: express request object
* @param {Response} res: express response object
* @param {NextFunction} next: express next function
*/
signIn = (req: Request, res: Response, next: NextFunction): void => {
/**
* Request Configuration
* We manipulate these three request objects below
* to acquire a token with the appropriate claims
*/
if (!req.session['authCodeRequest']) {
req.session.authCodeRequest = {
authority: "",
scopes: [],
state: {},
redirectUri: "",
} as AuthorizationUrlRequest;
}
if (!req.session['tokenRequest']) {
req.session.tokenRequest = {
authority: "",
scopes: [],
redirectUri: ""
} as AuthorizationCodeRequest;
}
if (!req.session['account']) {
req.session.account = {
homeAccountId: "",
environment: "",
tenantId: "",
username: "",
idTokenClaims: {},
} as AccountInfo;
}
// random GUID for csrf protection
req.session.nonce = this.cryptoProvider.createNewGuid();Then, it creates and encodes a state object to pass with an authorization code request. The object is passed to the state parameter as a means of controlling the application flow. For more information, see Pass custom state in authentication requests using MSAL.
const state = this.cryptoProvider.base64Encode(
JSON.stringify({
stage: AppStages.SIGN_IN,
path: req.route.path,
nonce: req.session.nonce
})
);
const params: AuthCodeParams = {
authority: this.msalConfig.auth.authority,
scopes: OIDC_DEFAULT_SCOPES,
state: state,
redirect: this.appSettings.settings.redirectUri,
prompt: PromptValue.SELECT_ACCOUNT
};
// get url to sign user in
this.getAuthCode(req, res, next, params);
}The getAuthCode() method assigns request parameters, and calls the MSAL Node getAuthCodeUrl() API. It then redirects the app to this URL:
/**
* This method is used to generate an auth code request
* @param {Object} req: express request object
* @param {Object} res: express response object
* @param {NextFunction} next: express next function
* @param {AuthCodeParams} params: modifies auth code request url
*/
private getAuthCode = async (req: Request, res: Response, next: NextFunction, params: AuthCodeParams): Promise<void> => {
// ...
// request an authorization code to exchange for tokens
try {
const response = await this.msalClient.getAuthCodeUrl(req.session.authCodeRequest);
res.redirect(response);
} catch (error) {
console.log(error);
res.status(500).send(error);
}After making an authorization code URL request, the user is redirected to the redirect route defined in the Azure AD app registration. Once redirected, the handleRedirect middleware takes over. It first checks for nonce parameter in state against cross-site resource forgery (csrf) attacks, and then for the current app stage. Then, using the code in query parameters, access tokens are requested using the MSAL Node acquireTokenByCode() API, and the response is appended to the express-session variable.
/**
* Middleware that handles redirect depending on request state
* There are basically 2 stages: sign-in and acquire token
* @param {Request} req: express request object
* @param {Response} res: express response object
* @param {NextFunction} next: express next function
*/
handleRedirect = async (req: Request, res: Response, next: NextFunction): Promise<void> => {
if (req.query.state) {
const state = JSON.parse(this.cryptoProvider.base64Decode(req.query.state));
// check if nonce matches
if (state.nonce === req.session.nonce) {
switch (state.stage) {
case AppStages.SIGN_IN: {
// token request should have auth code
const tokenRequest: AuthorizationCodeRequest = {
redirectUri: this.urlUtils.ensureAbsoluteUrl(req, this.appSettings.settings.redirectUri),
scopes: OIDC_DEFAULT_SCOPES,
code: <string>req.query.code,
};
try {
// exchange auth code for tokens
const tokenResponse = await this.msalClient.acquireTokenByCode(tokenRequest)
console.log("\nResponse: \n:", tokenResponse);
try {
const isIdTokenValid = await this.tokenValidator.validateIdToken(tokenResponse.idToken);
if (isIdTokenValid) {
// assign session variables
req.session.account = tokenResponse.account;
req.session.isAuthenticated = true;
res.status(200).redirect(this.appSettings.settings.homePageRoute);
} else {
console.log(ErrorMessages.INVALID_TOKEN);
res.status(401).send(ErrorMessages.NOT_PERMITTED);
}
} catch (error) {
console.log(error);
res.status(500).send(error);
}
} catch (error) {
console.log(error);
res.status(500).send(error);
}
break;
}
// ...
default:
res.status(500).send(ErrorMessages.CANNOT_DETERMINE_APP_STAGE);
break;
}
} else {
console.log(ErrorMessages.NONCE_MISMATCH)
res.status(401).send(ErrorMessages.NOT_PERMITTED);
}
} else {
res.status(500).send(ErrorMessages.STATE_NOT_FOUND)
}
};Web apps (and confidential client apps in general) should validate ID Tokens. In signIn middleware, we add the ID token to the session (see above), and then validate it following the guide: ID Token validation.
First, verify the token signature.
verifyTokenSignature = async (authToken: string): Promise<TokenClaims | boolean> => {
if (StringUtils.isEmpty(authToken)) {
console.log(ErrorMessages.TOKEN_NOT_FOUND);
return false;
}
// we will first decode to get kid parameter in header
let decodedToken;
try {
decodedToken = jwt.decode(authToken, { complete: true });
} catch (error) {
console.log(ErrorMessages.TOKEN_NOT_DECODED);
console.log(error);
return false;
}
// obtains signing keys from discovery endpoint
let keys;
try {
keys = await this.getSigningKeys(decodedToken.header, decodedToken.payload.tid);
} catch (error) {
console.log(ErrorMessages.KEYS_NOT_OBTAINED);
console.log(error);
return false;
}
// verify the signature at header section using keys
let verifiedToken: TokenClaims;
try {
verifiedToken = jwt.verify(authToken, keys);
/**
* if a multiplexer was used in place of tenantId i.e. if the app
* is multi-tenant, the tenantId should be obtained from the user's
* token's tid claim for verification purposes
*/
if (this.appSettings.credentials.tenantId === "common" ||
this.appSettings.credentials.tenantId === "organizations" ||
this.appSettings.credentials.tenantId === "consumers") {
this.appSettings.credentials.tenantId = decodedToken.payload.tid;
}
return verifiedToken;
} catch (error) {
console.log(ErrorMessages.TOKEN_NOT_VERIFIED);
console.log(error);
return false;
}
}Then, validate the token claims:
/**
* Validates the id token for a set of claims
* @param {TokenClaims} idTokenClaims: decoded id token claims
*/
validateIdTokenClaims = (idTokenClaims: TokenClaims): boolean => {
const now = Math.round((new Date()).getTime() / 1000); // in UNIX format
/**
* At the very least, check for issuer, audience, issue and expiry dates.
* For more information on validating id tokens, visit:
* https://docs.microsoft.com/azure/active-directory/develop/id-tokens#validating-an-id_token
*/
const checkIssuer = idTokenClaims['iss'].includes(this.appSettings.credentials.tenantId) ? true: false;
const checkAudience = idTokenClaims["aud"] === this.msalConfig.auth.clientId ? true : false;
const checkTimestamp = idTokenClaims["iat"] <= now && idTokenClaims["exp"] >= now ? true : false;
return checkIssuer && checkAudience && checkTimestamp;
}Simply add the isAuthenticated middleware to your route, before the controller that displays the page you want to be secure. This would require any user to be authenticated to access this route:
const express = require('express');
const msalWrapper = require('msal-express-wrapper');
// initialize wrapper
const authProvider = new msalWrapper.AuthProvider(config, cache);
// initialize router
const router = express.Router();
router.get('/id', authProvider.isAuthenticated, mainController.getIdPage);isAuthenticated() middleware simply checks the user's isAuthenticated session variable, which is assigned during sign-in flow. You can customize isAuthenticated() middleware to redirect an unauthenticated user to the sign-in page if you wish so.
/**
* Check if authenticated in session
* @param {Object} req: express request object
* @param {Object} res: express response object
* @param {Function} next: express next
*/
isAuthenticated = (req: Request, res: Response, next: NextFunction): void | Response => {
if (req.session) {
if (!req.session.isAuthenticated) {
return res.status(401).send(ErrorMessages.NOT_PERMITTED);
}
next();
} else {
res.status(401).send(ErrorMessages.NOT_PERMITTED);
}
}We construct a logout URL following the guide here. Then, we destroy the current express-session and redirect the user to the sign-out endpoint:
/**
* Initiate sign out and clean the session
* @param {Request} req: express request object
* @param {Response} res: express response object
* @param {NextFunction} next: express next function
*/
signOut = (req: Request, res: Response, next: NextFunction): void => {
const postLogoutRedirectUri = this.urlUtils.ensureAbsoluteUrl(req, this.appSettings.settings.postLogoutRedirectUri)
/**
* Construct a logout URI and redirect the user to end the
* session with Azure AD/B2C. For more information, visit:
* (AAD) https://docs.microsoft.com/azure/active-directory/develop/v2-protocols-oidc#send-a-sign-out-request
* (B2C) https://docs.microsoft.com/azure/active-directory-b2c/openid-connect#send-a-sign-out-request
*/
const logoutURI = `${this.msalConfig.auth.authority}/oauth2/v2.0/logout?post_logout_redirect_uri=${postLogoutRedirectUri}`;
req.session.isAuthenticated = false;
req.session.destroy(() => {
res.redirect(logoutURI);
});
}- What is Azure Active Directory B2C?
- Application types that can be used in Active Directory B2C
- Recommendations and best practices for Azure Active Directory B2C
- Azure AD B2C session
- Initialize client applications using MSAL.js
- Single sign-on with MSAL.js
- Handle MSAL.js exceptions and errors
- Logging in MSAL.js applications
- Pass custom state in authentication requests using MSAL.js
- Prompt behavior in MSAL.js interactive requests
- Use MSAL.js to work with Azure AD B2C
For more information about how OAuth 2.0 protocols work in this scenario and other scenarios, see Authentication Scenarios for Azure AD.
Use Stack Overflow to get support from the community.
Ask your questions on Stack Overflow first and browse existing issues to see if someone has asked your question before.
Make sure that your questions or comments are tagged with [azure-active-directory azure-ad-b2c ms-identity adal msal].
If you find a bug in the sample, raise the issue on GitHub Issues.
To provide feedback on or suggest features for Azure Active Directory, visit User Voice page.
If you'd like to contribute to this sample, see CONTRIBUTING.MD.
This project has adopted the Microsoft Open Source Code of Conduct. For more information, see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.

