Skip to content

Commit 5254846

Browse files
authored
Merge pull request #188355 from MicrosoftDocs/main
2/11 AM Publish
2 parents c4163ae + bc92f50 commit 5254846

File tree

84 files changed

+1838
-1690
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

84 files changed

+1838
-1690
lines changed

.openpublishing.publish.config.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -789,6 +789,18 @@
789789
"branch": "main",
790790
"branch_mapping": {}
791791
},
792+
{
793+
"path_to_root": "active-directory-b2c-msal-node-sign-in-sign-out-webapp",
794+
"url": "https://github.com/Azure-Samples/active-directory-b2c-msal-node-sign-in-sign-out-webapp",
795+
"branch": "main",
796+
"branch_mapping": {}
797+
},
798+
{
799+
"path_to_root": "active-directory-b2c-javascript-nodejs-webapi",
800+
"url": "https://github.com/Azure-Samples/active-directory-b2c-javascript-nodejs-webapi",
801+
"branch": "master",
802+
"branch_mapping": {}
803+
},
792804
{
793805
"path_to_root": "msdocs-python-flask-webapp-quickstart",
794806
"url": "https://github.com/Azure-Samples/msdocs-python-flask-webapp-quickstart",

.openpublishing.redirection.active-directory.json

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10684,7 +10684,22 @@
1068410684
"source_path_from_root": "/articles/active-directory-b2c/troubleshoot-custom-policies.md",
1068510685
"redirect_url": "/azure/active-directory-b2c/troubleshoot",
1068610686
"redirect_document_id": false
10687-
},
10687+
},
10688+
{
10689+
"source_path_from_root": "/articles/active-directory-b2c/tutorial-authenticate-nodejs-web-app-msal.md",
10690+
"redirect_url": "/azure/active-directory-b2c/enable-authentication-in-node-web-app",
10691+
"redirect_document_id": false
10692+
},
10693+
{
10694+
"source_path_from_root": "/articles/active-directory-b2c/tutorial-acquire-access-token.md",
10695+
"redirect_url": "/azure/active-directory-b2c/enable-authentication-in-node-web-app-with-api",
10696+
"redirect_document_id": false
10697+
},
10698+
{
10699+
"source_path_from_root": "/articles/active-directory-b2c/tutorial-call-api-with-access-token.md",
10700+
"redirect_url": "/azure/active-directory-b2c/enable-authentication-in-node-web-app-with-api",
10701+
"redirect_document_id": false
10702+
},
1068810703
{
1068910704
"source_path": "articles/active-directory/managed-identities-azure-resources/how-to-manage-ua-identity-arm.md",
1069010705
"redirect_url": "/azure/active-directory/managed-identities-azure-resources/how-manage-user-assigned-managed-identities?pivots=identity-mi-methods-arm",

articles/active-directory-b2c/TOC.yml

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,6 @@
3333
href: tenant-management.md
3434
- name: 5 - Clean up and delete tenant
3535
href: tutorial-delete-tenant.md
36-
- name: App integration
37-
items:
38-
- name: Web app
39-
items:
40-
- name: Sign in and sign out user Nodejs tutorial
41-
href: tutorial-authenticate-nodejs-web-app-msal.md
42-
- name: Acquire access token Nodejs tutorial
43-
href: tutorial-acquire-access-token.md
44-
- name: Call a protected API Nodejs tutorial
45-
href: tutorial-call-api-with-access-token.md
4636
- name: Samples
4737
items:
4838
- name: Integrate Azure AD B2C with your applications
@@ -139,6 +129,22 @@
139129
href: enable-authentication-spa-app.md
140130
- name: Authentication options
141131
href: enable-authentication-spa-app-options.md
132+
- name: Node.js web app
133+
items:
134+
- name: Configure a sample Node.js web app
135+
href: configure-a-sample-node-web-app.md
136+
- name: Enable authentication in Node.js web app
137+
href: enable-authentication-in-node-web-app.md
138+
- name: Authentication options
139+
href: enable-authentication-in-node-web-app-options.md
140+
- name: Node.js web app that calls a web API
141+
items:
142+
- name: Configure a sample Node.js web API
143+
href: configure-authentication-in-sample-node-web-app-with-api.md
144+
- name: Enable authentication in Node.js web API
145+
href: enable-authentication-in-node-web-app-with-api.md
146+
- name: Authentication options
147+
href: enable-authentication-in-node-web-app-with-api-options.md
142148
- name: Angular single-page application
143149
items:
144150
- name: Configure a sample Angular app
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
---
2+
title: Configure authentication in a sample Node.js web application by using Azure Active Directory B2C (Azure AD B2C)
3+
description: This article discusses how to use Azure Active Directory B2C to sign in and sign up users in a Node.js web application.
4+
titleSuffix: Azure AD B2C
5+
services: active-directory-b2c
6+
author: kengaderdus
7+
manager: CelesteDG
8+
ms.service: active-directory
9+
ms.workload: identity
10+
ms.topic: how-to
11+
ms.date: 02/09/2022
12+
ms.author: kengaderdus
13+
ms.subservice: B2C
14+
---
15+
16+
# Configure authentication in a sample Node.js web application by using Azure Active Directory B2C
17+
18+
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](https://github.com/AzureAD/microsoft-authentication-library-for-js/tree/dev/lib/msal-node) to handle authentication and authorization.
19+
20+
In this article, you’ll do the following tasks:
21+
22+
- Register a web application in the Azure portal.
23+
- Create combined **Sign in and sign up**, **Profile editing**, and **Password reset** user flows for the app in the Azure portal.
24+
- Update a sample Node application to use your own Azure AD B2C application and user flows.
25+
- Test the sample application.
26+
27+
## Prerequisites
28+
29+
- [Node.js](https://nodejs.org).
30+
- [Visual Studio Code](https://code.visualstudio.com/download) or another code editor.
31+
- Azure AD B2C tenant. If you haven't already created your own, follow the steps in [Tutorial: Create an Azure Active Directory B2C tenant](tutorial-create-tenant.md) and record your tenant name.
32+
33+
## Step 1: Configure your user flows
34+
35+
[!INCLUDE [active-directory-b2c-app-integration-add-user-flow](../../includes/active-directory-b2c-app-integration-add-user-flow.md)] Repeat the steps to create three separate user flows as follows:
36+
37+
- A combined **Sign in and sign up** user flow, such as `susi`. This user flow also supports the **Forgot your password** experience.
38+
- A **Profile editing** user flow, such as `edit_profile`.
39+
- A **Password reset** user flow, such as `reset_password`.
40+
41+
Azure AD B2C prepends `B2C_1_` to the user flow name. For example, `susi` becomes `B2C_1_susi`.
42+
43+
## Step 2: Register a web application
44+
45+
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.
46+
47+
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.
48+
49+
### Step 2.1: Register the app
50+
51+
To register the web app, follow these steps:
52+
53+
1. Sign in to the [Azure portal](https://portal.azure.com).
54+
1. Make sure you're using the directory that contains your Azure AD B2C tenant. Select the **Directories + subscriptions** icon in the portal toolbar.
55+
1. On the **Portal settings | Directories + subscriptions** page, find your Azure AD B2C directory in the **Directory name** list, and then select **Switch**.
56+
1. In the Azure portal, search for and select **Azure AD B2C**.
57+
1. Select **App registrations**, and then select **New registration**.
58+
1. Under **Name**, enter a name for the application (for example, *webapp1*).
59+
1. Under **Supported account types**, select **Accounts in any identity provider or organizational directory (for authenticating users with user flows)**.
60+
1. Under **Redirect URI**, select **Web** and then, in the URL box, enter `http://localhost:3000/redirect`.
61+
1. Under **Permissions**, select the **Grant admin consent to openid and offline_access permissions** checkbox.
62+
1. Select **Register**.
63+
1. Select **Overview**.
64+
1. Record the **Application (client) ID** for later use, when you configure the web application.
65+
66+
![Screenshot of the web app Overview page for recording your web app I D.](./media/configure-authentication-sample-python-web-app/get-azure-ad-b2c-app-id.png)
67+
68+
### Step 2.2: Create a web app client secret
69+
70+
[!INCLUDE [active-directory-b2c-app-integration-client-secret](../../includes/active-directory-b2c-app-integration-client-secret.md)]
71+
72+
## Step 3: Get the sample web app
73+
74+
[Download the zip file](https://github.com/Azure-Samples/active-directory-b2c-msal-node-sign-in-sign-out-webapp/archive/refs/heads/main.zip), or clone the sample web application from GitHub.
75+
76+
```bash
77+
git clone https://github.com/Azure-Samples/active-directory-b2c-msal-node-sign-in-sign-out-webapp.git
78+
```
79+
80+
Extract the sample file to a folder. You'll get a web app with the following directory structure:
81+
82+
```text
83+
active-directory-b2c-msal-node-sign-in-sign-out-webapp/
84+
├── index.js
85+
└── package.json
86+
└── .env
87+
└── views/
88+
└── layouts/
89+
└── main.hbs
90+
└── signin.hbs
91+
```
92+
93+
The `views` folder contains Handlebars files for the application's user interface.
94+
95+
## Step 4: Install dependencies
96+
97+
1. Open a console window, and change to the directory that contains the Node.js sample app. For example:
98+
99+
```bash
100+
cd active-directory-b2c-msal-node-sign-in-sign-out-webapp
101+
```
102+
103+
1. Run the following commands to install app dependencies:
104+
105+
```bash
106+
npm install && npm update
107+
```
108+
109+
## Step 5: Configure the sample web app
110+
111+
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:
112+
113+
|Key |Value |
114+
|---------|---------|
115+
|`APP_CLIENT_ID`|The **Application (client) ID** for the web app you registered in [step 2.1](#step-2-register-a-web-application). |
116+
|`APP_CLIENT_SECRET`|The client secret for the web app you created in [step 2.2](#step-22-create-a-web-app-client-secret) |
117+
|`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_node_app`. Learn how to [Get your tenant name](tenant-management.md#get-your-tenant-name). |
118+
|`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`.|
119+
|`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`. |
120+
|`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.|
121+
|`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.|
122+
|`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_node_app`.|
123+
124+
Your final configuration file should look like the following sample:
125+
126+
:::code language="text" source="~/active-directory-b2c-msal-node-sign-in-sign-out-webapp/.env":::
127+
128+
129+
## Run the sample web app
130+
131+
You can now test the sample app. You need to start the Node server and access it through your browser on `http://localhost:3000`.
132+
133+
1. In your terminal, run the following code to start the Node.js web server:
134+
135+
```bash
136+
node index.js
137+
```
138+
139+
2. In your browser, go to `http://localhost:3000`. You should see the page with a **Sign in** button.
140+
141+
:::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.":::
142+
143+
### Test sign in
144+
145+
1. After the page with the **Sign in** button finishes loading, select **Sign in**. You're prompted to sign in.
146+
1. 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. If you have an account but have forgotten your password, select **Forgot your password?** to recover your password. After you successfully sign in or sign up, you should see the following page that shows sign-in status.
147+
148+
:::image type="content" source="./media/configure-a-sample-node-web-app/tutorial-dashboard-page.png" alt-text="Screenshot shows web app sign-in status.":::
149+
150+
### Test profile editing
151+
152+
1. After you sign in, select **Edit profile**.
153+
1. 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**.
154+
155+
### Test password reset
156+
157+
1. After you sign in, select **Reset password**.
158+
1. 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**.
159+
1. Select **Continue**.
160+
1. Enter your new password, confirm it, and then select **Continue**. You should see the page that shows sign-in status.
161+
162+
### Test sign-out
163+
164+
After you sign in, select **Sign out**. You should see the page that has a **Sign in** button.
165+
166+
## Next steps
167+
168+
- Learn how to [customize and enhance the Azure AD B2C authentication experience for your web app](enable-authentication-in-node-web-app-options.md).
169+
- Learn how to [Enable authentication in your own Node web application using Azure AD B2C](enable-authentication-in-node-web-app.md).

0 commit comments

Comments
 (0)