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
Copy file name to clipboardExpand all lines: articles/active-directory/develop/scenario-web-app-call-api-acquire-token.md
+29-32Lines changed: 29 additions & 32 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
---
2
-
title: Get a token in Web apps that call web APIs - Microsoft identity platform | Azure
3
-
description: Learn how to build a Web app that calls web APIs (acquiring a token for the app)
2
+
title: Get a token in a web app that calls web APIs - Microsoft identity platform | Azure
3
+
description: Learn how to acquire a token for a web app that calls web APIs
4
4
services: active-directory
5
5
documentationcenter: dev-center-name
6
6
author: jmprieur
@@ -15,19 +15,19 @@ ms.workload: identity
15
15
ms.date: 10/30/2019
16
16
ms.author: jmprieur
17
17
ms.custom: aaddev
18
-
#Customer intent: As an application developer, I want to know how to write a Web app that calls web APIs using the Microsoft identity platform for developers.
18
+
#Customer intent: As an application developer, I want to know how to write a web app that calls web APIs by using the Microsoft identity platform for developers.
19
19
---
20
20
21
-
# Web app that calls web APIs - acquire a token for the app
21
+
# A web app that calls web APIs: Acquire a token for the app
22
22
23
-
Now that you have built you client application object, you'll use it to acquire a token to call a web API. In ASP.NET or ASP.NET Core, calling a web API is then done in the controller. It's about:
23
+
You've built your client application object. Now, you'll use it to acquire a token to call a web API. In ASP.NET or ASP.NET Core, calling a web API is done in the controller:
24
24
25
-
-Getting a token for the web API using the token cache. To get this token, you call `AcquireTokenSilent`.
26
-
-Calling the protected API with the access token.
25
+
-Get a token for the web API by using the token cache. To get this token, you call the `AcquireTokenSilent` method.
26
+
-Call the protected API, passing the access token to it as a parameter.
27
27
28
28
# [ASP.NET Core](#tab/aspnetcore)
29
29
30
-
The controller methods are protected by an `[Authorize]` attribute that forces users being authenticated to use the Web App. Here is the code that calls Microsoft Graph.
30
+
The controller methods are protected by an `[Authorize]` attribute that forces users being authenticated to use the web app. Here's the code that calls Microsoft Graph:
31
31
32
32
```csharp
33
33
[Authorize]
@@ -40,54 +40,51 @@ public class HomeController : Controller
40
40
this.tokenAcquisition=tokenAcquisition;
41
41
}
42
42
43
-
// Code for the controller actions(see code below)
43
+
// Code for the controller actions(see code below)
44
44
45
45
}
46
46
```
47
47
48
-
The `ITokenAcquisition` service is injected by ASP.NET through dependency injection.
48
+
The `ITokenAcquisition` service is injected by ASP.NET by using dependency injection.
49
49
50
-
51
-
Here is a simplified code of the action of the HomeController, which gets a token to call the Microsoft Graph.
50
+
Here's simplified code for the action of the `HomeController`, which gets a token to call Microsoft Graph:
To understand more thoroughly the code required for this scenario, see the phase 2 ([2-1-Web App Calls Microsoft Graph](https://github.com/Azure-Samples/active-directory-aspnetcore-webapp-openidconnect-v2/tree/master/2-WebApp-graph-user/2-1-Call-MSGraph)) step of the [ms-identity-aspnetcore-webapp-tutorial](https://github.com/Azure-Samples/ms-identity-aspnetcore-webapp-tutorial) tutorial.
66
+
To better understand the code required for this scenario, see the phase 2 ([2-1-Web App Calls Microsoft Graph](https://github.com/Azure-Samples/active-directory-aspnetcore-webapp-openidconnect-v2/tree/master/2-WebApp-graph-user/2-1-Call-MSGraph)) step of the [ms-identity-aspnetcore-webapp-tutorial](https://github.com/Azure-Samples/ms-identity-aspnetcore-webapp-tutorial) tutorial.
68
67
69
-
There are many additional complexities, such as:
68
+
There are other complex variations, such as:
70
69
71
-
- Calling several APIs,
72
-
-processing incremental consent and Conditional Access.
70
+
- Calling several APIs.
71
+
-Processing incremental consent and conditional access.
73
72
74
-
These advanced steps are processed in chapter 3 of the tutorial [3-WebApp-multi-APIs](https://github.com/Azure-Samples/active-directory-aspnetcore-webapp-openidconnect-v2/tree/master/3-WebApp-multi-APIs)
73
+
These advanced steps are covered in chapter 3 of the [3-WebApp-multi-APIs](https://github.com/Azure-Samples/active-directory-aspnetcore-webapp-openidconnect-v2/tree/master/3-WebApp-multi-APIs) tutorial.
75
74
76
75
# [ASP.NET](#tab/aspnet)
77
76
78
-
Things are similar in ASP.NET:
77
+
The code for ASP.NET is similar to the code shown for ASP.NET Core:
79
78
80
-
- A controller action protected by an [Authorize] attribute extracts the tenant ID and user ID of the `ClaimsPrincipal` member of the controller. (ASP.NET uses `HttpContext.User`.)
81
-
- From there, it builds an MSAL.NET `IConfidentialClientApplication`.
79
+
- A controller action, protected by an [Authorize] attribute, extracts the tenant ID and user ID of the `ClaimsPrincipal` member of the controller. (ASP.NET uses `HttpContext.User`.)
80
+
- From there, it builds an MSAL.NET `IConfidentialClientApplication` object.
82
81
- Finally, it calls the `AcquireTokenSilent` method of the confidential client application.
83
82
84
-
The code is similar to the code shown for ASP.NET Core.
85
-
86
83
# [Java](#tab/java)
87
84
88
-
In the Java sample, the code that calls an API is in the getUsersFromGraph method [AuthPageController.java#L62](https://github.com/Azure-Samples/ms-identity-java-webapp/blob/d55ee4ac0ce2c43378f2c99fd6e6856d41bdf144/src/main/java/com/microsoft/azure/msalwebsample/AuthPageController.java#L62).
85
+
In the Java sample, the code that calls an API is in the getUsersFromGraph method in [AuthPageController.java#L62](https://github.com/Azure-Samples/ms-identity-java-webapp/blob/d55ee4ac0ce2c43378f2c99fd6e6856d41bdf144/src/main/java/com/microsoft/azure/msalwebsample/AuthPageController.java#L62).
89
86
90
-
It attempts to call `getAuthResultBySilentFlow`. If the user needs to consent to more scopes, the code processes the `MsalInteractionRequiredException` to challenge the user.
87
+
The method attempts to call `getAuthResultBySilentFlow`. If the user needs to consent to more scopes, the code processes the `MsalInteractionRequiredException` object to challenge the user.
91
88
92
89
```java
93
90
@RequestMapping("/msal4jsample/graph/me")
@@ -101,8 +98,8 @@ public ModelAndView getUserFromGraph(HttpServletRequest httpRequest, HttpServlet
101
98
} catch (ExecutionException e) {
102
99
if (e.getCause() instanceofMsalInteractionRequiredException) {
103
100
104
-
// If silent call returns MsalInteractionRequired, then redirect to Authorization endpoint
105
-
// so user can consent to new scopes
101
+
// If the silent call returns MsalInteractionRequired, redirect to authorization endpoint
102
+
// so user can consent to new scopes.
106
103
String state =UUID.randomUUID().toString();
107
104
String nonce =UUID.randomUUID().toString();
108
105
@@ -142,22 +139,22 @@ public ModelAndView getUserFromGraph(HttpServletRequest httpRequest, HttpServlet
142
139
}
143
140
return mav;
144
141
}
145
-
// Code omitted here.
142
+
// Code omitted here
146
143
```
147
144
148
145
# [Python](#tab/python)
149
146
150
-
In the python sample, the code calling Microsoft graph is in [app.py#L53-L62](https://github.com/Azure-Samples/ms-identity-python-webapp/blob/48637475ed7d7733795ebeac55c5d58663714c60/app.py#L53-L62).
147
+
In the Python sample, the code that calls Microsoft Graph is in [app.py#L53-L62](https://github.com/Azure-Samples/ms-identity-python-webapp/blob/48637475ed7d7733795ebeac55c5d58663714c60/app.py#L53-L62).
151
148
152
-
It attempts to get a token from the token cache, and then calls the web API after setting the authorization header. If it can't, it re-signs in the user.
149
+
The code attempts to get a token from the token cache. Then, after setting the authorization header, it calls the web API. If it can't get a token, it signs the user in again.
153
150
154
151
```python
155
152
@app.route("/graphcall")
156
153
defgraphcall():
157
154
token = _get_token_from_cache(app_config.SCOPE)
158
155
ifnot token:
159
156
return redirect(url_for("login"))
160
-
graph_data = requests.get( # Use token to call downstream service
157
+
graph_data = requests.get( # Use token to call downstream service.
0 commit comments