title | titleSuffix | description | services | author | manager | ms.service | ms.subservice | ms.topic | ms.workload | ms.date | ms.author | ms.custom |
---|---|---|---|---|---|---|---|---|---|---|---|---|
Quickstart: Add authentication to a Node.js web app with MSAL Node | Azure |
Microsoft identity platform |
In this quickstart, you learn how to implement authentication with a Node.js web app and the Microsoft Authentication Library (MSAL) for Node.js. |
active-directory |
mmacy |
celested |
active-directory |
develop |
quickstart |
identity |
10/22/2020 |
marsma |
aaddev, scenarios:getting-started, languages:js, devx-track-js |
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.
See How the sample works for an illustration.
This quickstart uses the Microsoft Authentication Library for Node.js (MSAL Node) with the authorization code flow.
- An Azure subscription. Create an Azure subscription for free.
- Node.js
- Visual Studio Code or another code editor
[!div renderon="docs"]
- Sign in to the Azure portal.
- If you have access to multiple tenants, use the Directory + subscription filter :::image type="icon" source="./media/common/portal-directory-subscription-filter.png" border="false"::: to select the tenant in which you want to register an application.
- Under Manage, select App registrations > New registration.
- Enter a Name for your application. Users of your app might see this name, and you can change it later.
- Under Supported account types, select Accounts in any organizational directory and personal Microsoft accounts.
- Set the Redirect URI value to
http://localhost:3000/redirect
.- Select Register.
- On the app Overview page, note the Application (client) ID value for later use.
- Under Manage, select Certificates & secrets > New client secret. Leave the description blank and default expiration, and then select Add.
- Note the value of Client secret for later use.
[!div class="sxs-lookup" renderon="portal"]
For the code sample for this quickstart to work, you need to create a client secret and add the following reply URL:
http://localhost:3000/redirect
.[!div renderon="portal" id="makechanges" class="nextstepaction"] Make this change for me
[!div id="appconfigured" class="alert alert-info"] Your application is configured with these attributes.
[!div renderon="docs"] To run the project with a web server by using Node.js, download the core project files.
[!div renderon="portal" class="sxs-lookup"] Run the project with a web server by using Node.js.
[!div renderon="portal" class="sxs-lookup" id="autoupdate" class="nextstepaction"] Download the code sample
[!div renderon="docs"]
Extract the project, open the ms-identity-node-main folder, and then open the index.js file.
Set the
clientID
value with the application (client) ID, and then set theclientSecret
value with the client secret.const config = { auth: { clientId: "Enter_the_Application_Id_Here", authority: "https://login.microsoftonline.com/common", clientSecret: "Enter_the_Client_Secret_Here" }, system: { loggerOptions: { loggerCallback(loglevel, message, containsPii) { console.log(message); }, piiLoggingEnabled: false, logLevel: msal.LogLevel.Verbose, } } };
[!div renderon="docs"]
Modify the values in the
config
section:
Enter_the_Application_Id_Here
is the application (client) ID for the application you registered.To find the application (client) ID, go to the app registration's Overview page in the Azure portal.
Enter_the_Client_Secret_Here
is the client secret for the application you registered.To retrieve or generate a new client secret, under Manage, select Certificates & secrets.
The default
authority
value represents the main (global) Azure cloud:authority: "https://login.microsoftonline.com/common",[!div class="sxs-lookup" renderon="portal"]
[!div renderon="docs"]
Run the project by using Node.js.
-
To start the server, run the following commands from within the project directory:
npm install npm start
-
Go to
http://localhost:3000/
. -
Select Sign In to start the sign-in process.
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.
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".
The MSAL Node library signs in users and requests the tokens that are used to access an API that's protected by Microsoft identity platform. You can download the latest version by using the Node.js Package Manager (npm):
npm install @azure/msal-node
[!div class="nextstepaction"] Adding Auth to an existing web app - GitHub code sample >