services | platforms | author | level | client | service | endpoint |
---|---|---|---|---|---|---|
active-directory |
dotnet |
jmprieur |
400 |
.NET Desktop (WPF) |
ASP.NET Core Web API, Microsoft Graph |
AAD v2.0 |
ASP.NET Core 2.1 Web API calling Microsoft Graph, itself called from a WPF application using Azure AD V2
The sample in this folder is part of a multi-phase tutorial. This folder is about the second phase named Web API now calls Microsoft Graph. The first phase is available from 1. Desktop app calls Web API.
This article README-incremental-instructions.md builds on top of the README.md of the first part. If you want to see the full instructions on how to configure the sample, see README.md
At that time, the Azure AD v2.0 endpoint does not yet completely support the on-behalf-of flow for users signing-in with a Microsoft Personal account. Limitations are called out in the Current limitations section
- About this sample
- How to run this sample
- How was the code created
- Community Help and Support
- Contributing
- More information
You expose a Web API and you want to protect it so that only authenticated user can access it. You want to enable authenticated users with both work and school accounts or Microsoft personal accounts (formerly live account) to use your Web API. Your API calls a downstream API (Microsoft Graph) to provide added value to its client apps.
With respect to the previous part of the tutorial, this part adds the following steps: 3. When the client calls the Web API, the Web API acquires another token to call the Microsoft Graph (3) 4. then the Web API calls the graph
The user experience on the client application is similar to the one in the first part, except that, when the signed-in user adds todo list items, the Web API appends the name of the user to the todo item (between parenthesis). This is done by calling Microsoft Graph (even if in this particular case, this would not be strictly necessary)
From your shell or command line:
cd "aspnetcore-webapi\2. Web API now calls Microsoft Graph"
There are two projects in this sample. Each needs to be separately registered in your Azure AD tenant. To register these projects, you can:
- either follow the steps Step 2: Register the sample with your Azure Active Directory tenant and Step 3: Configure the sample to use your Azure AD tenant
- or use PowerShell scripts that:
- automatically creates the Azure AD applications and related objects (passwords, permissions, dependencies) for you
- modify the Visual Studio projects' configuration files.
If you want to use this automation:
-
On Windows run PowerShell and navigate to the root of the cloned directory
-
In PowerShell run:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process -Force
-
Run the script to create your Azure AD application and configure the code of the sample application accordinly.
.\AppCreationScripts\Configure.ps1
Other ways of running the scripts are described in App Creation Scripts
-
Once you've run the script, be sure to follow the manual steps. Indeed Azure AD PowerShell does not yet provide full control on applications consuming v2.0 tokens, even if this registration is already possible from the Azure portal:
- In the list of pages for the application registration of the TodoListService-v2 application, select Manifest
- in the manifest, search for "accessTokenAcceptedVersion", and replace null by 2. This property lets Azure AD know that the Web API accepts v2.0 tokens
- search for signInAudience and make sure it's set to AzureADandPersonalMicrosoftAccount
- Select Save
- In the Authentication page for the TodoListService-v2 application, check the
urn:ietf:wg:oauth:2.0:oob
reply URI so that the client can propose incremental consent to the user for the Web API when needed. - In tthe application registration page for the TodoListClient-v2 application, select the Manifest section:
- search for signInAudience and make sure it's set to AzureADandPersonalMicrosoftAccount
- Select Save
Tip: Get directly to the app registration portal page for a give app, you can navigate to the links provided in the AppCreationScripts\createdApps.html. This file is generated by the scripts during the app registration and configuration.
- In the list of pages for the application registration of the TodoListService-v2 application, select Manifest
-
Open the Visual Studio solution and click start
If you don't want to use this automation, follow the steps below
These instructions only show the differences with the first part.
- In App registrations (Preview) page, find the TodoListService-2 app
- From the Certificates & secrets page, in the Client secrets section, choose New client secret:
- Type a key description (of instance
app secret
), - Select a key duration of either In 1 year, In 2 years, or Never Expires.
- When you press the Add button, the key value will be displayed, copy, and save the value in a safe location.
- You'll need this key later to configure the project in Visual Studio. This key value will not be displayed again, nor retrievable by any other means, so record it as soon as it is visible from the Azure portal.
- Type a key description (of instance
- Select the API permissions section
- Click the Add a permission button and then,
- Ensure that the Microsoft APIs tab is selected
- In the Commonly used Microsoft APIs section, click on Microsoft Graph
- In the Delegated permissions section, ensure that the right permissions are checked: User.Read. Use the search box if necessary.
- Select the Add permissions button
- [Optional] if you are a tenant admin, and agree to grant the admin consent to the web api, select Grant admin consent for {your tenant domain}.
- [Otherwise] If you have not granted admin consent to the Web API in the previous optional step, select Authentication in the list of pages and there:
- Check the
urn:ietf:wg:oauth:2.0:oob
Redirect URI checkbox. This is so that the client can propose incremental consent to the user for the downstream web apis used by our TodoListService-v2 Web API. - Select Save
- Check the
- [Optional] Select the Manifest section and:
- in the manifest, search for "accessTokenAcceptedVersion", and see that its value is 2. This property lets Azure AD know that the Web API accepts v2.0 tokens
- Select Save
Important: it's up to the Web API to decide which version of token (v1.0 or v2.0) it accepts. Then when clients request a token for your Web API using the v2.0 endpoint, they'll get a token which version is accepted by the Web API. The code validating the tokens in this sample was written to accept both versions.
Nothing more to do more here. All was done in the first part
By default the sample is configured to enable users to sign in with any work and school accounts (AAD) accounts.
This constrain is ensured by ida:Tenant
in TodoListClient\App.Config
having the value organizations
.
- Open the solution in Visual Studio.
- In the TodoListService-v2 project, open the
appsettings.json
file. - Find the
ClientSecret
property and replace the existing value with the key you saved during the creation of theTodoListService-v2
app, in the Azure portal.
Nothing more to do more here. All was done in the first part
Clean the solution, rebuild the solution, and run it
The on-behalf-of flow works for Microsoft Personal accounts, but the consent is not yet rolled-up in the client for the user to consent to the Web API calling the downstream API (here Microsoft Graph). To make this work, the suggestion is:
- either to use the same client ID in the Client and the Service. This way the consent for the service will appear in the client.
- or to provide a protected page on the Web API (which therefore also becomes a Web app) so that the user can have an interaction
For details about the way the code to protect the Web API was created, see How was the code created section, of the README.md file located in the sibling folder named 1. Desktop app calls Web API.
This section, here, is only about the additional code added to let the Web API call the Microsoft Graph
Calling a downstream API involves getting a token for this Web API. Acquiring a token is achieved by using MSAL.NET.
Reference the Microsoft.Identity.Client
NuGet package from the TodoListService project.
Add a reference to the Microsoft.Identity.Web
library. It contains reusable code that you can use in your Web APIs (and web apps)
Update Startup.cs
file:
-
Add a using for
Microsoft.Identity.Client
-
In the
ConfigureServices
method, replace:services.AddAuthentication(AzureADDefaults.BearerAuthenticationScheme) .AddAzureADBearer(options => Configuration.Bind("AzureAd", options));
by
services.AddProtectWebApiWithMicrosoftIdentityPlatformV2(Configuration) .AddProtectedApiCallsWebApis(Configuration, new string[] { "user.read" }) .AddInMemoryTokenCaches();
AddProtectWebApiWithMicrosoftIdentityPlatformV2
does the following:- add the JwtBearerAuthenticationScheme (Note the replacement of BearerAuthenticationScheme by JwtBearerAuthenticationScheme)
- set the authority to be the Microsoft identity platform v2.0 identity
- sets the audiences to validate
- register an issuer validator that accepts issuers to be in the Microsoft identity platform clouds.
Here is an idea of the code:
services.AddAuthentication(AzureADDefaults.JwtBearerAuthenticationScheme) .AddAzureADBearer(options => configuration.Bind("AzureAd", options)); services.AddSession(); // Added services.Configure<JwtBearerOptions>(AzureADDefaults.JwtBearerAuthenticationScheme, options => { // This is an Azure AD v2.0 Web API options.Authority += "/v2.0"; // The valid audiences are both the Client ID (options.Audience) and api://{ClientID} options.TokenValidationParameters.ValidAudiences = new string[] { options.Audience, $"api://{options.Audience}" }; // Instead of using the default validation (validating against a single tenant // as we do in line of business apps), // we inject our own multi-tenant issuer validation logic // (which even accepts both V1 and V2 tokens) options.TokenValidationParameters.IssuerValidator = AadIssuerValidator.ForAadInstance(options.Authority).ValidateAadIssuer; });
The services that are added are:
- a token acquisition service leveraging MSAL.NET
- an in memory token cache
The implementations of these classes are in the Microsoft.Identity.Web library (and folder), and they are designed to be reusable in your applications (Web apps and Web apis)
AddProtectedApiCallsWebApis
subscribes to theOnTokenValidated
JwtBearerAuthentication event, and, in this event, adds the user account into MSAL.NET's user token cache by using the AcquireTokenOnBehalfOfUser method. This is done by theAddAccountToCacheFromJwt
method of theITokenAcquisition
micro-service, which wraps MSAL.NETservices.Configure<JwtBearerOptions>(AzureADDefaults.JwtBearerAuthenticationScheme, options => { // When an access token for our own Web API is validated, we add it to MSAL.NET's cache // so that it can be used from the controllers. options.Events = new JwtBearerEvents(); // Subscribing to OnTokenValidated to add the token to the cache using the OnBehalfOf flow options.Events.OnTokenValidated = async context => { var tokenAcquisition = context.HttpContext.RequestServices.GetRequiredService<ITokenAcquisition>(); var scopes = new string[] { "user.read" }; context.Success(); // Adds the token to the cache, and also handles the incremental consent and claim challenges tokenAcquisition.AddAccountToCacheFromJwt(context, scopes); await Task.FromResult(0); }; });
-
At the beginning of the
Configure
method, insertapp.UseSession()
. This code ensures that the session exists for the session-based token cache to work properly.public void Configure(IApplicationBuilder app, IHostingEnvironment env) { app.UseSession(); ...
In the TodoListController.cs
file, the Post() action was modified
todoStore.Add(new TodoItem { Owner = owner, Title = Todo.Title });
is replaced by:
ownerName = await CallGraphAPIOnBehalfOfUser();
string title = string.IsNullOrWhiteSpace(ownerName) ? Todo.Title : $"{Todo.Title} ({ownerName})";
todoStore.Add(new TodoItem { Owner = owner, Title = title });
The work of calling the Microsoft Graph to get the owner name is done in CallGraphAPIOnBehalfOfUser()
.
This method is the following. It:
-
gets a token for the Microsoft Graph on behalf of the user (leveraging the token, which was added in the cache on the
TokenValidated
event instartup.cs
) -
Calls the graph and retrieves the name of the user.
public async Task<string> CallGraphAPIOnBehalfOfUser() { string[] scopes = new string[] { "user.read" }; // we use MSAL.NET to get a token to call the API On Behalf Of the current user try { string accessToken = await tokenAcquisition.GetAccessTokenOnBehalfOfUser(HttpContext, scopes); dynamic me = await CallGraphApiOnBehalfOfUser(accessToken); return me.userPrincipalName; } catch (MsalUiRequiredException ex) { tokenAcquisition.ReplyForbiddenWithWwwAuthenticateHeader(HttpContext, scopes, ex); return string.Empty; } }
An interesting piece is how MsalUiRequiredException
are handled. These exceptions are typically sent by Azure AD when there is a need for a user interaction. This can be the case when the user needs to re-sign-in, or needs to grant some additional consent, or to obtain additional claims. For instance, the user might need to do multi-factor authentication required specifically by a specific downstream API. When these exceptions happen, given that the Web API does not have any UI, it needs to challenge the client passing all the information enabling this client to handle the interaction with the user.
This sample uses the ReplyForbiddenWithWwwAuthenticateHeader
method of the TokenAcquisition
service. This method uses the HttpResponse to:
- Send an HTTP 404 (Forbidden) to the client
- Set information in the www-Authenticate header of the HttpResponse with information that would enable a client to get more consent from the user that is:
- the client ID of our Web API
- the scopes to request
- the claims (for conditional access, MFA etc ...)
The code for this method is available in Microsoft.Identity.Web\Client\TokenAcquisition.cs L394-L427
On the client side, when it calls the Web API and receives a 403 with a www-Authenticate header, the client will call the HandleChallengeFromWebApi
method, which will
- extract from the www-Authenticate header
- the client ID
- the scopes
- the claims
- Create a new
PublicClientApplication
(as the client is here a desktop app), and callAcquireTokenAsync
method to ask the user to consent.
The code for HandleChallengeFromWebApi
method is available from TodoListClient\MainWindow.xaml.cs L180-L206
See section How to deploy this sample to Azure in the first part of this tutorial, as the deployment is exactly the same.
Use Stack Overflow to get support from the community.
Ask your questions on Stack Overflow first and browse existing issues to see if someone has asked your question before.
Make sure that your questions or comments are tagged with [msal
dotnet
].
If you find a bug in the sample, please raise the issue on GitHub Issues.
To provide a recommendation, visit the following User Voice page.
If you'd like to contribute to this sample, see CONTRIBUTING.MD.
This project has adopted the Microsoft Open Source Code of Conduct. For more information, see the Code of Conduct FAQ or contact [email protected] with any additional questions or comments.
For more information, visit the following links:
-
To lean more about the application registration, visit:
-
To learn more about the code, visit Conceptual documentation for MSAL.NET and in particular:
-
Articles about the Azure AD V2 endpoint http://aka.ms/aaddevv2, with a focus on: