You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# Requires non-internal subscription - internal subscriptons doesn't provide permission to correctly configure AAD apps
11
11
---
@@ -42,9 +42,13 @@ The authentication in this procedure is provided at the hosting platform layer b
42
42
43
43
:::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.":::
44
44
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 userprofile
46
46
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.
48
52
49
53
## Prerequisites
50
54
@@ -53,15 +57,15 @@ Before your source code is executed on the frontend, the App Service injects the
Create the resource group, web app plan, the web app and deploy in a single step.
67
71
@@ -73,10 +77,10 @@ Create the resource group, web app plan, the web app and deploy in a single step
73
77
cd frontend
74
78
```
75
79
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.
77
81
78
82
```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"
80
84
```
81
85
82
86
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
85
89
cd ../backend
86
90
```
87
91
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.
89
93
90
94
```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"
92
96
```
93
97
94
98
::: zone-end
@@ -101,10 +105,10 @@ Create the resource group, web app plan, the web app and deploy in a single step
101
105
cd frontend
102
106
```
103
107
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.
105
109
106
110
```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"
108
112
```
109
113
110
114
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
113
117
cd ../backend
114
118
```
115
119
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.
117
121
118
122
```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"
120
124
```
121
125
122
126
::: zone-end
123
127
124
-
## Configure app setting
128
+
## 3. Configure app setting
125
129
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`.
127
131
128
132
```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"
130
134
```
131
135
132
-
## Call the backend
136
+
## 4. Call the backend
133
137
134
138
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.
135
139
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`.
137
141
138
142
:::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.":::
139
143
@@ -144,7 +148,7 @@ Browse to the frontend app and return the _fake_ profile from the backend. This
144
148
145
149
The `withAuthentication` value of **false** indicates the authentication _isn't_ set up yet.
146
150
147
-
## Configure authentication
151
+
## 5. Configure authentication
148
152
149
153
In this step, you enable authentication and authorization for the two web apps. This tutorial uses Azure Active Directory as the identity provider.
150
154
@@ -212,7 +216,7 @@ In this step, you **grant the frontend app access to the backend app** on the us
212
216
213
217
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.
214
218
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**.
216
220
217
221
1. In the **Request API permissions** page for the back-end app, select **Delegated permissions** and **user_impersonation**, then select **Add permissions**.
218
222
@@ -222,12 +226,12 @@ In this step, you **grant the frontend app access to the backend app** on the us
222
226
223
227
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).
224
228
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>*.
226
230
227
231
```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>)
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"
231
235
```
232
236
233
237
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
249
253
250
254
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).
251
255
252
-
## Frontend calls the authenticated backend
256
+
## 6. Frontend calls the authenticated backend
253
257
254
258
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.
255
259
@@ -280,6 +284,7 @@ The frontend app needs to pass the user's authentication with the correct `user_
280
284
}
281
285
```
282
286
287
+
This tutorial returns a _fake_ profile to simplify the scenario. The [next tutorial](tutorial-connect-app-access-microsoft-graph-as-user-javascript.md) inthis series demonstrates how to exchange the backend bearerToken for a newtokenwith the scope of a downstream Azure service, such as Microsoft Graph.
283
288
284
289
## <a name="call-api-securely-from-server-code"></a>Backend returns profile to frontend
285
290
@@ -307,9 +312,9 @@ if (bearerToken) {
307
312
}
308
313
```
309
314
310
-
## Browse to the apps
315
+
## 7.Browse to the apps
311
316
312
-
1. Use the frontend web site in a browser. TheURL is in the formate of`https://frontend-<ABC>.azurewebsites.net/`.
317
+
1. Use the frontend web site in a browser. TheURL is in the formate of`https://<front-end-app-name>.azurewebsites.net/`.
313
318
1. The browser requests your authentication to the web app. Complete the authentication.
314
319
1. After authentication completes, the frontend application returns the home page of the app.
315
320
@@ -322,22 +327,41 @@ if (bearerToken) {
322
327
323
328
The `withAuthentication` value of**true** indicates the authentication _is_ set up yet.
324
329
325
-
## When access tokens expire
330
+
## 8. Clean up resources
326
331
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
+
```
328
352
329
353
## Frequently asked questions
330
354
331
-
**How doI test this authentication on my local development machine?**
355
+
### How doI test this authentication on my local development machine?**
332
356
333
357
The authentication inthis 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.
334
358
335
-
**The app isn't displaying _fake_ profile, how doI debug it?**
359
+
## The app isn't displaying _fake_ profile, how doI debug it?
336
360
337
361
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:
338
362
339
363
* 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.
341
365
*HTTP headers:
342
366
* The `x-ms-token-*` headers are injected.
343
367
* 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
373
397
*404: The URL to the server doesn't match a route the server has
374
398
* 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.
375
399
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?
387
401
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).
389
403
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
0 commit comments