What causes the handler below to check the token for the tenant and audience?
By default, middleware runs in Active mode, so it will try to find a token in every request. If he finds one, he will try to check it. If it discovers that it is valid, a ClaimsPrincipal is created, available in the OWIN middleware and web API add-ons.
It also downloads the public keys, with which it verifies the signature of the token when starting the application from Azure AD. You can see this if you use a tool like Fiddler.
How to do this to find a token to execute a handler?
I am not sure if I understand this question, I hope that my answer above clarified this process.
Setting SaveSigninToken to true saves the token. How can I get a token, as well as purchase an access token for the Graph API from this token?
What you are trying to do is call the API using the on-behalf-of stream. Here you can find an example application: https://github.com/Azure-Samples/active-directory-dotnet-webapi-onbehalfof . More specifically, this part should interest you: https://github.com/Azure-Samples/active-directory-dotnet-webapi-onbehalfof/blob/master/TodoListService/Controllers/TodoListController.cs#L133 .
ClientCredential clientCred = new ClientCredential(clientId, appKey); var bootstrapContext = ClaimsPrincipal.Current.Identities.First().BootstrapContext as System.IdentityModel.Tokens.BootstrapContext; string userName = ClaimsPrincipal.Current.FindFirst(ClaimTypes.Upn) != null ? ClaimsPrincipal.Current.FindFirst(ClaimTypes.Upn).Value : ClaimsPrincipal.Current.FindFirst(ClaimTypes.Email).Value; string userAccessToken = bootstrapContext.Token; UserAssertion userAssertion = new UserAssertion(bootstrapContext.Token, "urn:ietf:params:oauth:grant-type:jwt-bearer", userName); string authority = String.Format(CultureInfo.InvariantCulture, aadInstance, tenant); string userId = ClaimsPrincipal.Current.FindFirst(ClaimTypes.NameIdentifier).Value; AuthenticationContext authContext = new AuthenticationContext(authority, new DbTokenCache(userId)); // In the case of a transient error, retry once after 1 second, then abandon. // Retrying is optional. It may be better, for your application, to return an error immediately to the user and have the user initiate the retry. bool retry = false; int retryCount = 0; do { retry = false; try { result = await authContext.AcquireTokenAsync(graphResourceId, clientCred, userAssertion); accessToken = result.AccessToken; } catch (AdalException ex) { if (ex.ErrorCode == "temporarily_unavailable") { // Transient error, OK to retry. retry = true; retryCount++; Thread.Sleep(1000); } } } while ((retry == true) && (retryCount < 1));