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: README.md
+74-9Lines changed: 74 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -27,12 +27,22 @@ endpoint: AAD v2.0
27
27
-[Validating the claims](#validating-the-claims)
28
28
-[Prerequisites](#prerequisites)
29
29
-[Setup](#setup)
30
+
-[Step 1: Clone or download this repository](#step-1--clone-or-download-this-repository)
31
+
-[Register the sample application(s) with your Azure Active Directory tenant](#register-the-sample-applications-with-your-azure-active-directory-tenant)
32
+
-[Choose the Azure AD tenant where you want to create your applications](#choose-the-azure-ad-tenant-where-you-want-to-create-your-applications)
33
+
-[Register the service app (TodoListService-ManualJwt)](#register-the-service-app-todolistservice-manualjwt)
34
+
-[Register the client app (TodoListClient-ManualJwt)](#register-the-client-app-todolistclient-manualjwt)
35
+
-[Running the sample](#running-the-sample)
30
36
-[Explore the sample](#explore-the-sample)
31
37
-[About The Code](#about-the-code)
38
+
-[Providing your own Custom token validation handler](#providing-your-own-custom-token-validation-handler)
32
39
-[How To Recreate This Sample](#how-to-recreate-this-sample)
33
40
-[Creating the TodoListService-ManualJwt Project](#creating-the-todolistservice-manualjwt-project)
34
41
-[Creating the TodoListClient Project](#creating-the-todolistclient-project)
35
42
-[How to deploy this sample to Azure](#how-to-deploy-this-sample-to-azure)
43
+
-[Create and publish the `TodoListService-ManualJwt` to an Azure Web Site](#create-and-publish-the-todolistservice-manualjwt-to-an-azure-web-site)
44
+
-[Update the Active Directory tenant application registration for `TodoListService-ManualJwt`](#update-the-active-directory-tenant-application-registration-for-todolistservice-manualjwt)
45
+
-[Update the `TodoListClient-ManualJwt` to call the `TodoListService-ManualJwt` Running in Azure Web Sites](#update-the-todolistclient-manualjwt-to-call-the-todolistservice-manualjwt-running-in-azure-web-sites)
36
46
-[Azure Government Deviations](#azure-government-deviations)
37
47
-[Troubleshooting](#troubleshooting)
38
48
-[Community Help and Support](#community-help-and-support)
@@ -257,24 +267,79 @@ Explore the sample by signing in, adding items to the To Do list, removing the u
257
267
258
268
## About The Code
259
269
260
-
The manual JWT validation occurs in the [TokenValidationHandler](https://github.com/Azure-Samples/active-directory-dotnet-webapi-manual-jwt-validation/blob/master/TodoListService-ManualJwt/Global.asax.cs#L58) implementation in the `Global.aspx.cs` file in the TodoListService-ManualJwt project. Each time a call is made to the web API, the [TokenValidationHandler.SendAsync()](https://github.com/Azure-Samples/active-directory-dotnet-webapi-manual-jwt-validation/blob/4b80657c5506c8cb30af67b9f61bb6aa68dfca58/TodoListService-ManualJwt/Global.asax.cs#L80) handler is executed:
270
+
The manual JWT validation occurs in the [TokenValidationHandler](https://github.com/Azure-Samples/active-directory-dotnet-webapi-manual-jwt-validation/blob/master/TodoListService-ManualJwt/Global.asax.cs#L58) implementation in the `Global.aspx.cs` file in the TodoListService-ManualJwt project.
271
+
Each time a call is made to the web API, the [TokenValidationHandler.SendAsync()](https://github.com/Azure-Samples/active-directory-dotnet-webapi-manual-jwt-validation/blob/4b80657c5506c8cb30af67b9f61bb6aa68dfca58/TodoListService-ManualJwt/Global.asax.cs#L80) handler is executed:
261
272
262
273
This method:
263
274
264
-
1. gets the token from the Authorization headers
265
-
1. gets the open ID configuration from the Azure AD discovery endpoint
266
-
1. ensures that the web API is consented to and provisioned in the Azure AD tenant from where the access token originated
267
-
1. verifies that the token has not expired
268
-
1. sets the parameters to validate:
275
+
1. Gets the token from the Authorization headers
276
+
1. Gets the open ID configuration, including keys from the Azure AD discovery endpoint
277
+
1. Sets the parameters to validate in `GetTokenValidationParameters()`
269
278
- the audience - the application accepts both its App ID URI and its AppID/clientID
270
279
- the valid issuers - the application accepts both Azure AD V1 and Azure AD V2
271
-
272
-
1. then it delegates to the `JwtSecurityTokenHandler` class (provided by the `Microsoft.IdentityModel.Tokens` library)
280
+
1. Then the token is validated
281
+
1. An asp.net claims principal is created after a successful validation
282
+
1. ensures that the web API is consented to and provisioned in the Azure AD tenant from where the access token originated
283
+
1. Finally, a check for scopes that the web API expects from the caller is carried out
273
284
274
285
The `TokenValidationHandler` class is registered with ASP.NET in the `TodoListService-ManualJwt/Global.asx.cs` file, in the [Application_Start()](https://github.com/Azure-Samples/active-directory-dotnet-webapi-manual-jwt-validation/blob/4b80657c5506c8cb30af67b9f61bb6aa68dfca58/TodoListService-ManualJwt/Global.asax.cs#L54) method.
275
286
276
287
For more validation options, please refer to [TokenValidationParameters.cs](https://docs.microsoft.com/dotnet/api/microsoft.identitymodel.tokens.tokenvalidationparameters?view=azure-dotnet)
277
288
289
+
### Providing your own Custom token validation handler
290
+
291
+
If you do not wish to control the token validation from its very beginning to the end as laid out in the `Global.asax.cs`, but only limit yourself to validate business logic based on claims in the presented token, you can craft a Custom token handler as provided in the example below.
292
+
The provided example, validates to allow callers from a list of whitelisted tenants only.
- [Quickstart: Register an application with the Microsoft identity platform](https://docs.microsoft.com/azure/active-directory/develop/quickstart-register-app)
392
457
- [Quickstart: Configure a client application to access web APIs](https://docs.microsoft.com/azure/active-directory/develop/quickstart-configure-app-access-web-apis)
// The ConfigurationManager class holds properties to control the metadata refresh interval. For more details, https://docs.microsoft.com/en-us/dotnet/api/microsoft.identitymodel.protocols.configurationmanager-1?view=azure-dotnet
0 commit comments