A small Node.js Web API for Azure AD B2C that shows how to protect your web api and accept B2C access tokens using Passport.js.
page_type: sample
languages:
This sample demonstrates how to protect a Node.js Web API with Microsoft identity platform and Azure AD B2C using the passport-azure-ad library.
You will need a client application for calling the Web API. Choose:
File/folder | Description |
---|---|
config.js |
Contains configuration parameters for the sample. |
index.js |
Main application logic resides here. |
process.json |
Contains configuration parameters for logging via Morgan. |
From your shell or command line:
git clone https://github.com/Azure-Samples/active-directory-b2c-javascript-nodejs-webapi.git
or download and extract the repository .zip file.
Given that the name of the sample is quite long, and so are the names of the referenced packages, you might want to clone it in a folder close to the root of your hard drive, to avoid maximum file path length limitations on Windows.
cd active-directory-b2c-javascript-nodejs-webapi
npm install
This sample comes with a pre-registered application for testing purposes. If you would like to use your own Azure AD B2C tenant and application, follow the steps below to register and configure the application in the Azure Portal. Otherwise, continue with the steps for Running the sample.
As a first step you’ll need to:
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
active-directory-b2c-javascript-nodejs-webapi
.Set
next to the Application ID URI to generate a URI that is unique for this app.demo.read
.Access active-directory-b2c-javascript-nodejs-webapi
Allows the app to access active-directory-b2c-javascript-nodejs-webapi as the signed-in user.
Manifest
blade.accessTokenAcceptedVersion
property to 2.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”.
config.json
file.tenantName
and replace the existing value with your Azure AD B2C tenant’s name e.g. fabrikamb2c
.clientID
and replace the existing value with the application ID (clientId) of the active-directory-b2c-javascript-nodejs-webapi
application copied from the Azure Portal.policyName
and replace the existing value with name of the policy you’ve created, e.g. B2C_1_SUSI
.
cd active-directory-b2c-javascript-nodejs-webapi
npm start
Call this web API from your client application. Upon an authorized call, the web API will respond by:
res.status(200).json({'name': req.authInfo['name']});
Did the sample not work for you as expected? Then please reach out to us using the GitHub Issues page.
Consider taking a moment to share your experience with us.
passport-azure-ad validates the token against the issuer
, scope
and audience
claims (defined in BearerStrategy
constructor) using the passport.authenticate()
API:
app.get('/hello', passport.authenticate('oauth-bearer', { session: false }),
(req, res) => {
console.log('Validated claims: ', req.authInfo);
);
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, please raise the issue on GitHub Issues.
To provide a recommendation, visit the following 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.