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
+36-28Lines changed: 36 additions & 28 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -22,20 +22,24 @@ ms.custom: aaddev
22
22
23
23
# Protected web API: Code configuration
24
24
25
-
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.
25
+
To configure the code for your protected web API, you need to understand:
26
26
27
-
## What defines ASP.NET/ASP.NET Core APIs as protected?
27
+
- What defines APIs as protected.
28
+
- How to configure a bearer token.
29
+
- How to validate the token.
28
30
29
-
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.
31
+
## What defines ASP.NET and ASP.NET Core APIs as protected?
32
+
33
+
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.
30
34
31
35
Consider the following questions:
32
36
33
-
- How does the web API know the identity of the app that calls it? (Only an app can call a web API.)
34
-
- If the app called the web API on behalf of a user, what's the user's identity?
37
+
-Only an app can call a web API. How does the API know the identity of the app that calls it?
38
+
- If the app calls the API on behalf of a user, what's the user's identity?
35
39
36
40
## Bearer token
37
41
38
-
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.
42
+
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.
39
43
40
44
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):
Whenanappiscalledonacontrolleractionthatholdsan**[Authorize]**attribute, ASP.NETandASP.NETCoreextracttheaccesstokenfromtheAuthorizationheader's bearer token. The access token is then forwarded to the JwtBearer middleware, which calls Microsoft IdentityModel Extensions for .NET.
Currently, theASP.NETCoretemplatescreateAzureActiveDirectory (AzureAD) webAPIsthatsigninuserswithinyourorganizationoranyorganization. Theydon'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 Startup.cs:
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
133
+
TheprecedingcodesnippetisextractedfromtheASP.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 more than the snippet shows, 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.
145
+
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.
140
146
141
147
### Validators
142
148
143
-
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).
149
+
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).
|**ValidateIssuer**|EnsuresthetokenwasissuedbyatrustedSTS, meaningit's from someone you trust. |
157
+
|**ValidateIssuerSigningKey**|Ensurestheapplicationvalidatingthetokentruststhekeythatwasusedtosignthetoken. There's a special case where the key is embedded in the token. But this case doesn'tusuallyarise. |
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.
164
+
Inmostcases, youdon't need to change the parameters. Apps that aren'tsingletenantsareexceptions. ThesewebappsacceptusersfromanyorganizationorfrompersonalMicrosoftaccounts.Issuersinthiscasemustbevalidated.
157
165
158
166
## Token validation in Azure Functions
159
167
160
-
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).
168
+
YoucanalsovalidateincomingaccesstokensinAzureFunctions. Youcanfindexamplesofsuchvalidationin[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