Skip to content

A Windows Desktop (WPF) application calling an ASP.NET Web API protected by the Azure AD v2.0 endpoint

License

Notifications You must be signed in to change notification settings

Shama-K/AppModelv2-NativeClient-DotNet

 
 

Repository files navigation

services platforms author level client service endpoint page_type languages products description
active-directory
dotnet
jmprieur
200
.NET Desktop (WPF)
ASP.NET Core Web API
Microsoft identity platform
sample
csharp
azure
azure-active-directory
dotnet
office-ms-graph
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 2019 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:

Expand this section if you want to use this automation:
  1. On Windows, run PowerShell and navigate to the root of the cloned directory

  2. In PowerShell run:

    Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process -Force
  3. Run the script to create your Azure AD application and configure the code of the sample application accordingly.

  4. In PowerShell run:

    cd .\AppCreationScripts\
    .\Configure.ps1

    Other 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.

  5. 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:

  1. Sign in to the Azure portal using either a work or school account or a personal Microsoft account.
  2. 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))

  1. Navigate to the Microsoft identity platform for developers App registrations page.
  2. Select New registration.
  3. 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).
  4. Select Register to create the application.
  5. 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.
  6. Select Save to save your changes.
  7. 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 Set next 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.
  8. 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.
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".

  1. Open the TodoListService\Web.config file
  2. Find the app key ida:ClientId and replace the existing value with the application ID (clientId) of the TodoListService (appmodelv2-nativeclient-dotnet) application copied from the Azure portal.

Register the client app (TodoListClient (appmodelv2-nativeclient-dotnet))

  1. Navigate to the Microsoft identity platform for developers App registrations page.

  2. Select New registration.

  3. 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).
  4. Select Register to create the application.

  5. 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.

  6. In the app's registration screen, select Authentication in the menu.

  7. Select Save to save your changes.

  8. 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".

  1. Open the TodoListClient\App.Config file
  2. Find the app key ida:Tenant and replace the existing value with your Azure AD tenant name.
  3. Find the app key ida:ClientId and replace the existing value with the application ID (clientId) of the TodoListClient (appmodelv2-nativeclient-dotnet) application copied from the Azure portal.
  4. Find the app key TodoListServiceScope and replace the existing value with Scope.
  5. Find the app key TodoListServiceBaseAddress and replace the existing value with the base address of the TodoListService (appmodelv2-nativeclient-dotnet) project (by default https://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::

  1. Go back to the Application registration portal and open the properties of your TodoListService.
  2. In the Expose an API section, click on Add a client application under the Authorized client applications section.
  3. In the Client ID field, paste the application ID of the TodoListClient application.
  4. In the Authorized scopes section, select the scope for this Web API api://<Application ID>/access_as_user.
  5. 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.

  1. Press <F5> to run your project. Your TodoListClient should open.
  2. 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.

To restrict who can sign in to your application, use one of the options:

Option 1: Restrict access to a single organization (single-tenant)

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:

  1. Open the App_Start\Startup.Auth file, and change the value of the metadata endpoint that's passed into the OpenIdConnectSecurityTokenProvider to "https://login.microsoftonline.com/{Tenant ID}/v2.0/.well-known/openid-configuration" (you can also use the Tenant Name, such as contoso.onmicrosoft.com).
  2. In the same file, set the ValidIssuer property on the TokenValidationParameters to "https://sts.windows.net/{Tenant Id}/" and the ValidateIssuer argument to true.

Option 2: Use a custom method to validate issuers

You can implement a custom method to validate issuers by using the IssuerValidator parameter. For more information about how to use this parameter, read about the TokenValidationParameters class on MSDN.

About

A Windows Desktop (WPF) application calling an ASP.NET Web API protected by the Azure AD v2.0 endpoint

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 71.3%
  • PowerShell 9.2%
  • HTML 9.2%
  • JavaScript 8.8%
  • CSS 1.4%
  • ASP.NET 0.1%