Skip to content

Commit dd34269

Browse files
committed
Updates based on Cephas's feedback
1 parent 450032e commit dd34269

File tree

1 file changed

+58
-52
lines changed

1 file changed

+58
-52
lines changed

articles/app-service/tutorial-auth-aad.md

Lines changed: 58 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ description: Learn how to use App Service authentication and authorization to se
44
keywords: app service, azure app service, authN, authZ, secure, security, multi-tiered, azure active directory, azure ad
55
ms.devlang: csharp
66
ms.topic: tutorial
7-
ms.date: 02/13/2023
8-
ms.custom: "devx-track-csharp, seodec18, devx-track-azurecli"
7+
ms.date: 3/08/2023
8+
ms.custom: "devx-track-js, seodec18, devx-track-azurecli, engagement-fy23"
99
zone_pivot_groups: app-service-platform-windows-linux
1010
# Requires non-internal subscription - internal subscriptons doesn't provide permission to correctly configure AAD apps
1111
---
@@ -42,9 +42,13 @@ The authentication in this procedure is provided at the hosting platform layer b
4242

4343
:::image type="content" source="./media/tutorial-auth-aad/front-end-app-service-to-back-end-app-service-authentication.png" alt-text="Conceptual diagram show the authentication flow from the web user to the front-end app to the back-end app.":::
4444

45-
The frontend app is configured to securely use the backend API. The frontend application provides a Microsoft sign-in for the user, then allows the user to get their _fake_ profile from the backend. In the next article in this series, the fake profile is replaced with a profile from Microsoft Graph.
45+
## Get the user profile
4646

47-
Before your source code is executed on the frontend, the App Service injects the authenticated `accessToken` from the App Service `x-ms-token-aad-access-token` header. The frontend source code then accesses and sends the accessToken to the backend server as the `bearerToken` to securely access the backend API. The backend server validates the bearerToken before it's passed into your backend source code.
47+
The frontend app is configured to securely use the backend API. The frontend application provides a Microsoft sign-in for the user, then allows the user to get their **_fake_** profile from the backend. This tutorial uses a fake profile to simplify the steps to complete the scenario.
48+
49+
Before your source code is executed on the frontend, the App Service injects the authenticated `accessToken` from the App Service `x-ms-token-aad-access-token` header. The frontend source code then accesses and sends the accessToken to the backend server as the `bearerToken` to securely access the backend API. The backend server validates the bearerToken before it's passed into your backend source code. Once your backend source code receives the bearerToken, it can be used.
50+
51+
_In [the next article](tutorial-connect-app-access-microsoft-graph-as-user-javascript.md) in this series_, the bearerToken is exchanged for a token with a scope to access the Microsoft Graph API. The Microsoft Graph API returns the user's profile information.
4852

4953
## Prerequisites
5054

@@ -53,15 +57,15 @@ Before your source code is executed on the frontend, the App Service injects the
5357
- [Node.js (LTS)](https://nodejs.org/download/)
5458
[!INCLUDE [azure-cli-prepare-your-environment-no-header.md](~/articles/reusable-content/azure-cli/azure-cli-prepare-your-environment-no-header.md)]
5559

56-
## Clone the sample application
60+
## 1. Clone the sample application
5761

5862
1. In the [Azure Cloud Shell](https://shell.azure.com), run the following command to clone the sample repository.
5963

6064
```azurecli-interactive
6165
git clone https://github.com/Azure-Samples/js-e2e-web-app-easy-auth-app-to-app
6266
```
6367
64-
## Create and deploy apps
68+
## 2. Create and deploy apps
6569
6670
Create the resource group, web app plan, the web app and deploy in a single step.
6771
@@ -73,10 +77,10 @@ Create the resource group, web app plan, the web app and deploy in a single step
7377
cd frontend
7478
```
7579
76-
1. Create and deploy the frontend web app with [az webapp up](/cli/azure/webapp#az-webapp-up). Because web app name has to be globally unique, replace `<ABC>` with a unique set of initials or numbers.
80+
1. Create and deploy the frontend web app with [az webapp up](/cli/azure/webapp#az-webapp-up). Because web app name has to be globally unique, replace `<front-end-app-name>` with a unique name.
7781
7882
```azurecli-interactive
79-
az webapp up --resource-group myAuthResourceGroup --name frontend-<ABC> --plan myPlan --sku FREE --location "West Europe"--runtime "NODE:16-lts"
83+
az webapp up --resource-group myAuthResourceGroup --name <front-end-app-name> --plan myPlan --sku FREE --location "West Europe"--runtime "NODE:16-lts"
8084
```
8185
8286
1. Change into the backend web app directory.
@@ -85,10 +89,10 @@ Create the resource group, web app plan, the web app and deploy in a single step
8589
cd ../backend
8690
```
8791
88-
1. Deploy the backend web app to same resource group and app plan. Because web app name has to be globally unique, replace `<ABC>` with a unique set of initials or numbers.
92+
1. Deploy the backend web app to same resource group and app plan. Because web app name has to be globally unique, replace `<back-end-app-name>` with a unique set of initials or numbers.
8993
9094
```azurecli-interactive
91-
az webapp up --resource-group myAuthResourceGroup --name backend-<ABC> --plan myPlan --runtime "NODE:16-lts"
95+
az webapp up --resource-group myAuthResourceGroup --name <back-end-app-name> --plan myPlan --runtime "NODE:16-lts"
9296
```
9397
9498
::: zone-end
@@ -101,10 +105,10 @@ Create the resource group, web app plan, the web app and deploy in a single step
101105
cd frontend
102106
```
103107
104-
1. Create and deploy the frontend web app with [az webapp up](/cli/azure/webapp#az-webapp-up). Because web app name has to be globally unique, replace `<ABC>` with a unique set of initials or numbers.
108+
1. Create and deploy the frontend web app with [az webapp up](/cli/azure/webapp#az-webapp-up). Because web app name has to be globally unique, replace `<front-end-app-name>` with a unique set of initials or numbers.
105109
106110
```azurecli-interactive
107-
az webapp up --resource-group myAuthResourceGroup --name frontend-<ABC> --plan myPlan --sku FREE --location "West Europe" --os-type Linux --runtime "NODE:16-lts"
111+
az webapp up --resource-group myAuthResourceGroup --name <front-end-app-name> --plan myPlan --sku FREE --location "West Europe" --os-type Linux --runtime "NODE:16-lts"
108112
```
109113
110114
1. Change into the backend web app directory.
@@ -113,27 +117,27 @@ Create the resource group, web app plan, the web app and deploy in a single step
113117
cd ../backend
114118
```
115119
116-
1. Deploy the backend web app to same resource group and app plan. Because web app name has to be globally unique, replace `<ABC>` with a unique set of initials or numbers.
120+
1. Deploy the backend web app to same resource group and app plan. Because web app name has to be globally unique, replace `<back-end-app-name>` with a unique set of initials or numbers.
117121
118122
```azurecli-interactive
119-
az webapp up --resource-group myAuthResourceGroup --name backend-<ABC> --plan myPlan --runtime "NODE:16-lts"
123+
az webapp up --resource-group myAuthResourceGroup --name <back-end-app-name> --plan myPlan --runtime "NODE:16-lts"
120124
```
121125
122126
::: zone-end
123127
124-
## Configure app setting
128+
## 3. Configure app setting
125129
126-
The frontend application needs to the know the URL of the backend application for API requests. Use the following Azure CLI command to configure the app setting. The URL should in the format of `https://frontend-<ABC>`.
130+
The frontend application needs to the know the URL of the backend application for API requests. Use the following Azure CLI command to configure the app setting. The URL should be in the format of `https://<back-end-app-name>.azurewebsites.net`.
127131
128132
```azurecli-interactive
129-
az webapp config appsettings set --resource-group myAuthResourceGroup --name frontend-<ABC> --settings BACKEND_URL=backend-<ABC>
133+
az webapp config appsettings set --resource-group myAuthResourceGroup --name <front-end-app-name> --settings BACKEND_URL="https://<back-end-app-name>.azurewebsites.net"
130134
```
131135

132-
## Call the backend
136+
## 4. Call the backend
133137

134138
Browse to the frontend app and return the _fake_ profile from the backend. This action validates that the frontend is successfully requesting the profile from the backend, and the backend is returning the profile.
135139

136-
1. Open the frontend web app in a browser. Replace `<ABC>` with your unique set of initials or numbers. : `https://frontend-<ABC>`.
140+
1. Open the frontend web app in a browser, `https://<front-end-app-name>.azurewebsites.net`.
137141

138142
:::image type="content" source="./media/tutorial-auth-aad/app-home-page.png" alt-text="Screenshot of web browser showing frontend application after successfully completing authentication.":::
139143

@@ -144,7 +148,7 @@ Browse to the frontend app and return the _fake_ profile from the backend. This
144148

145149
The `withAuthentication` value of **false** indicates the authentication _isn't_ set up yet.
146150

147-
## Configure authentication
151+
## 5. Configure authentication
148152

149153
In this step, you enable authentication and authorization for the two web apps. This tutorial uses Azure Active Directory as the identity provider.
150154

@@ -212,7 +216,7 @@ In this step, you **grant the frontend app access to the backend app** on the us
212216

213217
1. In the **Authentication** page for the frontend app, select your frontend app name under **Identity provider**. This app registration was automatically generated for you. Select **API permissions** in the left menu.
214218

215-
1. Select **Add a permission**, then select **My APIs** > **\backend-\<ABC\>**.
219+
1. Select **Add a permission**, then select **My APIs** > **front-end-app-name**.
216220

217221
1. In the **Request API permissions** page for the back-end app, select **Delegated permissions** and **user_impersonation**, then select **Add permissions**.
218222

@@ -222,12 +226,12 @@ In this step, you **grant the frontend app access to the backend app** on the us
222226

223227
The frontend app now has the required permissions to access the back-end app as the signed-in user. In this step, you configure App Service authentication and authorization to give you a usable access token for accessing the back end. For this step, you need the back end's client ID, which you copied from [Enable authentication and authorization for back-end app](#enable-authentication-and-authorization-for-back-end-app).
224228

225-
In the Cloud Shell, run the following commands on the frontend app to add the `scope` parameter to the authentication setting `identityProviders.azureActiveDirectory.login.loginParameters`. Replace *\frontend-\<ABC\>* and *\<back-end-client-id>*.
229+
In the Cloud Shell, run the following commands on the frontend app to add the `scope` parameter to the authentication setting `identityProviders.azureActiveDirectory.login.loginParameters`. Replace *\<front-end-app-name>* and *\<back-end-client-id>*.
226230

227231
```azurecli-interactive
228-
authSettings=$(az webapp auth show -g myAuthResourceGroup -n frontend-<ABC>)
232+
authSettings=$(az webapp auth show -g myAuthResourceGroup -n <front-end-app-name>)
229233
authSettings=$(echo "$authSettings" | jq '.properties' | jq '.identityProviders.azureActiveDirectory.login += {"loginParameters":["scope=openid profile email offline_access api://<back-end-client-id>/user_impersonation"]}')
230-
az webapp auth set --resource-group myAuthResourceGroup --name frontend-<ABC> --body "$authSettings"
234+
az webapp auth set --resource-group myAuthResourceGroup --name <front-end-app-name> --body "$authSettings"
231235
```
232236

233237
The commands effectively add a `loginParameters` property with additional custom scopes. Here's an explanation of the requested scopes:
@@ -249,7 +253,7 @@ Your apps are now configured. The front end is now ready to access the back end
249253

250254
For information on how to configure the access token for other providers, see [Refresh identity provider tokens](configure-authentication-oauth-tokens.md#refresh-auth-tokens).
251255

252-
## Frontend calls the authenticated backend
256+
## 6. Frontend calls the authenticated backend
253257

254258
The frontend app needs to pass the user's authentication with the correct `user_impersonation` scope to the backend. The following steps review the code provided in the sample for this functionality.
255259

@@ -280,6 +284,7 @@ The frontend app needs to pass the user's authentication with the correct `user_
280284
}
281285
```
282286

287+
This tutorial returns a _fake_ profile to simplify the scenario. The [next tutorial](tutorial-connect-app-access-microsoft-graph-as-user-javascript.md) in this series demonstrates how to exchange the backend bearerToken for a new token with the scope of a downstream Azure service, such as Microsoft Graph.
283288

284289
## <a name="call-api-securely-from-server-code"></a>Backend returns profile to frontend
285290

@@ -307,9 +312,9 @@ if (bearerToken) {
307312
}
308313
```
309314

310-
## Browse to the apps
315+
## 7. Browse to the apps
311316

312-
1. Use the frontend web site in a browser. The URL is in the formate of `https://frontend-<ABC>.azurewebsites.net/`.
317+
1. Use the frontend web site in a browser. The URL is in the formate of `https://<front-end-app-name>.azurewebsites.net/`.
313318
1. The browser requests your authentication to the web app. Complete the authentication.
314319
1. After authentication completes, the frontend application returns the home page of the app.
315320

@@ -322,22 +327,41 @@ if (bearerToken) {
322327

323328
The `withAuthentication` value of **true** indicates the authentication _is_ set up yet.
324329

325-
## When access tokens expire
330+
## 8. Clean up resources
326331

327-
Your access token expires after some time. For information on how to refresh your access tokens without requiring users to reauthenticate with your app, see [Refresh identity provider tokens](configure-authentication-oauth-tokens.md#refresh-auth-tokens).
332+
In the preceding steps, you created Azure resources in a resource group.
333+
334+
1. Delete the resource group by running the following command in the Cloud Shell. This command may take a minute to run.
335+
336+
337+
```azurecli-interactive
338+
az group delete --name myAuthResourceGroup
339+
```
340+
341+
342+
1. Delete app registrations for both frontend and backend apps
343+
344+
```azurecli-interactive
345+
# list all authentication apps
346+
az ad app list --query [].[displayName,id] --output table
347+
348+
# delete app - do this for both frontend and backend app
349+
# use ID in table from previous command
350+
az ad app delete id
351+
```
328352

329353
## Frequently asked questions
330354

331-
**How do I test this authentication on my local development machine?**
355+
### How do I test this authentication on my local development machine?**
332356

333357
The authentication in this procedure is provided at the hosting platform layer by Azure App Service. There's no equivalent emulator. You must deploy the frontend and backend app and configuration authentication for each in order to use the authentication.
334358
335-
**The app isn't displaying _fake_ profile, how do I debug it?**
359+
## The app isn't displaying _fake_ profile, how do I debug it?
336360

337361
The frontend and backend apps both have `/debug` routes to help debug the authentication when this application doesn't return the _fake_ profile. The frontend debug route provides the critical pieces to validate:
338362
339363
* Environment variables:
340-
* The `BACKEND_URL` is configured correctly as `https://backend-<ABC>.azurewebsites.net`. Don't include that trailing forward slash or the route.
364+
* The `BACKEND_URL` is configured correctly as `https://<back-end-app-name>.azurewebsites.net`. Don't include that trailing forward slash or the route.
341365
* HTTP headers:
342366
* The `x-ms-token-*` headers are injected.
343367
* Microsoft Graph profile name for signed in user is displayed.
@@ -373,28 +397,10 @@ Because the frontend app calls the backend app from server source code, this isn
373397
* 404: The URL to the server doesn't match a route the server has
374398
* Use the backend app's streaming logs to watch as you make the frontend request for the user's profile. There's debug information in the source code with `console.log` which helps determine where the failure happened.
375399

376-
## Clean up resources
377-
378-
In the preceding steps, you created Azure resources in a resource group.
379-
380-
1. Delete the resource group by running the following command in the Cloud Shell. This command may take a minute to run.
381-
382-
383-
```azurecli-interactive
384-
az group delete --name myAuthResourceGroup
385-
```
386-
400+
### What happens when the front-end token expires?
387401

388-
1. Delete app registrations for both frontend and backend apps
402+
Your access token expires after some time. For information on how to refresh your access tokens without requiring users to reauthenticate with your app, see [Refresh identity provider tokens](configure-authentication-oauth-tokens.md#refresh-auth-tokens).
389403

390-
```azurecli-interactive
391-
# list all authentication apps
392-
az ad app list --query [].[displayName,id] --output table
393-
394-
# delete app - do this for both frontend and backend app
395-
# use ID in table from previous command
396-
az ad app delete id
397-
```
398404

399405
<a name="next"></a>
400406
## Next steps

0 commit comments

Comments
 (0)