| title | Configure authentication in a sample Node.js web application by using Azure Active Directory B2C (Azure AD B2C) | ||
|---|---|---|---|
| description | This article discusses how to use Azure Active Directory B2C to sign in and sign up users in a Node.js web application. | ||
| titleSuffix | Azure AD B2C | ||
| author | kengaderdus | ||
| manager | CelesteDG | ||
| ms.service | azure-active-directory | ||
| ms.topic | how-to | ||
| ms.date | 01/11/2024 | ||
| ms.author | kengaderdus | ||
| ms.subservice | b2c | ||
| ms.custom |
|
[!INCLUDE active-directory-b2c-end-of-sale-notice-b]
This sample article uses a sample Node.js application to show how to add Azure Active Directory B2C (Azure AD B2C) authentication to a Node.js web application. The sample application enables users to sign in, sign out, update profile and reset password using Azure AD B2C user flows. The sample web application uses Microsoft Authentication Library (MSAL) for Node to handle authentication and authorization.
In this article, you’ll do the following tasks:
- Register a web application in the Azure portal.
- Create combined Sign in and sign up, Profile editing, and Password reset user flows for the app in the Azure portal.
- Update a sample Node application to use your own Azure AD B2C application and user flows.
- Test the sample application.
- Node.js.
- Visual Studio Code or another code editor.
- Azure AD B2C tenant. If you haven't already created your own, follow the steps in Tutorial: Create an Azure Active Directory B2C tenant and record your tenant name.
[!INCLUDE active-directory-b2c-app-integration-add-user-flow]
To enable your application sign in with Azure AD B2C, register your app in the Azure AD B2C directory. The app registration establishes a trust relationship between the app and Azure AD B2C.
During app registration, you'll specify the Redirect URI. The redirect URI is the endpoint to which the user is redirected by Azure AD B2C after they authenticate with Azure AD B2C. The app registration process generates an Application ID, also known as the client ID, that uniquely identifies your app. After your app is registered, Azure AD B2C uses both the application ID, and the redirect URI to create authentication requests.
To register the web app, follow these steps:
-
Sign in to the Azure portal.
-
If you have access to multiple tenants, select the Settings icon in the top menu to switch to your Azure AD B2C tenant from the Directories + subscriptions menu.
-
In the Azure portal, search for and select Azure AD B2C.
-
Select App registrations, and then select New registration.
-
Under Name, enter a name for the application (for example, webapp1).
-
Under Supported account types, select Accounts in any identity provider or organizational directory (for authenticating users with user flows).
-
Under Redirect URI, select Web and then, in the URL box, enter
http://localhost:3000/redirect. -
Under Permissions, select the Grant admin consent to openid and offline_access permissions checkbox.
-
Select Register.
-
Select Overview.
-
Record the Application (client) ID for later use, when you configure the web application.
[!INCLUDE active-directory-b2c-app-integration-client-secret]
Download the zip file, or clone the sample web application from GitHub.
git clone https://github.com/Azure-Samples/active-directory-b2c-msal-node-sign-in-sign-out-webapp.gitExtract the sample file to a folder. You'll get a web app with the following directory structure:
active-directory-b2c-msal-node-sign-in-sign-out-webapp/
├── index.js
└── package.json
└── .env
└── views/
└── layouts/
└── main.hbs
└── signin.hbs
The views folder contains Handlebars files for the application's user interface.
-
Open a console window, and change to the directory that contains the Node.js sample app. For example:
cd active-directory-b2c-msal-node-sign-in-sign-out-webapp -
Run the following commands to install app dependencies:
npm install && npm update
Open your web app in a code editor such as Visual Studio Code. Under the project root folder, open the .env file. This file contains information about your Azure AD B2C identity provider. Update the following app settings properties:
| Key | Value |
|---|---|
APP_CLIENT_ID |
The Application (client) ID for the web app you registered in step 2.1. |
APP_CLIENT_SECRET |
The client secret value for the web app you created in step 2.2 |
SIGN_UP_SIGN_IN_POLICY_AUTHORITY |
The Sign in and sign up user flow authority such as https://<your-tenant-name>.b2clogin.com/<your-tenant-name>.onmicrosoft.com/<sign-in-sign-up-user-flow-name>. Replace <your-tenant-name> with the name of your tenant and <sign-in-sign-up-user-flow-name> with the name of your Sign in and Sign up user flow such as B2C_1_susi. Learn how to Get your tenant name. |
RESET_PASSWORD_POLICY_AUTHORITY |
The Reset password user flow authority such as https://<your-tenant-name>.b2clogin.com/<your-tenant-name>.onmicrosoft.com/<reset-password-user-flow-name>. Replace <your-tenant-name> with the name of your tenant and <reset-password-user-flow-name> with the name of your Reset password user flow such as B2C_1_reset_password_node_app. |
EDIT_PROFILE_POLICY_AUTHORITY |
The Profile editing user flow authority such as https://<your-tenant-name>.b2clogin.com/<your-tenant-name>.onmicrosoft.com/<profile-edit-user-flow-name>. Replace <your-tenant-name> with the name of your tenant and <reset-password-user-flow-name> with the name of your reset password user flow such as B2C_1_edit_profile_node_app. |
AUTHORITY_DOMAIN |
The Azure AD B2C authority domain such as https://<your-tenant-name>.b2clogin.com. Replace <your-tenant-name> with the name of your tenant. |
APP_REDIRECT_URI |
The application redirect URI where Azure AD B2C will return authentication responses (tokens). It matches the Redirect URI you set while registering your app in Azure portal, and it must be publicly accessible. Leave the value as is. |
LOGOUT_ENDPOINT |
The Azure AD B2C sign-out endpoint such as https://<your-tenant-name>.b2clogin.com/<your-tenant-name>.onmicrosoft.com/<sign-in-sign-up-user-flow-name>/oauth2/v2.0/logout?post_logout_redirect_uri=http://localhost:3000. Replace <your-tenant-name> with the name of your tenant and <sign-in-sign-up-user-flow-name> with the name of your Sign in and Sign up user flow such as B2C_1_susi. |
Your final configuration file should look like the following sample:
:::code language="text" source="~/active-directory-b2c-msal-node-sign-in-sign-out-webapp/.env":::
You can now test the sample app. You need to start the Node server and access it through your browser on http://localhost:3000.
-
In your terminal, run the following code to start the Node.js web server:
node index.js
-
In your browser, go to
http://localhost:3000. You should see the page with a Sign in button.:::image type="content" source="./media/configure-a-sample-node-web-app/tutorial-login-page.png" alt-text="Screenshot that shows a Node web app sign in page.":::
-
After the page with the Sign in button completes loading, select Sign in. You're prompted to sign in.
-
Enter your sign-in credentials, such as email address and password. If you don't have an account, select Sign up now to create an account. After you successfully sign in or sign up, you should see the following page that shows sign-in status.
:::image type="content" source="./media/configure-a-sample-node-web-app/tutorial-dashboard-page.png" alt-text="Screenshot shows web app sign-in status.":::
- After you sign in, select Edit profile.
- Enter new changes as required, and then select Continue. You should see the page with sign-in status with the new changes, such as Given Name.
- After you sign in, select Reset password.
- In the next dialog that appears, you can cancel the operation by selecting Cancel. Alternatively, enter your email address, and then select Send verification code. You'll receive a verification code to your email account. Copy the verification code in your email, enter it into the password reset dialog, and then select Verify code.
- Select Continue.
- Enter your new password, confirm it, and then select Continue. You should see the page that shows sign-in status.
After you sign in, select Sign out. You should see the page that has a Sign in button.
