Bearbeiten

Freigeben über


Quickstart: Sign in users in a sample web app

In this quickstart, you use a sample web app to show you how to sign in users and call Microsoft Graph API in your workforce tenant. The sample app uses the Microsoft Authentication Library to handle authentication.

Before you begin, use the Choose a tenant type selector at the top of this page to select tenant type. Microsoft Entra ID provides two tenant configurations, workforce and external. A workforce tenant configuration is for your employees, internal apps, and other organizational resources. An external tenant is for your customer-facing apps.

Prerequisites

  • An Azure account with an active subscription. If you don't already have one, Create an account for free.
  • This Azure account must have permissions to manage applications. Any of the following Microsoft Entra roles include the required permissions:
    • Application Administrator
    • Application Developer
    • Cloud Application Administrator
  • A workforce tenant. You can use your Default Directory or set up a new tenant.
  • Visual Studio Code or another code editor.
  • Register a new app in the Microsoft Entra admin center with the following configuration. For more information, see Register an application.
    • Name: identity-client-web-app
    • Supported account types: Accounts in this organizational directory only (Single tenant)
    • Platform configuration: Web
    • Redirect URI: http://localhost:3000/auth/redirect
    • Front-channel logout URL: https://localhost:5001/signout-callback-oidc
  • Node.js

Add app client secret or certificate

Create a client secret for the registered application. The application uses the client secret to prove its identity when it requests for tokens:

  1. From the App registrations page, select the application that you created (such as web app client secret) to open its Overview page.
  2. Under Manage, select Certificates & secrets > Client secrets > New client secret.
  3. In the Description box, enter a description for the client secret (for example, web app client secret).
  4. Under Expires, select a duration for which the secret is valid (per your organizations security rules), and then select Add.
  5. Record the secret's Value. You use this value for configuration in a later step. The secret value won't be displayed again, and isn't retrievable by any means, after you navigate away from the Certificates and secrets. Make sure you record it.

When you create credentials for a confidential client application:

  • Microsoft recommends that you use a certificate instead of a client secret before moving the application to a production environment. For more information on how to use a certificate, see instructions in Microsoft identity platform application authentication certificate credentials.

  • For testing purposes, you can create a self-signed certificate and configure your apps to authenticate with it. However, in production, you should purchase a certificate signed by a well-known certificate authority, then use Azure Key Vault to manage certificate access and lifetime.

Clone or download sample web application

To obtain the sample application, you can either clone it from GitHub or download it as a .zip file.

  • Download the .zip file, then extract it to a file path where the length of the name is fewer than 260 characters or clone the repository:

  • To clone the sample, open a command prompt and navigate to where you wish to create the project, and enter the following command:

    git clone https://github.com/Azure-Samples/ms-identity-node.git
    

Configure the sample web app

For you to sign in users with the sample app, you need to update it with your app and tenant details:

In the ms-identity-node-main folder, open the .env file in the App folder. Replace the following placeholders:

Variable Description Example(s)
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)
Enter_the_Tenant_Info_here Tenant ID or Primary domain contoso.microsoft.com or aaaabbbb-0000-cccc-1111-dddd2222eeee
Enter_the_Application_Id_Here Client ID of the application you registered 00001111-aaaa-2222-bbbb-3333cccc4444
Enter_the_Client_Secret_Here Client secret of the application you registered A1b-C2d_E3f.H4i,J5k?L6m!N7o-P8q_R9s.T0u
Enter_the_Graph_Endpoint_Here The Microsoft Graph API cloud instance that your app calls https://graph.microsoft.com/ (include the trailing forward-slash)
Enter_the_Express_Session_Secret_Here A random string of characters used to sign the Express session cookie A1b-C2d_E3f.H4...

After you make changes, your file should look similar to the following snippet:

CLOUD_INSTANCE=https://login.microsoftonline.com/
TENANT_ID=aaaabbbb-0000-cccc-1111-dddd2222eeee
CLIENT_ID=00001111-aaaa-2222-bbbb-3333cccc4444
CLIENT_SECRET=A1b-C2d_E3f.H4...

REDIRECT_URI=http://localhost:3000/auth/redirect
POST_LOGOUT_REDIRECT_URI=http://localhost:3000

GRAPH_API_ENDPOINT=https://graph.microsoft.com/

EXPRESS_SESSION_SECRET=6DP6v09eLiW7f1E65B8k

Run and test sample web app

You've configured your sample app. You can proceed to run and test it.

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

    cd App
    npm install
    npm start
    
  2. Go to http://localhost:3000/.

  3. 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 sign you in and access your profile. After you're signed in successfully, you'll be redirected back to the application home page.

How the app works

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 Microsoft Entra 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.

In this quickstart, you use a sample web app to show you how to sign in users in your external tenant. The sample app uses the Microsoft Authentication Library to handle authentication.

Before you begin, use the Choose a tenant type selector at the top of this page to select tenant type. Microsoft Entra ID provides two tenant configurations, workforce and external. A workforce tenant configuration is for your employees, internal apps, and other organizational resources. An external tenant is for your customer-facing apps.

Prerequisites

Add app client secret

Create a client secret for the registered application. The application uses the client secret to prove its identity when it requests for tokens:

  1. From the App registrations page, select the application that you created (such as web app client secret) to open its Overview page.
  2. Under Manage, select Certificates & secrets > Client secrets > New client secret.
  3. In the Description box, enter a description for the client secret (for example, web app client secret).
  4. Under Expires, select a duration for which the secret is valid (per your organizations security rules), and then select Add.
  5. Record the secret's Value. You use this value for configuration in a later step. The secret value won't be displayed again, and isn't retrievable by any means, after you navigate away from the Certificates and secrets. Make sure you record it.

When you create credentials for a confidential client application:

  • Microsoft recommends that you use a certificate instead of a client secret before moving the application to a production environment. For more information on how to use a certificate, see instructions in Microsoft identity platform application authentication certificate credentials.

  • For testing purposes, you can create a self-signed certificate and configure your apps to authenticate with it. However, in production, you should purchase a certificate signed by a well-known certificate authority, then use Azure Key Vault to manage certificate access and lifetime.

Once you register your application, it gets assigned the User.Read permission. However, since the tenant is an external tenant, the customer users themselves can't consent to this permission. You as the tenant administrator must consent to this permission on behalf of all the users in the tenant:

  1. From the App registrations page, select the application that you created (such as ciam-client-app) to open its Overview page.

  2. Under Manage, select API permissions.

    1. Select Grant admin consent for <your tenant name>, then select Yes.
    2. Select Refresh, then verify that Granted for <your tenant name> appears under Status for the permission.

Clone or download sample web application

To obtain the sample application, you can either clone it from GitHub or download it as a .zip file:

  • To clone the sample, open a command prompt and navigate to where you wish to create the project, and enter the following command:

    git clone https://github.com/Azure-Samples/ms-identity-ciam-javascript-tutorial.git
    
  • Alternatively, download the sample .zip file, then extract it to a file path where the length of the name is fewer than 260 characters.

Install project dependencies

  1. Open a console window, and change to the directory that contains the Node.js sample app:

    cd 1-Authentication\5-sign-in-express\App
    
  2. Run the following commands to install app dependencies:

    npm install
    

Configure the sample web app

For you to sign in users with the sample app, you need to update it with your app and tenant details:

  1. In your code editor, open App\authConfig.js file.

  2. Find the placeholder:

    • Enter_the_Application_Id_Here and replace it with the Application (client) ID of the app you registered earlier.
    • Enter_the_Tenant_Subdomain_Here and replace it with the Directory (tenant) subdomain. For example, if your tenant primary domain is contoso.onmicrosoft.com, use contoso. If you don't have your tenant name, learn how to read your tenant details.
    • Enter_the_Client_Secret_Here and replace it with the app secret value you copied earlier.

Run and test sample web app

You can now test the sample Node.js web app. You need to start the Node.js server and access it through your browser at http://localhost:3000.

  1. In your terminal, run the following command:

    npm start 
    
  2. Open your browser, then go to http://localhost:3000. You should see the page similar to the following screenshot:

    Screenshot of sign in into a node web app.

  3. After the page completes loading, select Sign in when prompted.

  4. On the sign-in page, type your Email address, select Next, type your Password, then select Sign in. If you don't have an account, select No account? Create one link, which starts the sign-up flow.

  5. If you choose the sign-up option, after filling in your email, one-time passcode, new password, and more account details, you complete the whole sign-up flow. You see a page similar to the following screenshot. You see a similar page if you choose the sign-in option.

    Screenshot of view ID token claims.

  6. Select Sign out to sign the user out of the web app or select View ID token claims to view ID token claims returned by Microsoft Entra.

How it works

When users select the Sign in link, the app initiates an authentication request and redirects users to Microsoft Entra External ID. On the sign-in or sign-up page that appears, once a user successfully signs in, or creates an account, Microsoft Entra External ID returns an ID token to the app. The app validates the ID token, reads the claims, and returns a secure page to the users.

When the users select the Sign out link, the app clears its session, then redirect the user to Microsoft Entra External ID sign-out endpoint to notify it that the user has signed out.

If you want to build an app similar to the sample you've run, complete the steps in Sign in users in your own Node.js web application article.