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-protected-web-api-app-configuration.md
+28-24Lines changed: 28 additions & 24 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -17,7 +17,7 @@ ms.workload: identity
17
17
ms.date: 05/07/2019
18
18
ms.author: jmprieur
19
19
ms.custom: aaddev
20
-
#Customer intent: As an application developer, I want to know how to write a protected web API using the Microsoft identity platform for developers.
20
+
#Customer intent: As a software developer, I want to know how to write a protected web API using the Microsoft identity platform for developers.
21
21
ms.collection: M365-identity-device-management
22
22
---
23
23
@@ -27,16 +27,16 @@ To configure the code for your protected web API, you need to understand what de
27
27
28
28
## What defines ASP.NET/ASP.NET Core APIs as protected?
29
29
30
-
Like web apps, the ASP.NET/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 identity that's authorized.
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.
31
31
32
32
Consider the following questions:
33
33
34
-
- How does the web API know the identity of the app that calls it? (Only an app can call a web API.)
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
35
- If the app called the web API on behalf of a user, what's the user's identity?
36
36
37
37
## Bearer token
38
38
39
-
The information about the identity of the app, and about the user (unless the web app accepts service-to-service calls from a daemon app), is held in the bearer token that's set in the header when the app is called.
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.
40
40
41
41
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):
ThiscodesnippetisextractedfromtheASP.NETCoreWebApiincrementaltutorialin [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 the Startup.cs
130
+
ThisprecedingcodesnippetisextractedfromtheASP.NETCoreWebApiincrementaltutorialin [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.
Therecanalsobespecialvalidations. Forexample, it's possible to validate that signing keys (when embedded in a token) are trusted and that the token isn'tbeingreplayed. Finally, someprotocolsrequirespecificvalidations.
142
+
Therecanalsobespecialvalidations. Forexample, it's possible to validate that signing keys, when embedded in a token, are trusted and that the token isn'tbeingreplayed. Also, someprotocolsrequirespecificvalidations.
141
143
142
144
### Validators
143
145
144
-
Thevalidationstepsarecapturedinvalidators, whichareallinthe [MicrosoftIdentityModelExtensionsfor .NET](https://github.com/AzureAD/azure-activedirectory-identitymodel-extensions-for-dotnet) open-source library, in one source file: [Microsoft.IdentityModel.Tokens/Validators.cs](https://github.com/AzureAD/azure-activedirectory-identitymodel-extensions-for-dotnet/blob/master/src/Microsoft.IdentityModel.Tokens/Validators.cs).
146
+
Thevalidationstepsarecapturedinvalidators, whichareprovidedbythe [MicrosoftIdentityModelExtensionsfor .NET](https://github.com/AzureAD/azure-activedirectory-identitymodel-extensions-for-dotnet) open-source library. The validators are defined in the library source file [Microsoft.IdentityModel.Tokens/Validators.cs](https://github.com/AzureAD/azure-activedirectory-identitymodel-extensions-for-dotnet/blob/master/src/Microsoft.IdentityModel.Tokens/Validators.cs).
|**ValidateSignature**|Ensuresthetokenhasn't been tampered with. |
157
+
|**ValidateTokenReplay**|Ensuresthetokenisn't replayed. There is a special case for some onetime-use protocols. |
156
158
157
-
Thevalidatorsareallassociatedwithpropertiesofthe `TokenValidationParameters` class, themselvesinitializedfromtheASP.NET/ASP.NETCoreconfiguration. Inmostcases, youwon't have to change the parameters. There'soneexception, forappsthataren't single tenants. (That is, web apps that accept users from any organization or from personal Microsoft accounts.) In this case, the issuer must be validated.
159
+
Thevalidatorsareassociatedwithpropertiesofthe**TokenValidationParameters**class. ThepropertiesareinitializedfromtheASP.NETandASP.NETCoreconfiguration. Inmostcases, youwon't need to change the parameters.
160
+
161
+
There's one exception for apps that aren'tsingletenants. ThesearewebappsthatacceptusersfromanyorganizationorfrompersonalMicrosoftaccounts. Inthiscase, theissuermustbevalidated.
158
162
159
163
## Token validation in Azure Functions
160
164
161
-
It's also possible to validate incoming access tokens in Azure functions. You can find examples of validating tokens in Azure functions in [Dotnet](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).
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).
0 commit comments