Skip to content

Latest commit

 

History

History

4-Deployment

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

Deploy your JavaScript Applications to Azure Cloud and use Azure Services to manage your operations

  1. Overview
  2. Scenario
  3. Prerequisites
  4. Setup
  5. Registration
  6. Deployment
  7. Explore the sample
  8. More information
  9. Community Help and Support
  10. Contributing
  11. Code of Conduct

Overview

This sample demonstrates how to deploy a JavaScript single-page application (SPA) coupled with a Node.js web API to Azure Cloud using Azure Storage and Azure App Service, respectively. To do so, we will use the same code sample from Chapter 3.

Scenario

  1. The client application uses the MSAL.js library to sign-in a user and obtain a JWT Access Token from Microsoft Entra ID.
  2. The Access Token is used as a bearer token to authorize the user to call the protected web API.
  3. The protected web API responds with the claims in the Access Token.

Overview

Prerequisites

  • Node.js must be installed to run this sample.
  • A modern web browser. This sample uses ES6 conventions and will not run on Internet Explorer.
  • Visual Studio Code is recommended for running and editing this sample.
  • VS Code Azure Tools Extension extension is recommended for interacting with Azure through VS Code interface.
  • An Microsoft Entra ID tenant. For more information, see: How to get a Microsoft Entra tenant
  • A user account in your Microsoft Entra ID tenant.
  • An Azure subscription. This sample uses Azure Storage and Azure App Service.

Setup

  • Setup the service app:
    cd ms-identity-javascript-tutorial
    cd 3-Authorization-II/1-call-api
    cd API
    npm install
  • Setup the client app:
    cd ..
    cd SPA
    npm install

Registration

Register the service app (Node.js web API)

Use the same app registration credentials that you've obtained during chapter 3-1.

Register the client app (JavaScript SPA)

Use the same app registration credentials that you've obtained during chapter 3-1.

Deployment

There are basically 3 stages that you will have to go through in order to deploy your projects and enable authentication:

  1. Upload your project files to Azure services and obtain published website URIs
  2. Update Microsoft Entra ID App Registration with URIs you have just obtained
  3. Update your configuration files with URIs you have just obtained

Deploy the service app (Node.js web API)

There are various ways to deploy your applications to Azure App Service. Here we provide steps for deployment via VS Code Azure Tools Extension. For more alternatives, visit: Static website hosting in Azure Storage.

We recommend watching the video tutorial offered by Microsoft Docs for preparation.

Step 1: Deploy your app

  1. In the VS Code activity bar, select the Azure logo to show the AZURE APP SERVICE explorer. Select Sign in to Azure... and follow the instructions. Once signed in, the explorer should show the name of your Azure subscription(s).

api_step1

  1. On the App Service explorer section you will see an upward-facing arrow icon. Click on it publish your local files in the API folder to Azure App Services.

api_step2

  1. Choose a creation option based on the operating system to which you want to deploy. in this sample, we choose Linux.
  2. Select a Node.js version when prompted. An LTS version is recommended.
  3. Type a globally unique name for your web app and press Enter. The name must be unique across all of Azure.
  4. After you respond to all the prompts, VS Code shows the Azure resources that are being created for your app in its notification popup.
  5. Select Yes when prompted to update your configuration to run npm install on the target Linux server.

api_step3

Step 2: Disable default authentication

Now you need to navigate to the Azure App Service Portal, and locate your project there. Once you do, click on the Authentication/Authorization blade. There, make sure that the App Services Authentication is switched off (and nothing else is checked), as we are using our own authentication logic.

disable_easy_auth

Step 3: Enable cross-origin resource sharing (CORS)

enable_cors

Deploy the client app (JavaScript SPA)

There are various ways to deploy your applications to Azure Storage. Here we provide steps for deployment via VS Code Azure Tools Extension. For more alternatives, visit: Static website hosting in Azure Storage.

We recommend watching the video tutorial offered by Microsoft Docs for preparation.

Step 1: Deploy the app

Create a distributable files folder, where your html, css and javascript files will be located (in the sample, the SPA/App/ folder is already usable for this). Then follow the steps below:

  1. Right click on the SPA/App/ folder inside. This will open a context menu where you will see the option Deploy to static website via Azure Storage. Click on it.

spa_step1

  1. Follow the dialog window that opens on the top. Select your subscription, then give a name to your storage account.

spa_step2

  1. Once your storage account is created and your files are uploaded, you will see a notification on the bottom-right corner of VS Code interface. When it's done, you will be notified with the published URI of your static website (e.g. https://javascriptspa1.z22.web.core.windows.net/).

spa_step3

Step 2: Update the client app's authentication parameters

  1. Navigate back to to the Microsoft Entra admin center.
  2. In the left-hand navigation pane, select the Microsoft Entra ID service, and then select App registrations.
  3. In the resulting screen, select the name of your application.
  4. From the Branding menu, update the Home page URL, to the address of your service, for example https://javascriptspa1.z22.web.core.windows.net/. Save the configuration.
  5. Add the same URI in the list of values of the Authentication -> Redirect URIs menu. If you have multiple redirect URIs, make sure that there a new entry using the App service's URI for each redirect URI.

Step 3: Update the client app's configuration files

Now you need to update your authentication configuration files in the client project. To do so, go to your Azure Storage Account explorer via VS Code Azure panel. There, click on your project's name > Blob Container > Web as shown below:

spa_step4

Open authConfig.js. Then:

  1. Find the key msalConfig.auth.redirectUri and replace the existing value with your static webpage's URI that you have just registered e.g. https://javascriptspa1.z22.web.core.windows.net/
  2. Find the key apiConfig.uri and replace the existing value with your published web API's endpoint, e.g. https://node-webapi-1.azurewebsites.net/api

Explore the sample

  1. Open your browser and navigate to your deployed client app's URI, for instance: https://javascriptspa1.z22.web.core.windows.net/.
  2. Click the sign-in button on the top right corner.
  3. Once you authenticate, click the Call API button at the center.

Screenshot

We'd love your feedback!

Were we successful in addressing your learning objective? Consider taking a moment to share your experience with us.

More information

For more information about how OAuth 2.0 protocols work in this scenario and other scenarios, see Authentication Scenarios for Microsoft Entra ID.

Community Help and Support

Use Stack Overflow 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-ad azure-ad-b2c ms-identity msal].

If you find a bug in the sample, please raise the issue on GitHub Issues.

To provide a recommendation, visit the following User Voice page.

Contributing

If you'd like to contribute to this sample, see CONTRIBUTING.MD.

This project has adopted the Microsoft Open Source Code of Conduct. For more information, see the Code of Conduct FAQ or contact [email protected] with any additional questions or comments.

Code of Conduct

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact [email protected] with any additional questions or comments.