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
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:
27
27
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.
29
31
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.
31
35
32
36
Consider the following questions:
33
37
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?
36
40
37
41
## Bearer token
38
42
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.
40
44
41
45
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 the Startup.cs file:
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.
134
+
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 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. Also, someprotocolsrequirespecificvalidations.
146
+
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.
143
147
144
148
### Validators
145
149
@@ -150,19 +154,19 @@ The validators are described in this table:
|**ValidateIssuer**|EnsuresthetokenwasissuedbyatrustedSTS, meaningit's from someone you trust. |
158
+
|**ValidateIssuerSigningKey**|Ensurestheapplicationvalidatingthetokentruststhekeythatwasusedtosignthetoken. There's a special case where the key is embedded in the token. But this case doesn'tusuallyarise. |
Thevalidatorsareassociatedwithpropertiesofthe**TokenValidationParameters**class. ThepropertiesareinitializedfromtheASP.NETandASP.NETCoreconfiguration. Inmostcases, youwon't need to change the parameters.
163
+
Thevalidatorsareassociatedwithpropertiesofthe**TokenValidationParameters**class. ThepropertiesareinitializedfromtheASP.NETandASP.NETCoreconfiguration. Inmostcases, youdon't need to change the parameters.
160
164
161
-
There's one exception for apps that aren'tsingletenants. ThesearewebappsthatacceptusersfromanyorganizationorfrompersonalMicrosoftaccounts. Inthiscase, theissuermustbevalidated.
165
+
Appsthataren'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.
162
166
163
167
## Token validation in Azure Functions
164
168
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
+
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