- Overview
- Scenario
- Prerequisites
- Setup
- Registration
- Deployment
- Explore the sample
- More information
- Community Help and Support
- Contributing
- Code of Conduct
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.
- The client application uses the MSAL.js library to sign-in a user and obtain a JWT Access Token from Microsoft Entra ID.
- The Access Token is used as a bearer token to authorize the user to call the protected web API.
- The protected web API responds with the claims in the Access Token.
- 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 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
Use the same app registration credentials that you've obtained during chapter 3-1.
Use the same app registration credentials that you've obtained during chapter 3-1.
There are basically 3 stages that you will have to go through in order to deploy your projects and enable authentication:
- Upload your project files to Azure services and obtain published website URIs
- Update Microsoft Entra ID App Registration with URIs you have just obtained
- Update your configuration files with URIs you have just obtained
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.
- 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).
- 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.
- Choose a creation option based on the operating system to which you want to deploy. in this sample, we choose Linux.
- Select a Node.js version when prompted. An LTS version is recommended.
- Type a globally unique name for your web app and press Enter. The name must be unique across all of Azure.
- After you respond to all the prompts, VS Code shows the Azure resources that are being created for your app in its notification popup.
- Select Yes when prompted to update your configuration to run npm install on the target Linux server.
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.
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.
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:
- 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.
- Follow the dialog window that opens on the top. Select your subscription, then give a name to your storage account.
- 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/
).
- Navigate back to to the Microsoft Entra admin center.
- In the left-hand navigation pane, select the Microsoft Entra ID service, and then select App registrations.
- In the resulting screen, select the name of your application.
- 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.
- 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.
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:
Open authConfig.js
. Then:
- 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/
- 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
- Open your browser and navigate to your deployed client app's URI, for instance:
https://javascriptspa1.z22.web.core.windows.net/
. - Click the sign-in button on the top right corner.
- Once you authenticate, click the Call API button at the center.
Were we successful in addressing your learning objective? Consider taking a moment to share your experience with us.
For more information about how OAuth 2.0 protocols work in this scenario and other scenarios, see Authentication Scenarios for Microsoft Entra ID.
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.
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 opencode@microsoft.com with any additional questions or comments.
This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.