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
@@ -26,7 +26,7 @@ In this tutorial, you learn how to:
26
26
27
27
Complete the previous tutorial, [Access Microsoft Graph from a secured JavaScript app as the user](tutorial-auth-aad.md), before starting this tutorial but don't remove the resources at the end of the tutorial. This tutorial assumes you have the two App services and their corresponding authentication apps.
28
28
29
-
The previous tutorial used the Azure Cloud Shell as the shell for the Azure CLI. This tutorial continues that usage. In order to add the code to the backend app service to exchange the token, this tutorial uses a version of the _code_ editor. Other editors are available such as nano, vim, and emacs.
29
+
The previous tutorial used the Azure Cloud Shell as the shell for the Azure CLI. This tutorial continues that usage.
30
30
31
31
## Architecture
32
32
@@ -41,9 +41,13 @@ The tutorial shows how to pass the user credential provided by the frontend app
41
41
1. Sign in user to a frontend App service configured to use Active Directory as the identity provider.
42
42
1. The frontend App service passes user's token to backend App service.
43
43
1. The backend App is secured to allow the frontend to make an API request. The user's access token has an audience for the backend API and scope of `user_impersonation`.
44
+
1. The backend app registration already has the Microsoft Graph with the scope `User.Read`. This is added by default to all app registrations.
44
45
45
46
This tutorial extends the architecture:
46
47
48
+
* Grant admin consent to bypass the user consent screen for the back-end app.
49
+
* Change the application code to convert the access token sent from the front-end app to an access token with the required permission for Microsoft Graph.
50
+
47
51
1. Provide code to have backend app **exchange token** for new token with scope of downstream Azure service such as Microsoft Graph.
48
52
1. Provide code to have backend app **use new token** to access downstream service as the current authenticate user.
49
53
1.**Redeploy** backend app with `az webapp up`.
@@ -71,8 +75,6 @@ In this tutorial, in order to read user profile from Microsoft Graph, the back-e
71
75
72
76
:::image type="content" source="./media/tutorial-connect-app-app-graph-javascript/azure-portal-authentication-app-api-permission-admin-consent-granted.png" alt-text="Screenshot of Azure portal authentication app with admin consent granted in status column.":::
73
77
74
-
75
-
76
78
## 2. Install npm packages
77
79
78
80
In the previous tutorial, the backend app didn't need any npm packages for authentication because the only authentication was provided by configuring the identity provider in the Azure portal. In this tutorial, the signed-in user's access token for the back-end API must be exchanged for an access token with Microsoft Graph in its scope. This exchange is completed with two libraries because this exchange doesn't use App Service authentication anymore, but Azure Active Directory and MSAL.js directly.
@@ -134,125 +136,133 @@ The source code to complete this step is provided for you. Use the following ste
134
136
```azurecli-interactive
135
137
az webapp up --resource-group myAuthResourceGroup --name <back-end-app-name>
136
138
137
-
## 4. Exchange backend API token for the Microsoft Graph token
139
+
## 4. Review backend code to exchange backend API token for the Microsoft Graph token
138
140
139
141
In order to change the backend API audience token for a Microsoft Graph token, the backend app needs to find the Tenant ID and use that as part of the MSAL.js configuration object. Because the backend app with configured with Microsoft as the identity provider, the Tenant ID and several other required values are already in the App service app settings.
140
142
141
143
The following code is already provided for you in the sample app. You need to understand why it's there and how it works so that you can apply this work to other apps you build that need this same functionality.
142
144
143
145
### Get the Tenant ID
144
146
145
-
Get the current tenant ID from the `WEBSITE_AUTH_OPENID_ISSUER` environment variable. The ID just needs to be parsed out of the variable with a regular expression.
147
+
1. Open the `./backend/src/with-graph/auth.js` file.
3. This function gets the current tenant ID from the `WEBSITE_AUTH_OPENID_ISSUER` environment variable. The ID is parsed out of the variable with a regular expression.
156
162
157
163
### Configure MSAL.js
158
164
159
-
Build the MSAL.js configuration object.
160
-
161
-
```javascript
162
-
// ./backend/src/auth.js
163
-
// Exchange current bearerToken for Graph API token
1. Still in the `./backend/src/with-graph/auth.js` file, review the `getGraphToken()` function.
166
+
1. Build the MSAL.js configuration object, use the MSAL configuration to create the clientCredentialAuthority. Configure the on-behalf-off request. Then use the acquireTokenOnBehalfOf to exchange the backend API access token for a Graph access token.
167
+
168
+
```javascript
169
+
// ./backend/src/auth.js
170
+
// Exchange current bearerToken for Graph API token
171
+
// Env vars were set by App Service
172
+
export async function getGraphToken(backEndAccessToken) {
## 5. Review backend code to access Microsoft Graph with the new token
213
222
214
-
## 5. Get the user's profile from Microsoft Graph
223
+
To access Microsoft Graph as a user signed in to the frontend application, the changes include:
215
224
216
-
Before the token exchange, remember that the steps included:
217
-
* Configuration of the Active Directory app registration with an API permission to Microsoft Graph with the scope `User.Read`.
225
+
* Configuration of the Active Directory app registration with an API permission to the downstream service, Microsoft Graph, with the necessary scope of `User.Read`.
218
226
* Grant admin consent to bypass the user consent screen for the back-end app.
219
-
* Change the application code to convert the access token sent from the front-end app to an access token with the required permission for Microsoft Graph.
227
+
* Change the application code to convert the access token sent from the front-end app to an access token with the required permission for the downstream service, Microsoft Graph.
220
228
221
229
Now that the code has the correct token for Microsoft Graph, use it to create a client to Microsoft Graph then get the user's profile.
222
230
223
-
The following code is already provided for you in the sample app. You need to understand why it's there and how it works so that you can apply this work to other apps you build that need this same functionality.
@@ -271,7 +281,7 @@ export async function getGraphProfile(accessToken) {
271
281
272
282
#### I got an error `80049217`, what does it mean?
273
283
274
-
This error, `CompactToken parsing failed with error code: 80049217`, means the backend App service isn't authorized to return the Microsoft Graph token.
284
+
This error, `CompactToken parsing failed with error code: 80049217`, means the backend App service isn't authorized to return the Microsoft Graph token. This error is caused because the app registration is missing the `User.Read` permission.
275
285
276
286
#### I got an error `AADSTS65001`, what does it mean?
0 commit comments