Skip to content

Commit 32c980f

Browse files
author
BobbySchmidt2
committed
changing repos
1 parent c325f77 commit 32c980f

4 files changed

+89
-83
lines changed

articles/active-directory/develop/scenario-protected-web-api-app-configuration.md

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,24 @@ ms.collection: M365-identity-device-management
2323

2424
# Protected web API: Code configuration
2525

26-
To configure the code for your protected web API, you need to understand what defines APIs as protected, how to configure a bearer token, and how to validate the token.
26+
To configure the code for your protected web API, you need to understand:
2727

28-
## What defines ASP.NET/ASP.NET Core APIs as protected?
28+
- What defines APIs as protected.
29+
- How to configure a bearer token.
30+
- How to validate the token.
2931

30-
Like web apps, the ASP.NET and ASP.NET Core web APIs are protected because their controller actions are prefixed with the `[Authorize]` attribute. So the controller actions can be called only if the API is called with an authorized identity.
32+
## What defines ASP.NET and ASP.NET Core APIs as protected?
33+
34+
Like web apps, the ASP.NET and ASP.NET Core web APIs are protected because their controller actions are prefixed with the **[Authorize]** attribute. The controller actions can be called only if the API is called with an authorized identity.
3135

3236
Consider the following questions:
3337

34-
- How does the web API know the identity of the app that calls it, since only an app can call a web API?
35-
- If the app called the web API on behalf of a user, what's the user's identity?
38+
- Only an app can call a web API. How does the API know the identity of the app that calls it?
39+
- If the app calls the API on behalf of a user, what's the user's identity?
3640

3741
## Bearer token
3842

39-
Unless the web app accepts service-to-service calls from a daemon app, information about the app identity and about the user is held in the bearer token that's set in the header when the app is called.
43+
The bearer token that's set in the header when the app is called holds information about the app identity. It also holds information about the user unless the web app accepts service-to-service calls from a daemon app.
4044

4145
Here's a C# code example that shows a client calling the API after it acquires a token with Microsoft Authentication Library for .NET (MSAL.NET):
4246

@@ -55,7 +59,7 @@ HttpResponseMessage response = await _httpClient.GetAsync(apiUri);
5559
> [!IMPORTANT]
5660
> A client application requests the bearer token to the Microsoft identity platform endpoint *for the web API*. The web API is the only application that should verify the token and view the claims it contains. Client apps should never try to inspect the claims in tokens.
5761
>
58-
> In the future, the web API could require that the token be encrypted. This requirement would prevent access for client apps that can view access tokens.
62+
> In the future, the web API might require that the token be encrypted. This requirement would prevent access for client apps that can view access tokens.
5963

6064
## JwtBearer configuration
6165

@@ -91,9 +95,9 @@ This section describes how to configure a bearer token.
9195

9296
### Code initialization
9397

94-
When an app is called on a controller action that holds an `[Authorize]` attribute, ASP.NET and ASP.NET Core look at the bearer token in the Authorization header of the calling request and extracts the access token. The token is then forwarded to the JwtBearer middleware, which calls Microsoft IdentityModel Extensions for .NET.
98+
When an app is called on a controller action that holds an **[Authorize]** attribute, ASP.NET and ASP.NET Core extract the access token from the Authorization header's bearer token. The access token is then forwarded to the JwtBearer middleware, which calls Microsoft IdentityModel Extensions for .NET.
9599

96-
In ASP.NET Core, this middleware is initialized in the Startup.cs file:
100+
In ASP.NET Core, this middleware is initialized in the Startup.cs file.
97101

98102
```csharp
99103
using Microsoft.AspNetCore.Authentication.JwtBearer;
@@ -105,7 +109,7 @@ The middleware is added to the web API by this instruction:
105109
services.AddAzureAdBearer(options => Configuration.Bind("AzureAd", options));
106110
```
107111

108-
Currently, the ASP.NET Core templates create Azure Active Directory (Azure AD) web APIs that sign in users within your organization or any organization. They do not sign in users with personal accounts. But you can easily change the templates to use the Microsoft identity platform endpoint by adding this code to the Startup.cs file:
112+
Currently, the ASP.NET Core templates create Azure Active Directory (Azure AD) web APIs that sign in users within your organization or any organization. They don't sign in users with personal accounts. But you can change the templates to use the Microsoft identity platform endpoint by adding this code to the Startup.cs file:
109113

110114
```csharp
111115
services.Configure<JwtBearerOptions>(AzureADDefaults.JwtBearerAuthenticationScheme, options =>
@@ -127,19 +131,19 @@ services.Configure<JwtBearerOptions>(AzureADDefaults.JwtBearerAuthenticationSche
127131
});
128132
```
129133

130-
This preceding code snippet is extracted from the ASP.NET Core Web Api incremental tutorial in [Microsoft.Identity.Web/WebApiServiceCollectionExtensions.cs#L50-L63](https://github.com/Azure-Samples/active-directory-dotnet-native-aspnetcore-v2/blob/154282843da2fc2958fad151e2a11e521e358d42/Microsoft.Identity.Web/WebApiServiceCollectionExtensions.cs#L50-L63). The **AddProtectedWebApi** method, which does a lot more, is called from Startup.cs.
134+
This preceding code snippet is extracted from the ASP.NET Core web API incremental tutorial in [Microsoft.Identity.Web/WebApiServiceCollectionExtensions.cs#L50-L63](https://github.com/Azure-Samples/active-directory-dotnet-native-aspnetcore-v2/blob/154282843da2fc2958fad151e2a11e521e358d42/Microsoft.Identity.Web/WebApiServiceCollectionExtensions.cs#L50-L63). The **AddProtectedWebApi** method, which does more than the snippet shows, is called from Startup.cs.
131135
132136
## Token validation
133137

134-
In the preceding snippet, the JwtBearer middleware, like the OpenID Connect middleware in web apps, is directed by the value of `TokenValidationParameters` to validate the token. The token is decrypted as needed, the claims are extracted, and the signature is verified. The middleware then validates the token by checking for this data:
138+
In the preceding snippet, the JwtBearer middleware, like the OpenID Connect middleware in web apps, validates the token based on the value of `TokenValidationParameters`. The token is decrypted as needed, the claims are extracted, and the signature is verified. The middleware then validates the token by checking for this data:
135139

136140
- Audience: The token is targeted for the web API.
137-
- Sub: The token was issued for an app that's allowed to call the web API.
138-
- Issuer: The token was issued by a trusted security token service (STS).
139-
- Expiry: The token's lifetime is in range.
140-
- Signature: The token wasn't tampered with.
141+
- Sub: It was issued for an app that's allowed to call the web API.
142+
- Issuer: It was issued by a trusted security token service (STS).
143+
- Expiry: Its lifetime is in range.
144+
- Signature: It wasn't tampered with.
141145

142-
There can also be special validations. For example, it's possible to validate that signing keys, when embedded in a token, are trusted and that the token isn't being replayed. Also, some protocols require specific validations.
146+
There can also be special validations. For example, it's possible to validate that signing keys, when embedded in a token, are trusted and that the token isn't being replayed. Finally, some protocols require specific validations.
143147

144148
### Validators
145149

@@ -150,19 +154,19 @@ The validators are described in this table:
150154
| Validator | Description |
151155
|---------|---------|
152156
| **ValidateAudience** | Ensures the token is for the application that validates the token for you. |
153-
| **ValidateIssuer** | Ensures the token was issued by a trusted STS, meaning from someone you trust. |
154-
| **ValidateIssuerSigningKey** | Ensures the application validating the token trusts the key that was used to sign the token. The is a special case where the key is embedded in the token. But this case does not usually arise. |
155-
| **ValidateLifetime** | Ensures the token is still or already valid. The validator checks if the lifetime of the token, meaning the **notbefore** and **expires** claims, is in range. |
157+
| **ValidateIssuer** | Ensures the token was issued by a trusted STS, meaning it's from someone you trust. |
158+
| **ValidateIssuerSigningKey** | Ensures the application validating the token trusts the key that was used to sign the token. There's a special case where the key is embedded in the token. But this case doesn't usually arise. |
159+
| **ValidateLifetime** | Ensures the token is still or already valid. The validator checks if the lifetime of the token is in the range specified by the **notbefore** and **expires** claims. |
156160
| **ValidateSignature** | Ensures the token hasn't been tampered with. |
157-
| **ValidateTokenReplay** | Ensures the token isn't replayed. There is a special case for some onetime-use protocols. |
161+
| **ValidateTokenReplay** | Ensures the token isn't replayed. There's a special case for some onetime-use protocols. |
158162

159-
The validators are associated with properties of the **TokenValidationParameters** class. The properties are initialized from the ASP.NET and ASP.NET Core configuration. In most cases, you won't need to change the parameters.
163+
The validators are associated with properties of the **TokenValidationParameters** class. The properties are initialized from the ASP.NET and ASP.NET Core configuration. In most cases, you don't need to change the parameters.
160164

161-
There's one exception for apps that aren't single tenants. These are web apps that accept users from any organization or from personal Microsoft accounts. In this case, the issuer must be validated.
165+
Apps that aren't single tenants are exceptions. These web apps accept users from any organization or from personal Microsoft accounts. In this case, issuers must be validated.
162166

163167
## Token validation in Azure Functions
164168

165-
It's also possible to validate incoming access tokens in Azure Functions. You can find examples of validating tokens in Azure Functions in [Microsoft .NET](https://github.com/Azure-Samples/ms-identity-dotnet-webapi-azurefunctions), [NodeJS](https://github.com/Azure-Samples/ms-identity-nodejs-webapi-azurefunctions), and [Python](https://github.com/Azure-Samples/ms-identity-python-webapi-azurefunctions).
169+
You can also validate incoming access tokens in Azure Functions. You can find examples of such validation in [Microsoft .NET](https://github.com/Azure-Samples/ms-identity-dotnet-webapi-azurefunctions), [NodeJS](https://github.com/Azure-Samples/ms-identity-nodejs-webapi-azurefunctions), and [Python](https://github.com/Azure-Samples/ms-identity-python-webapi-azurefunctions).
166170
167171
## Next steps
168172

0 commit comments

Comments
 (0)