Skip to content

Commit ff10ea6

Browse files
authored
Merge pull request #199849 from MicrosoftDocs/main
5/30/2022 AM Publish
2 parents 52530a9 + 0fa1017 commit ff10ea6

34 files changed

+463
-249
lines changed

.openpublishing.publish.config.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@
9191
"url": "https://github.com/Azure-Samples/msdocs-storage-bind-function-service",
9292
"branch": "main",
9393
"branch_mapping": {}
94-
},
94+
},
9595
{
9696
"path_to_root": "azure_cli_scripts",
9797
"url": "https://github.com/Azure-Samples/azure-cli-samples",
@@ -901,8 +901,13 @@
901901
"url": "https://github.com/Azure-Samples/azure-sql-binding-func-dotnet-todo",
902902
"branch": "docs-snippets",
903903
"branch_mapping": {}
904+
},
905+
{
906+
"path_to_root": "ms-identity-node",
907+
"url": "https://github.com/Azure-Samples/ms-identity-node",
908+
"branch": "main",
909+
"branch_mapping": {}
904910
}
905-
906911
],
907912
"branch_target_mapping": {
908913
"live": ["Publish", "PDF"],

articles/active-directory/develop/includes/web-app/quickstart-nodejs-msal.md

Lines changed: 29 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ ms.custom: aaddev, scenarios:getting-started, languages:js, devx-track-js
1616
# Customer intent: As an application developer, I want to know how to set up authentication in a web application built using Node.js and MSAL Node.
1717
---
1818

19-
In this quickstart, you download and run a code sample that demonstrates how a Node.js web app can sign in users by using the authorization code flow. The code sample also demonstrates how to get an access token to call Microsoft Graph API.
19+
In this quickstart, you download and run a code sample that demonstrates how a Node.js web app can sign in users by using the authorization code flow. The code sample also demonstrates how to get an access token to call the Microsoft Graph API.
2020

2121
See [How the sample works](#how-the-sample-works) for an illustration.
2222

@@ -37,8 +37,8 @@ This quickstart uses the Microsoft Authentication Library for Node.js (MSAL Node
3737
1. If you have access to multiple tenants, use the **Directories + subscriptions** filter :::image type="icon" source="../../media/common/portal-directory-subscription-filter.png" border="false"::: in the top menu to switch to the tenant in which you want to register the application.
3838
1. Under **Manage**, select **App registrations** > **New registration**.
3939
1. Enter a **Name** for your application. Users of your app might see this name, and you can change it later.
40-
1. Under **Supported account types**, select **Accounts in any organizational directory and personal Microsoft accounts**.
41-
1. Set the **Redirect URI** value to `http://localhost:3000/redirect`.
40+
1. Under **Supported account types**, select **Accounts in this organizational directory only**.
41+
1. Set the **Redirect URI** value to `http://localhost:3000/auth/redirect`.
4242
1. Select **Register**.
4343
1. On the app **Overview** page, note the **Application (client) ID** value for later use.
4444
1. Under **Manage**, select **Certificates & secrets** > **Client secrets** > **New client secret**. Leave the description blank and default expiration, and then select **Add**.
@@ -51,67 +51,57 @@ To run the project with a web server by using Node.js, [download the core projec
5151

5252
#### Step 3: Configure your Node app
5353

54-
Extract the project, open the *ms-identity-node-main* folder, and then open the *index.js* file.
55-
56-
Set the `clientID` value with the application (client) ID, and then set the `clientSecret` value with the client secret.
57-
58-
```javascript
59-
const config = {
60-
auth: {
61-
clientId: "Enter_the_Application_Id_Here",
62-
authority: "https://login.microsoftonline.com/common",
63-
clientSecret: "Enter_the_Client_Secret_Here"
64-
},
65-
    system: {
66-
        loggerOptions: {
67-
            loggerCallback(loglevel, message, containsPii) {
68-
                console.log(message);
69-
            },
70-
         piiLoggingEnabled: false,
71-
         logLevel: msal.LogLevel.Verbose,
72-
        }
73-
    }
74-
};
75-
```
76-
54+
Extract the project, open the *ms-identity-node-main* folder, and then open the *.env* file under the *App* folder. Replace the values above as follows:
7755

78-
Modify the values in the `config` section:
56+
| Variable | Description | Example(s) |
57+
|-----------|--------------|------------|
58+
| `Enter_the_Cloud_Instance_Id_Here` | The Azure cloud instance in which your application is registered | `https://login.microsoftonline.com/` (include the trailing forward-slash) |
59+
| `Enter_the_Tenant_Info_here` | Tenant ID or Primary domain | `contoso.microsoft.com` or `cbe899ec-5f5c-4efe-b7a0-599505d3d54f` |
60+
| `Enter_the_Application_Id_Here` | Client ID of the application you registered | `cbe899ec-5f5c-4efe-b7a0-599505d3d54f` |
61+
| `Enter_the_Client_Secret_Here` | Client secret of the application you registered | `WxvhStRfDXoEiZQj1qCy` |
62+
| `Enter_the_Graph_Endpoint_Here` | The Microsoft Graph API cloud instance that your app will call | `https://graph.microsoft.com/` (include the trailing forward-slash) |
63+
| `Enter_the_Express_Session_Secret_Here` | A random string of characters used to sign the Express session cookie | `WxvhStRfDXoEiZQj1qCy` |
7964

80-
- `Enter_the_Application_Id_Here` is the application (client) ID for the application you registered.
65+
Your file should look similar to below:
8166

82-
To find the application (client) ID, go to the app registration's **Overview** page in the Azure portal.
83-
- `Enter_the_Client_Secret_Here` is the client secret for the application you registered.
67+
```text
68+
CLOUD_INSTANCE=https://login.microsoftonline.com/
69+
TENANT_ID=cbe899ec-5f5c-4efe-b7a0-599505d3d54f
70+
CLIENT_ID=fa29b4c9-7675-4b61-8a0a-bf7b2b4fda91
71+
CLIENT_SECRET=WxvhStRfDXoEiZQj1qCy
8472
85-
To retrieve or generate a new client secret, under **Manage**, select **Certificates & secrets**.
73+
REDIRECT_URI=http://localhost:3000/auth/redirect
74+
POST_LOGOUT_REDIRECT_URI=http://localhost:3000
8675
87-
The default `authority` value represents the main (global) Azure cloud:
76+
GRAPH_API_ENDPOINT=https://graph.microsoft.com/
8877
89-
```javascript
90-
authority: "https://login.microsoftonline.com/common",
78+
EXPRESS_SESSION_SECRET=6DP6v09eLiW7f1E65B8k
9179
```
9280

81+
9382
#### Step 4: Run the project
9483

9584
Run the project by using Node.js.
9685

9786
1. To start the server, run the following commands from within the project directory:
9887

9988
```console
89+
cd App
10090
npm install
10191
npm start
10292
```
10393

10494
1. Go to `http://localhost:3000/`.
10595

106-
1. Select **Sign In** to start the sign-in process.
96+
1. Select **Sign in** to start the sign-in process.
10797

108-
The first time you sign in, you're prompted to provide your consent to allow the application to access your profile and sign you in. After you're signed in successfully, you will see a log message in the command line.
98+
The first time you sign in, you're prompted to provide your consent to allow the application to sign you in and access your profile. After you're signed in successfully, you'll be redirected back to the application home page.
10999

110100
## More information
111101

112102
### How the sample works
113103

114-
The sample hosts a web server on localhost, port 3000. When a web browser accesses this site, the sample immediately redirects the user to a Microsoft authentication page. Because of this, the sample does not contain any HTML or display elements. Authentication success displays the message "OK".
104+
The sample hosts a web server on localhost, port 3000. When a web browser accesses this address, the app renders the home page. Once the user selects **Sign in**, the app redirects the browser to Azure AD sign-in screen, via the URL generated by the MSAL Node library. After user consents, the browser redirects the user back to the application home page, along with an ID and access token.
115105

116106
### MSAL Node
117107

@@ -123,5 +113,6 @@ npm install @azure/msal-node
123113

124114
## Next steps
125115

116+
Learn more about the web app scenario that the Microsoft identity platform supports:
126117
> [!div class="nextstepaction"]
127-
> [Adding Auth to an existing web app - GitHub code sample >](https://github.com/AzureAD/microsoft-authentication-library-for-js/tree/dev/samples/msal-node-samples/auth-code)
118+
> [Web app that signs in users scenario](../../scenario-web-app-sign-user-overview.md)
456 KB
Loading
509 KB
Loading
350 KB
Loading
170 KB
Loading
343 KB
Loading

articles/active-directory/develop/scenario-web-app-sign-user-app-configuration.md

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -180,31 +180,15 @@ In the Azure portal, the reply URIs that you register on the **Authentication**
180180

181181
# [Node.js](#tab/nodejs)
182182

183-
Here, the configuration parameters reside in `index.js`
183+
Here, the configuration parameters reside in *.env* as environment variables:
184184

185-
```javascript
185+
:::code language="text" source="~/ms-identity-node/App/.env":::
186186

187-
const REDIRECT_URI = "http://localhost:3000/redirect";
187+
These parameters are used to create a configuration object in *authConfig.js* file, which will eventually be used to initialize MSAL Node:
188188

189-
const config = {
190-
auth: {
191-
clientId: "Enter_the_Application_Id_Here",
192-
authority: "https://login.microsoftonline.com/Enter_the_Tenant_Info_Here/",
193-
clientSecret: "Enter_the_Client_Secret_Here"
194-
},
195-
system: {
196-
loggerOptions: {
197-
loggerCallback(loglevel, message, containsPii) {
198-
console.log(message);
199-
},
200-
piiLoggingEnabled: false,
201-
logLevel: msal.LogLevel.Verbose,
202-
}
203-
}
204-
};
205-
```
189+
:::code language="js" source="~/ms-identity-node/App/authConfig.js":::
206190

207-
In the Azure portal, the reply URIs that you register on the Authentication page for your application need to match the redirectUri instances that the application defines (`http://localhost:3000/redirect`).
191+
In the Azure portal, the reply URIs that you register on the Authentication page for your application need to match the redirectUri instances that the application defines (`http://localhost:3000/auth/redirect`).
208192

209193
> [!NOTE]
210194
> This quickstart proposes to store the client secret in the configuration file for simplicity. In your production app, you'd want to use other ways to store your secret, such as a key vault or an environment variable.
@@ -350,12 +334,9 @@ For details about the authorization code flow that this method triggers, see the
350334
351335
# [Node.js](#tab/nodejs)
352336
353-
```javascript
354-
const msal = require('@azure/msal-node');
337+
Node sample the Express framework. MSAL is initialized in *auth* route handler:
355338
356-
// Create msal application object
357-
const cca = new msal.ConfidentialClientApplication(config);
358-
```
339+
:::code language="js" source="~/ms-identity-node/App/routes/auth.js" range="6-16":::
359340
360341
# [Python](#tab/python)
361342

articles/active-directory/develop/scenario-web-app-sign-user-app-registration.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ By default, the sample uses:
9696

9797
1. When the **Register an application page** appears, enter your application's registration information:
9898
1. Enter a **Name** for your application, for example `node-webapp`. Users of your app might see this name, and you can change it later.
99-
1. Change **Supported account types** to **Accounts in any organizational directory and personal Microsoft accounts (e.g. Skype, Xbox, Outlook.com)**.
100-
1. In the **Redirect URI (optional)** section, select **Web** in the combo box and enter the following redirect URI: `http://localhost:3000/redirect`.
99+
1. Change **Supported account types** to **Accounts in this organizational directory only**.
100+
1. In the **Redirect URI (optional)** section, select **Web** in the combo box and enter the following redirect URI: `http://localhost:3000/auth/redirect`.
101101
1. Select **Register** to create the application.
102102
1. On the app's **Overview** page, find the **Application (client) ID** value and record it for later. You'll need it to configure the configuration file for this project.
103103
1. Under **Manage**, select **Certificates & secrets**.

articles/active-directory/develop/scenario-web-app-sign-user-sign-in.md

Lines changed: 16 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ else
7272

7373
# [Java](#tab/java)
7474

75-
In our Java quickstart, the sign-in button is located in the [main/resources/templates/index.html](https://github.com/Azure-Samples/ms-identity-java-webapp/blob/master/msal-java-webapp-sample/src/main/resources/templates/index.html) file.
75+
In the Java quickstart, the sign-in button is located in the [main/resources/templates/index.html](https://github.com/Azure-Samples/ms-identity-java-webapp/blob/master/msal-java-webapp-sample/src/main/resources/templates/index.html) file.
7676

7777
```html
7878
<!DOCTYPE html>
@@ -94,13 +94,13 @@ In our Java quickstart, the sign-in button is located in the [main/resources/tem
9494

9595
# [Node.js](#tab/nodejs)
9696

97-
In the Node.js quickstart, there's no sign-in button. The code-behind automatically prompts the user for sign-in when it's reaching the root of the web app.
97+
In the Node.js quickstart, the code for the sign-in button is located in *index.hbs* template file.
9898

99-
```javascript
100-
app.get('/', (req, res) => {
101-
// authentication logic
102-
});
103-
```
99+
:::code language="hbs" source="~/ms-identity-node/App/views/index.hbs" range="10-11":::
100+
101+
This template is served via the main (index) route of the app:
102+
103+
:::code language="js" source="~/ms-identity-node/App/routes/index.js" range="6-15":::
104104

105105
# [Python](#tab/python)
106106

@@ -169,40 +169,9 @@ public class AuthPageController {
169169

170170
# [Node.js](#tab/nodejs)
171171

172-
Unlike other platforms, here the MSAL Node takes care of letting the user sign in from the login page.
173-
174-
```javascript
175-
176-
// 1st leg of auth code flow: acquire a code
177-
app.get('/', (req, res) => {
178-
const authCodeUrlParameters = {
179-
scopes: ["user.read"],
180-
redirectUri: REDIRECT_URI,
181-
};
182-
183-
// get url to sign user in and consent to scopes needed for application
184-
pca.getAuthCodeUrl(authCodeUrlParameters).then((response) => {
185-
res.redirect(response);
186-
}).catch((error) => console.log(JSON.stringify(error)));
187-
});
188-
189-
// 2nd leg of auth code flow: exchange code for token
190-
app.get('/redirect', (req, res) => {
191-
const tokenRequest = {
192-
code: req.query.code,
193-
scopes: ["user.read"],
194-
redirectUri: REDIRECT_URI,
195-
};
196-
197-
pca.acquireTokenByCode(tokenRequest).then((response) => {
198-
console.log("\nResponse: \n:", response);
199-
res.sendStatus(200);
200-
}).catch((error) => {
201-
console.log(error);
202-
res.status(500).send(error);
203-
});
204-
});
205-
```
172+
When the user selects the **Sign in** link, which triggers the `/auth/signin` route, the sign-in controller takes over to authenticate the user with Microsoft identity platform.
173+
174+
:::code language="js" source="~/ms-identity-node/App/routes/auth.js" range="27-107, 135-161":::
206175

207176
# [Python](#tab/python)
208177

@@ -355,7 +324,7 @@ In our Java quickstart, the sign-out button is located in the main/resources/tem
355324

356325
# [Node.js](#tab/nodejs)
357326

358-
This sample application does not implement sign-out.
327+
:::code language="hbs" source="~/ms-identity-node/App/views/index.hbs" range="2, 8":::
359328

360329
# [Python](#tab/python)
361330

@@ -431,7 +400,9 @@ In Java, sign-out is handled by calling the Microsoft identity platform `logout`
431400

432401
# [Node.js](#tab/nodejs)
433402

434-
This sample application does not implement sign-out.
403+
When the user selects the **Sign out** button, the app triggers the `/signout` route, which destroys the session and redirects the browser to Microsoft identity platform sign-out endpoint.
404+
405+
:::code language="js" source="~/ms-identity-node/App/routes/auth.js" range="163-174":::
435406

436407
# [Python](#tab/python)
437408

@@ -479,7 +450,7 @@ In the Java quickstart, the post-logout redirect URI just displays the index.htm
479450

480451
# [Node.js](#tab/nodejs)
481452

482-
This sample application does not implement sign-out.
453+
In the Node quickstart, the post-logout redirect URI is used to redirect the browser back to sample home page after the user completes the logout process with the Microsoft identity platform.
483454

484455
# [Python](#tab/python)
485456

@@ -494,4 +465,4 @@ If you want to learn more about sign-out, read the protocol documentation that's
494465
## Next steps
495466

496467
Move on to the next article in this scenario,
497-
[Move to production](scenario-web-app-sign-user-production.md).
468+
[Move to production](scenario-web-app-sign-user-production.md).

0 commit comments

Comments
 (0)