AppModelv2-NativeClient-DotNet
AppModelv2-NativeClient-DotNet copied to clipboard
A Windows Desktop (WPF) application calling an ASP.NET Web API protected by the Azure AD v2.0 endpoint
services: active-directory platforms: dotnet author: jmprieur level: 200 client: .NET Desktop (WPF) service: ASP.NET Core Web API endpoint: Microsoft identity platform page_type: sample languages:
- csharp
products: - azure
- azure-active-directory
- dotnet
- office-ms-graph description: "This sample demonstrates a .NET Desktop (WPF) application calling a ASP.NET Web API that is secured using Microsoft identity platform"
Calling an ASP.NET Web API protected by Microsoft identity platform endpoint from an Windows Desktop (WPF) application
About this sample
Scenario
You expose a Web API and you want to protect it so that only authenticated user can access it. This sample shows how to expose a ASP.NET Web API so it can accept tokens issued by personal accounts (including outlook.com, live.com, and others) as well as work and school accounts from any company or organization that has integrated with Azure Active Directory.
The sample also include a Windows Desktop application (WPF) that demonstrate how you can request an access token to access a Web APIs.
How to run this sample
To run this sample, you'll need:
- Visual Studio 2022 or just the .NET Core SDK
- An Azure Active Directory (Azure AD) tenant. For more information on how to get an Azure AD tenant, see How to get an Azure AD tenant
- A user account in your Azure AD tenant. This sample will not work with a Microsoft account (formerly Windows Live account). Therefore, if you signed in to the Azure portal with a Microsoft account and have never created a user account in your directory before, you need to do that now.
Step 1: Clone or download this repository
From your shell or command line:
git clone https://github.com/AzureADQuickStarts/appmodelv2-nativeclient-dotnet.git
or download and extract the repository .zip file.
Given that the name of the sample is quiet long, and so are the names of the referenced NuGet packages, you might want to clone it in a folder close to the root of your hard drive, to avoid file size limitations on Windows.
Step 2: Register the sample application with your Azure Active Directory tenant
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. Note that this works for Visual Studio only.
- modify the Visual Studio projects' configuration files.
Expand this section 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 accordingly.
-
In PowerShell run:
cd .\AppCreationScripts\ .\Configure.ps1Other ways of running the scripts are described in App Creation Scripts The scripts also provide a guide to automated application registration, configuration and removal which can help in your CI/CD scenarios.
-
Open the Visual Studio solution and click start to run the code.
Follow the steps below to manually walk through the steps to register and configure the applications.
Choose the Azure AD tenant where you want to create your applications
As a first step you'll need to:
- Sign in to the Azure portal using either a work or school account or a personal Microsoft account.
- If your account is present in more than one Azure AD tenant, select your profile at the top right corner in the menu on top of the page, and then switch directory. Change your portal session to the desired Azure AD tenant.
Register the service app (TodoListService (appmodelv2-nativeclient-dotnet))
- Navigate to the Microsoft identity platform for developers App registrations page.
- Select New registration.
- In the Register an application page that appears, enter your application's registration information:
- In the Name section, enter a meaningful application name that will be displayed to users of the app, for example
TodoListService (appmodelv2-nativeclient-dotnet). - Under Supported account types, select Accounts in any organizational directory and personal Microsoft accounts (e.g. Skype, Xbox, Outlook.com).
- In the Name section, enter a meaningful application name that will be displayed to users of the app, for example
- Select Register to create the application.
- In the app's registration screen, find and note the Application (client) ID. You use this value in your app's configuration file(s) later in your code.
- Select Save to save your changes.
- In the app's registration screen, select the Expose an API blade to the left to open the page where you can declare the parameters to expose this app as an API for which client applications can obtain access tokens for.
The first thing that we need to do is to declare the unique resource URI that the clients will be using to obtain access tokens for this API. To declare an resource URI, follow the following steps:
- Click
Setnext to the Application ID URI to generate a URI that is unique for this app. - For this sample, accept the proposed Application ID URI (api://{clientId}) by selecting Save.
- Click
- All APIs have to publish a minimum of one scope for the client's to obtain an access token successfully. To publish a scope, follow the following steps:
- Select Add a scope button open the Add a scope screen and Enter the values as indicated below:
- For Scope name, use
access_as_user. - Select Admins and users options for Who can consent?
- For Admin consent display name type
Access TodoListService (appmodelv2-nativeclient-dotnet) - For Admin consent description type
Allows the app to access TodoListService (appmodelv2-nativeclient-dotnet) as the signed-in user. - For User consent display name type
Access TodoListService (appmodelv2-nativeclient-dotnet) - For User consent description type
Allow the application to access TodoListService (appmodelv2-nativeclient-dotnet) on your behalf. - Keep State as Enabled
- Click on the Add scope button on the bottom to save this scope.
- For Scope name, use
- Select Add a scope button open the Add a scope screen and Enter the values as indicated below:
Configure the service app (TodoListService (appmodelv2-nativeclient-dotnet)) to use your app registration
Open the project in your IDE (like Visual Studio) to configure the code.
In the steps below, "ClientID" is the same as "Application ID" or "AppId".
- Open the
TodoListService\appsettings.jsonfile - Find the
ClientIdproperty and replace the existing value with the application ID (clientId) of theTodoListService (appmodelv2-nativeclient-dotnet)application copied from the Azure portal. - Find the
Audienceproperty, and replace the ClientId as well.
Register the client app (TodoListClient (appmodelv2-nativeclient-dotnet))
-
Navigate to the Microsoft identity platform for developers App registrations page.
-
Select New registration.
-
In the Register an application page that appears, enter your application's registration information:
- In the Name section, enter a meaningful application name that will be displayed to users of the app, for example
TodoListClient (appmodelv2-nativeclient-dotnet). - Under Supported account types, select Accounts in any organizational directory and personal Microsoft accounts (e.g. Skype, Xbox, Outlook.com).
- In the Name section, enter a meaningful application name that will be displayed to users of the app, for example
-
Select Register to create the application.
-
In the app's registration screen, find and note the Application (client) ID. You use this value in your app's configuration file(s) later in your code.
-
In the app's registration screen, select Authentication in the menu.
- If you don't have a platform added, select Add a platform and select the Public client (mobile & desktop) option.
- In the Redirect URIs | Suggested Redirect URIs for public clients (mobile, desktop) section, select https://login.microsoftonline.com/common/oauth2/nativeclient
-
Select Save to save your changes.
-
In the app's registration screen, click on the API permissions blade in the left to open the page where we add access to the APIs that your application needs.
- Click the Add a permission button and then,
- Ensure that the My APIs tab is selected.
- In the list of APIs, select the API
TodoListService (appmodelv2-nativeclient-dotnet). - In the Delegated permissions section, select the access_as_user in the list. Use the search box if necessary.
- Click on the Add permissions button at the bottom.
Configure the client app (TodoListClient (appmodelv2-nativeclient-dotnet)) to use your app registration
Open the project in your IDE (like Visual Studio) to configure the code.
In the steps below, "ClientID" is the same as "Application ID" or "AppId".
- Open the
TodoListClient\App.Configfile - Find the app key
ida:Tenantand replace the existing value with your Azure AD tenant name. - Find the app key
ida:ClientIdand replace the existing value with the application ID (clientId) of theTodoListClient (appmodelv2-nativeclient-dotnet)application copied from the Azure portal. - Find the app key
TodoListServiceScopeand replace the existing value with Scope. - Find the app key
TodoListServiceBaseAddressand replace the existing value with the base address of the TodoListService (appmodelv2-nativeclient-dotnet) project (by defaulthttps://localhost:44321/).
Step 4: Pre-authorize your client application
One of the ways to allow users from other directories to access your Web API is by pre-authorizing the client applications to access your Web API by adding the Application Ids from client applications in the list of pre-authorized applications for your Web API. By adding a pre-authorized client, you will not require user to consent to use your Web API. Follow the steps below to pre-authorize your Web Application::
- Go back to the Application registration portal and open the properties of your TodoListService.
- In the Expose an API section, click on Add a client application under the Authorized client applications section.
- In the Client ID field, paste the application ID of the
TodoListClientapplication. - In the Authorized scopes section, select the scope for this Web API
api://<Application ID>/access_as_user. - Press the Add application button at the bottom of the page.
Step 5: Run your project
Clean the solution, rebuild the solution, and run it. You might want to go into the solution properties and set both projects as startup projects, with the service project starting first.
- Press
<F5>to run your project. Your TodoListClient should open. - Select Sign in in the top right (or Clear Cache/Sign-in) and then sign-in either using a personal Microsoft account (live.com or hotmail.com) or work or school account.
Optional: Restrict sign-in access to your application
By default, when you download this code sample and configure the application to use the Microsoft identity platform endpoint following the preceding steps, both personal accounts - like outlook.com, live.com, and others - as well as Work or school accounts from any organizations that are integrated with Azure AD can request tokens and access your Web API.
You can restrict sign-in access for your application to only user accounts that are in a single Azure AD tenant - including guest accounts of that tenant. This scenario is a common for line-of-business applications by changing the TenantId in the appsettings.json