Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 

README.md

A Node.js & Express web app calling a custom web API on Azure AD B2C using MSAL Node

  1. Overview
  2. Scenario
  3. Contents
  4. Prerequisites
  5. Setup
  6. Registration
  7. Running the sample
  8. Explore the sample
  9. About the code
  10. More information
  11. Community Help and Support
  12. Contributing

Overview

This sample demonstrates a Node.js & Express web application calling a Node.js & Express web API protected by Azure AD B2C using the Microsoft Authentication Library for Node.js (MSAL Node). In doing so, it also illustrates various authorization concepts, such as OAuth 2.0 Authorization Code Grant, dynamic scopes and incremental consent, access token validation and more.

Scenario

  1. The client application uses the MSAL Node to sign-in a user and obtain a JWT Access Token from Azure AD B2C.
  2. The Access Token is used as a bearer token to authorize the user to access the resource.
  3. The resource owner responds with the resource that the user has access to.

Overview

Contents

File/folder Description
AppCreationScripts/ Contains Powershell scripts to automate app registration.
ReadmeFiles/ Contains images and diagrams for the README.
WebApp/appSettings.json Contains authentication parameters for the web app.
WebApp/data/cache.json Stores MSAL Node token cache data.
WebApp/App/app.js Web application entry point.
WebApp/App/routes/router.js Router configuration where authentication middleware added.
WebAPI/config.json Contains authentication parameters for the web API.
WebAPI/index.js web API entry point.

Prerequisites

  • Node.js must be installed to run this sample.
  • A modern web browser. This sample uses ES6 conventions and will not run on Internet Explorer.
  • Visual Studio Code is recommended for running and editing this sample.
  • 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.

Setup

Step 1: Clone or download this repository

From your shell or command line:

    git clone https://github.com/Azure-Samples/ms-identity-javascript-nodejs-tutorial.git

or 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.

Step 2: Install project dependencies

Locate the root of the sample folder. Then:

    cd WebAPI
    npm install
    cd ../
    cd WebApp
    npm install

Registration

⚠️ This sample comes with a pre-registered application for demo 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 on Azure portal. Otherwise, continue with the steps for Running the sample.

Choose the Azure AD tenant where you want to create your applications

  1. Sign in to the Azure portal.
  2. 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.

Create User Flows and Custom Policies

Please refer to: Tutorial: Create user flows in Azure Active Directory B2C

Add External Identity Providers

Please refer to: Tutorial: Add identity providers to your applications in Azure Active Directory B2C

Register the service app (msal-node-webapi)

  1. Navigate to the Azure portal and select the Azure AD B2C service.
  2. Select the App Registrations blade on the left, then select New registration.
  3. 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-webapi.
    • Under Supported account types, select Accounts in any identity provider or organizational directory (for authenticating users with user flows).
  4. Select Register to create the application.
  5. 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.
  6. Select Save to save your changes.
  7. 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.
  8. 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.
  9. In the app's registration screen, select the Expose an API blade to the left to open the page where you can declare the parameters to expose this app as an API for which client applications can obtain access tokens for. The first thing that we need to do is to declare the unique resource URI that the clients will be using to obtain access tokens for this API. To declare an resource URI, follow the following steps:
    • Select Set next to the Application ID URI to generate a URI that is unique for this app.
    • For this sample, accept the proposed Application ID URI (https://{tenantName}.onmicrosoft.com/{clientId}) by selecting Save.
  10. All APIs have to publish a minimum of one scope for the client's to obtain an access token successfully. To publish a scope, follow these steps:
    • Select Add a scope button open the Add a scope screen and Enter the values as indicated below:
      • For Scope name, use access_as_user.
      • For Admin consent display name type Access msal-node-webapi.
      • For Admin consent description type Allows the app to access msal-node-webapi as the signed-in user.
      • Keep State as Enabled.
      • Select the Add scope button on the bottom to save this scope.
  11. Select the Manifest blade on the left.
    • Set accessTokenAcceptedVersion property to 2.
    • Click on Save.

Configure the service app (msal-node-webapi) to use your app registration

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".

  1. Open the WebAPI\config.json file.
  2. Find the key clientId and replace the existing value with the application ID (clientId) of msal-node-webapi app copied from the Azure portal.
  3. Find the key tenantId and replace the existing value with your Azure AD tenant ID.

Register the client app (msal-node-webapp)

  1. Navigate to the Azure portal and select the Azure AD B2C service.
  2. Select the App Registrations blade on the left, then select New registration.
  3. 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.
  4. Select Register to create the application.
  5. 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.
  6. Select Save to save your changes.
  7. 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.
  8. 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.
  9. In the app's registration screen, select the API permissions blade in the left to open the page where we add access to the APIs that your application needs.
    • Select the Add a permission button and then,
    • Ensure that the My APIs tab is selected.
    • In the list of APIs, select the API msal-node-webapi.
    • In the Delegated permissions section, select the Access 'msal-node-webapi' in the list. Use the search box if necessary.
    • Select the Add permissions button at the bottom.

Configure the client app (msal-node-webapp) to use your app registration

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".

  1. Open the WebApp\appSettings.json file.
  2. Find the key clientId and replace the existing value with the application ID (clientId) of msal-node-webapp app copied from the Azure portal.
  3. Find the key tenantId and replace the existing value with your Azure AD B2C tenant ID.
  4. Find the key clientSecret and replace the existing value with the key you saved during the creation of msal-node-webapp copied from the Azure portal.
  5. Find the key redirectUri and replace the existing value with the Redirect URI for msal-node-webapp. (by default http://localhost:4000/).
  6. Find the key postLogoutRedirectUri and replace the existing value with the base address of msal-node-webapp (by default http://localhost:4000/).
  7. Find the key endpoint and replace the existing value with the base address of msal-node-webapi (by default http://localhost:5000/).
  8. Find the key policies.authorities abd replace it with the authority strings of your policies/user-flows, e.g. https://fabrikamb2c.b2clogin.com/fabrikamb2c.onmicrosoft.com/b2c_1_susi.

The rest of the key-value pairs are for resources/APIs that you would like to call. They are set as default, but you can modify them as you wish:

   "<name of your resource>": {
      "callingPageRoute": "<route where this resource will be called from, e.g. '/webapi'>",
      "endpoint": "<URI coordinates of the resource, e.g. 'http://localhost:5000/myapi'>",
      "scopes": ["scope for the resource, e.g. 'api://xxxxxx/access_as_user' ", "..."]
   },

Running the sample

Locate the root of the sample folder. Then:

    cd WebAPI
    npm start
    cd ../
    cd WebApp
    npm start

Explore the sample

  1. Open your browser and navigate to http://localhost:4000.
  2. Click the Sign-in button on the top right corner.
  3. Once you sign-in, click on the Call web API.

Screenshot

ℹ️ Did the sample not work for you as expected? Then please reach out to us using the GitHub Issues page.

We'd love your feedback!

Were we successful in addressing your learning objective? Consider taking a moment to share your experience with us.

About the code

Configuration

Acquiring an access token

Calling a web API

Access Token validation

More information

For more information about how OAuth 2.0 protocols work in this scenario and other scenarios, see Authentication Scenarios for Azure AD.

Community Help and Support

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.

Contributing

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.