Skip to content

Commit 899d2e7

Browse files
authored
Merge pull request #102079 from mikematteson/seven-scenario-web-app-call-api-articles
Edit pass: seven-scenario-web-app-call-api-articles
2 parents 51f05ce + e20f3b9 commit 899d2e7

8 files changed

+218
-199
lines changed

articles/active-directory/develop/scenario-web-app-call-api-acquire-token.md

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
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
44
services: active-directory
55
documentationcenter: dev-center-name
66
author: jmprieur
@@ -15,19 +15,19 @@ ms.workload: identity
1515
ms.date: 10/30/2019
1616
ms.author: jmprieur
1717
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.
1919
---
2020

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
2222

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:
2424

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.
2727

2828
# [ASP.NET Core](#tab/aspnetcore)
2929

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:
3131

3232
```csharp
3333
[Authorize]
@@ -40,54 +40,51 @@ public class HomeController : Controller
4040
this.tokenAcquisition = tokenAcquisition;
4141
}
4242

43-
// Code for the controller actions(see code below)
43+
// Code for the controller actions (see code below)
4444
4545
}
4646
```
4747

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.
4949

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:
5251

5352
```csharp
5453
public async Task<IActionResult> Profile()
5554
{
56-
// Acquire the access token
55+
// Acquire the access token.
5756
string[] scopes = new string[]{"user.read"};
5857
string accessToken = await tokenAcquisition.GetAccessTokenOnBehalfOfUserAsync(scopes);
5958

60-
// use the access token to call a protected web API
59+
// Use the access token to call a protected web API.
6160
HttpClient client = new HttpClient();
6261
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
6362
string json = await client.GetStringAsync(url);
6463
}
6564
```
6665

67-
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.
6867

69-
There are many additional complexities, such as:
68+
There are other complex variations, such as:
7069

71-
- Calling several APIs,
72-
- processing incremental consent and Conditional Access.
70+
- Calling several APIs.
71+
- Processing incremental consent and conditional access.
7372

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.
7574

7675
# [ASP.NET](#tab/aspnet)
7776

78-
Things are similar in ASP.NET:
77+
The code for ASP.NET is similar to the code shown for ASP.NET Core:
7978

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.
8281
- Finally, it calls the `AcquireTokenSilent` method of the confidential client application.
8382

84-
The code is similar to the code shown for ASP.NET Core.
85-
8683
# [Java](#tab/java)
8784

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).
8986

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.
9188

9289
```java
9390
@RequestMapping("/msal4jsample/graph/me")
@@ -101,8 +98,8 @@ public ModelAndView getUserFromGraph(HttpServletRequest httpRequest, HttpServlet
10198
} catch (ExecutionException e) {
10299
if (e.getCause() instanceof MsalInteractionRequiredException) {
103100

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.
106103
String state = UUID.randomUUID().toString();
107104
String nonce = UUID.randomUUID().toString();
108105

@@ -142,22 +139,22 @@ public ModelAndView getUserFromGraph(HttpServletRequest httpRequest, HttpServlet
142139
}
143140
return mav;
144141
}
145-
// Code omitted here.
142+
// Code omitted here
146143
```
147144

148145
# [Python](#tab/python)
149146

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).
151148

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.
153150

154151
```python
155152
@app.route("/graphcall")
156153
def graphcall():
157154
token = _get_token_from_cache(app_config.SCOPE)
158155
if not token:
159156
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.
161158
app_config.ENDPOINT,
162159
headers={'Authorization': 'Bearer ' + token['access_token']},
163160
).json()

0 commit comments

Comments
 (0)