224 lines
12 KiB
Markdown
224 lines
12 KiB
Markdown
---
|
|
page_type: sample
|
|
languages:
|
|
- csharp
|
|
- javascript
|
|
- typescript
|
|
- python
|
|
- java
|
|
products:
|
|
- node.js
|
|
- dotnet
|
|
- aspnet
|
|
- aspnet-core
|
|
- dotnet-core
|
|
- ms-graph
|
|
- azure-app-service
|
|
- azure-storage
|
|
- azure-active-directory
|
|
- azure-active-directory-b2c
|
|
name: A Node.js & Express web app authenticating users against Azure AD with MSAL Node
|
|
urlFragment: ms-identity-javascript-nodejs-tutorial
|
|
description: "This sample demonstrates a Node.js & Express web app that authenticates users against Azure AD"
|
|
azureDeploy: <ENTER_FULLY_QUALIFIED_URL_TO_AN_AZURE_RESOURCE_MANAGER>
|
|
extendedZipContent: <FILES_OR_FOLDERS_WITH_TWO_ABSOLUTE_PATHS_TO_INCLUDE_WITH_ZIP:PATH(NAME_IN_THE_REPO), TARGET(NAME_IN_THE_ZIP)>
|
|
extensions: <ENTER_CONTENT_THAT_OTHER_TEAMS_CAN_USE_TO_IDENTIFY_SAMPLES>
|
|
---
|
|
# A Node.js & Express web app authenticating users against Azure AD with MSAL Node
|
|
|
|
1. [Overview](#overview)
|
|
1. [Scenario](#scenario)
|
|
1. [Contents](#contents)
|
|
1. [Prerequisites](#prerequisites)
|
|
1. [Setup](#setup)
|
|
1. [Registration](#registration)
|
|
1. [Running the sample](#running-the-sample)
|
|
1. [Explore the sample](#explore-the-sample)
|
|
1. [About the code](#about-the-code)
|
|
1. [Deployment](#deployment)
|
|
1. [More information](#more-information)
|
|
1. [Community Help and Support](#community-help-and-support)
|
|
1. [Contributing](#contributing)
|
|
|
|
![Build badge](https://identitydivision.visualstudio.com/_apis/public/build/definitions/a7934fdd-dcde-4492-a406-7fad6ac00e17/<BuildNumber>/badge)
|
|
|
|
## Overview
|
|
|
|
This sample demonstrates a Node.js & Express web app that authenticates users against Azure AD.
|
|
|
|
## Scenario
|
|
|
|
1. The client Node.js & Express web app uses the Microsoft Authentication Library (MSAL) to obtain an ID Token from **Azure AD**.
|
|
2. The **ID Token** proves that the user has successfully authenticated against **Azure AD**.
|
|
|
|
![Overview](./ReadmeFiles/topology.png)
|
|
|
|
## Contents
|
|
|
|
> Give a high-level folder structure of the sample.
|
|
|
|
| File/folder | Description |
|
|
|-------------------|--------------------------------------------|
|
|
| `CHANGELOG.md` | List of changes to the sample. |
|
|
| `CONTRIBUTING.md` | Guidelines for contributing to the sample. |
|
|
| `LICENSE` | The license for the sample. |
|
|
|
|
## Prerequisites
|
|
|
|
- An **Azure AD** tenant. For more information see: [How to get an Azure AD tenant](https://docs.microsoft.com/azure/active-directory/develop/quickstart-create-new-tenant)
|
|
- A user account in your **Azure AD** tenant. This sample will not work with a **personal Microsoft account**. Therefore, if you signed in to the [Azure portal](https://portal.azure.com) with a personal account and have never created a user account in your directory before, you need to do that now.
|
|
|
|
## Setup
|
|
|
|
### Step 1: Clone or download this repository
|
|
|
|
From your shell or command line:
|
|
|
|
```console
|
|
git clone https://github.com/Azure-Samples/ms-identity-javascript-nodejs-tutorial.git
|
|
```
|
|
|
|
or download and extract the repository .zip file.
|
|
|
|
> :warning: To avoid path length limitations on Windows, we recommend cloning into a directory near the root of your drive.
|
|
|
|
### Register the sample application(s) with your Azure Active Directory tenant
|
|
|
|
There is one project in this sample. To register it, you can:
|
|
|
|
- follow the steps below for manually register your apps
|
|
- or use PowerShell scripts that:
|
|
- **automatically** creates the Azure AD applications and related objects (passwords, permissions, dependencies) for you.
|
|
- modify the projects' configuration files.
|
|
|
|
<details>
|
|
<summary>Expand this section if you want to use this automation:</summary>
|
|
|
|
> :warning: If you have never used **Azure AD Powershell** before, we recommend you go through the [App Creation Scripts](./AppCreationScripts/AppCreationScripts.md) once to ensure that your environment is prepared correctly for this step.
|
|
|
|
1. On Windows, run PowerShell as **Administrator** and navigate to the root of the cloned directory
|
|
1. If you have never used Azure AD Powershell before, we recommend you go through the [App Creation Scripts](./AppCreationScripts/AppCreationScripts.md) once to ensure that your environment is prepared correctly for this step.
|
|
1. In PowerShell run:
|
|
|
|
```PowerShell
|
|
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process -Force
|
|
```
|
|
|
|
1. Run the script to create your Azure AD application and configure the code of the sample application accordingly.
|
|
1. In PowerShell run:
|
|
|
|
```PowerShell
|
|
cd .\AppCreationScripts\
|
|
.\Configure.ps1
|
|
```
|
|
|
|
> Other ways of running the scripts are described in [App Creation Scripts](./AppCreationScripts/AppCreationScripts.md)
|
|
> The scripts also provide a guide to automated application registration, configuration and removal which can help in your CI/CD scenarios.
|
|
|
|
</details>
|
|
|
|
### Choose the Azure AD tenant where you want to create your applications
|
|
|
|
As a first step you'll need to:
|
|
|
|
1. Sign in to the [Azure portal](https://portal.azure.com).
|
|
1. If your account is present in more than one Azure AD 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 tenant.
|
|
|
|
### Register the webApp app (ms-identity-node)
|
|
|
|
1. Navigate to the [Azure portal](https://portal.azure.com) and select the **Azure AD** service.
|
|
1. Select the **App Registrations** blade on the left, then select **New registration**.
|
|
1. 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 `ms-identity-node`.
|
|
- Under **Supported account types**, select **Accounts in this organizational directory only**.
|
|
- In the **Redirect URI (optional)** section, select **Web** in the combo-box and enter the following redirect URI: `http://localhost:3000/redirect`.
|
|
1. Select **Register** to create the application.
|
|
1. 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.
|
|
1. Select **Save** to save your changes.
|
|
1. In the app's registration screen, select the **Certificates & secrets** blade in the left to open the page where we can generate secrets and upload certificates.
|
|
1. In the **Client secrets** section, select **New client secret**:
|
|
- Type a key description (for instance `app secret`),
|
|
- Select one of the available key durations (**In 1 year**, **In 2 years**, or **Never Expires**) as per your security posture.
|
|
- The generated key value will be displayed when you select the **Add** button. Copy the generated value for use in the steps later.
|
|
- 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.
|
|
|
|
#### Configure the webApp app (ms-identity-node) 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 `index.js` file.
|
|
1. Find the key `Enter_the_Application_Id_Here` and replace the existing value with the application ID (clientId) of `ms-identity-node` app copied from the Azure portal.
|
|
1. Find the key `Enter_the_Cloud_Instance_Id_HereEnter_the_Tenant_Info_Here` and replace the existing value with 'https://login.microsoftonline.com/common'.
|
|
1. Find the key `Enter_the_Client_Secret_Here` and replace the existing value with the key you saved during the creation of `ms-identity-node` copied from the Azure portal.
|
|
|
|
## Running the sample
|
|
|
|
## Explore the sample
|
|
|
|
> Explain how to explore the sample.
|
|
> Insert a screenshot of the client application.
|
|
|
|
> :information_source: Did the sample not work for you as expected? Then please reach out to us using the [GitHub Issues](../../../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](Enter_Survey_Form_Link).
|
|
|
|
## About the code
|
|
|
|
> - Describe where the code uses auth libraries, or calls the graph
|
|
> - Describe specific aspects (e.g. caching, validation etc.)
|
|
|
|
## Deployment
|
|
|
|
|
|
### Deploying web app to Azure App Services
|
|
|
|
There is one web app in this sample. To deploy it to **Azure App Services**, you'll need to:
|
|
|
|
- create an **Azure App Service**
|
|
- publish the projects to the **App Services**, and
|
|
- update its client(s) to call the website instead of the local environment.
|
|
#### Update the Azure AD app registration (ms-identity-node)
|
|
|
|
1. Navigate back to to the [Azure portal](https://portal.azure.com).
|
|
In the left-hand navigation pane, select the **Azure Active Directory** service, and then select **App registrations (Preview)**.
|
|
1. In the resulting screen, select the `ms-identity-node` application.
|
|
1. In the app's registration screen, select **Authentication** in the menu.
|
|
- In the **Redirect URIs** section, update the reply URLs to match the site URL of your Azure deployment. For example:
|
|
- `https://ms-identity-node.azurewebsites.net/redirect`
|
|
|
|
> :warning: If your app is using an *in-memory* storage, **Azure App Services** will spin down your web site if it is inactive, and any records that your app was keeping will emptied. In addition, if you increase the instance count of your website, requests will be distributed among the instances. Your app's records, therefore, will not be the same on each instance.
|
|
|
|
|
|
## More information
|
|
|
|
- [Microsoft identity platform (Azure Active Directory for developers)](https://docs.microsoft.com/azure/active-directory/develop/)
|
|
- [Overview of Microsoft Authentication Library (MSAL)](https://docs.microsoft.com/azure/active-directory/develop/msal-overview)
|
|
- [Quickstart: Register an application with the Microsoft identity platform (Preview)](https://docs.microsoft.com/azure/active-directory/develop/quickstart-register-app)
|
|
- [Quickstart: Configure a client application to access web APIs (Preview)](https://docs.microsoft.com/azure/active-directory/develop/quickstart-configure-app-access-web-apis)
|
|
- [Understanding Azure AD application consent experiences](https://docs.microsoft.com/azure/active-directory/develop/application-consent-experience)
|
|
- [Understand user and admin consent](https://docs.microsoft.com/azure/active-directory/develop/howto-convert-app-to-be-multi-tenant#understand-user-and-admin-consent)
|
|
- [Application and service principal objects in Azure Active Directory](https://docs.microsoft.com/azure/active-directory/develop/app-objects-and-service-principals)
|
|
- [National Clouds](https://docs.microsoft.com/azure/active-directory/develop/authentication-national-cloud#app-registration-endpoints)
|
|
- [MSAL code samples](https://docs.microsoft.com/azure/active-directory/develop/sample-v2-code)
|
|
|
|
For more information about how OAuth 2.0 protocols work in this scenario and other scenarios, see [Authentication Scenarios for Azure AD](https://docs.microsoft.com/azure/active-directory/develop/authentication-flows-app-scenarios).
|
|
|
|
## Community Help and Support
|
|
|
|
Use [Stack Overflow](http://stackoverflow.com/questions/tagged/msal) 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](../../../issues).
|
|
|
|
To provide feedback on or suggest features for Azure Active Directory, visit [User Voice page](https://feedback.azure.com/forums/169401-azure-active-directory).
|
|
|
|
## Contributing
|
|
|
|
If you'd like to contribute to this sample, see [CONTRIBUTING.MD](/CONTRIBUTING.md).
|
|
|
|
This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/). For more information, see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with any additional questions or comments. |