services | platforms | author | level | client | service | endpoint |
---|---|---|---|---|---|---|
active-directory |
dotnet |
jmprieur |
200 |
.NET Desktop (Console) |
ASP.NET Web API |
AAD V1 |
This sample demonstrates a .Net WPF application calling a web API that is secured using Azure AD.
- The .Net TodoListClient WPF application uses the Active Directory Authentication Library (ADAL) to obtain a JWT access token from Azure Active Directory (Azure AD) through the OAuth 2.0 protocol:
- The access token is used as a bearer token to authenticate the user when calling the /todolist endpoint of the TodoListService web API.
Once you've started the TodolistService, you can run the TodoListClient WPF application, click on the sign-in button, and then enter items in a todo list. When you close the application and reopen it, the list is still displayed, as long as the service is running (it maintains the todo list in memory), or until you click on the Clear Cache button. At this point, you'll need to sign in again (and can use another identity). You will manipulate the todo list of the identity you signed-in with.
For more information about how the protocols work in this scenario and other scenarios, see Authentication Scenarios for Azure AD.
Looking for previous versions of this code sample? Check out the tags on the releases GitHub page.
To run this sample, you'll need:
- Visual Studio 2017
- An Internet connection
- 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.
From your shell or command line:
git clone https://github.com/Azure-Samples/active-directory-dotnet-native-client.git
Given that the name of the sample is pretty long, and so are the name of the referenced NuGet pacakges, you might want to clone it in a folder close to the root of your hard drive, to avoid file size limitations on Windows.
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 in the paragraphs below (Step 2 and Step 3)
- or use PowerShell scripts that:
- automatically create for you the Azure AD applications and related objects (passwords, permissions, dependencies)
- modify the Visual Studio projects' configuration files.
If you want to use this automation, read the instructions in App Creation Scripts
As a first step you'll need to:
- Sign in to the Azure portal.
- On the top bar, click on your account, and then on Switch Directory.
- Once the Directory + subscription pane opens, choose the Active Directory tenant where you wish to register your application, from the Favorites or All Directories list.
- Click on All services in the left-hand nav, and choose Azure Active Directory.
In the next steps, you might need the tenant name (or directory name) or the tenant ID (or directory ID). These are presented in the Properties of the Azure Active Directory window respectively as Name and Directory ID
- In the Azure Active Directory pane, click on App registrations and choose New application registration.
- Enter a friendly name for the application, for example 'TodoListService-NativeDotNet' and select 'Web app / API' as the Application Type.
- For the Sign-on URL, enter the base URL for the sample. By default, this sample uses
https://localhost:44321/
. - Click Create to create the application.
- In the succeeding page, Find the Application ID value and record it for later. You'll need it to configure the Visual Studio configuration file for this project.
- Then click on Settings, and choose Properties.
- For the App ID URI, replace the guid in the generated URI 'https://<your_tenant_name>/<guid>', with the name of your service, for example, 'https://<your_tenant_name>/TodoListService-NativeDotNet' (replacing
<your_tenant_name>
with the name of your Azure AD tenant)
- In the Azure Active Directory pane, click on App registrations and choose New application registration.
- Enter a friendly name for the application, for example 'TodoListClient-NativeDotNet' and select 'Native' as the Application Type.
- For the Redirect URI, enter
https://<your_tenant_name>/TodoListClient-NativeDotNet
, replacing<your_tenant_name>
with the name of your Azure AD tenant. - Click Create to create the application.
- In the succeeding page, Find the Application ID value and record it for later. You'll need it to configure the Visual Studio configuration file for this project.
- Then click on Settings, and choose Properties.
- Configure Permissions for your application. To that extent, in the Settings menu, choose the 'Required permissions' section and then,
click on Add, then Select an API, and type
TodoListService-NativeDotNet
in the textbox. Then, click on Select Permissions and select Access 'TodoListService-NativeDotNet'.
In the steps below, "ClientID" is the same as "Application ID" or "AppId".
Open the solution in Visual Studio to configure the projects
- Open the
TodoListService\Web.Config
file - Find the app key
ida:Tenant
and replace the existing value with your Azure AD tenant name. - Find the app key
ida:Audience
and replace the existing value with the App ID URI you registered earlier for the TodoListService-NativeDotNet app. For instance usehttps://<your_tenant_name>/TodoListService-NativeDotNet
, where<your_tenant_name>
is the name of your Azure AD tenant.
- Open the
TodoListClient\App.Config
file - Find the app key
ida:Tenant
and replace the existing value with your Azure AD tenant name. - Find the app key
ida:ClientId
and replace the existing value with the application ID (clientId) of theTodoListClient-NativeDotNet
application copied from the Azure portal. - Find the app key
ida:RedirectUri
and replace the existing value with the Redirect URI for TodoListClient-NativeDotNet app. For instance usehttps://<your_tenant_name>/TodoListClient-NativeDotNet
, where<your_tenant_name>
is the name of your Azure AD tenant. - Find the app key
todo:TodoListResourceId
and replace the existing value with the App ID URI you registered earlier for the TodoListService-NativeDotNet app. For instance usehttps://<your_tenant_name>/TodoListService-NativeDotNet
, where<your_tenant_name>
is the name of your Azure AD tenant. - Find the app key
todo:TodoListBaseAddress
and replace the existing value with the base address of the TodoListService-NativeDotNet project (by defaulthttps://localhost:44321/
).
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.
Explore the sample by signing in, adding items to the To Do list, removing the user account (by clicking on "Clear Cache"), and starting again. Notice that if you stop the application without removing the user account, the next time you run the application you won't be prompted to sign in again. The sample implements a persistent cache for ADAL, and remembers the tokens from the previous run.
This project has one WebApp / Web API projects. To deploy them to Azure Web Sites, you'll need, for each one, to:
- create an Azure Web Site
- publish the Web App / Web APIs to the web site, and
- update its client(s) to call the web site instead of IIS Express.
- Sign in to the Azure portal.
- Click Create a resource in the top left-hand corner, select Web + Mobile --> Web App, select the hosting plan and region, and give your web site a name, for example,
TodoListService-NativeDotNet-contoso.azurewebsites.net
. Click Create Web Site. - Once the web site is created, click on it to manage it. For this set of steps, download the publish profile by clicking Get publish profile and save it. Other deployment mechanisms, such as from source control, can also be used.
- Switch to Visual Studio and go to the TodoListService-NativeDotNet project. Right click on the project in the Solution Explorer and select Publish. Click Import Profile on the bottom bar, and import the publish profile that you downloaded earlier.
- Click on Settings and in the
Connection tab
, update the Destination URL so that it is https, for example https://TodoListService-NativeDotNet-contoso.azurewebsites.net. Click Next. - On the Settings tab, make sure
Enable Organizational Authentication
is NOT selected. Click Save. Click on Publish on the main screen. - Visual Studio will publish the project and automatically open a browser to the URL of the project. If you see the default web page of the project, the publication was successful.
- Navigate to the Azure portal.
- On the top bar, click on your account and under the Directory list, choose the Active Directory tenant containing the
TodoListService-NativeDotNet
application. - On the applications tab, select the
TodoListService-NativeDotNet
application. - From the Settings -> Reply URLs menu, update the Sign-On URL, and Reply URL fields to the address of your service, for example https://TodoListService-NativeDotNet-contoso.azurewebsites.net. Save the configuration.
Update the TodoListClient-NativeDotNet
to call the TodoListService-NativeDotNet
Running in Azure Web Sites
- In Visual Studio, go to the
TodoListClient-NativeDotNet
project. - Open
TodoListClient\App.Config
. Only one change is needed - update thetodo:TodoListBaseAddress
key value to be the address of the website you published, for example, https://TodoListService-NativeDotNet-contoso.azurewebsites.net. - Run the client! If you are trying multiple different client types (for example, .Net, Windows Store, Android, iOS) you can have them all call this one published web API.
NOTE: Remember, the To Do list is stored in memory in this TodoListService sample. Azure Web Sites will spin down your web site if it is inactive, and your To Do list will get emptied. Also, if you increase the instance count of the web site, requests will be distributed among the instances. To Do will, therefore, not be the same on each instance.
The code using ADAL.NET is in the TodoListClient/MainWindow.xaml.cs file in the SignIn
method. See More information below
for details on how this work.
Also, to avoid you re sign in each time your run the client application, the code provides a Token cache serialization located in TodoListClient/FileCache.cs
The Startup.cs
file contains a partial implementation of the Startup
class, which Configration() method calls ConfigureAuth()
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
ConfigureAuth(app);
}
}
In the file, the ConfigureAuth(…)
method declare the uses of Windows Azure Active Directory bearer authentication and sets the application coordinates to communicate with Azure AD as WindowsAzureActiveDirectoryBearerAuthenticationOptions
.
public void ConfigureAuth(IAppBuilder app)
{
app.UseWindowsAzureActiveDirectoryBearerAuthentication(
new WindowsAzureActiveDirectoryBearerAuthenticationOptions
{
Tenant = ConfigurationManager.AppSettings["ida:Tenant"],
TokenValidationParameters = new TokenValidationParameters
{
ValidAudience = ConfigurationManager.AppSettings["ida:Audience"]
}
});
}
- The
[Authorize]
attributes on the controller protect the controllers and actions with JWT bearer authentication. See for instance theControllers\TodoListController.cs
class with an authorize attribute. This attribute will force the user to sign in before accessing that page.
[Authorize]
public class TodoListController : ApiController
{
- When an authorized caller successfully invokes one of the
TodoListController
APIs, the action might need access to information about the caller. OWIN provides access to the claims inside the bearer token via theClaimsPrincpal
object. - A common requirement for web APIs is to validate the "scopes" present in the token - this ensures that the end user has consented to the permissions required to access the Todo List Service:
public IEnumerable<TodoItem> Get()
{
// user_impersonation is the default permission exposed by applications in AAD
if (ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/scope").Value != "user_impersonation")
{
throw new HttpResponseException(new HttpResponseMessage {
StatusCode = HttpStatusCode.Unauthorized,
ReasonPhrase = "The Scope claim does not contain 'user_impersonation' or scope claim not found"
});
}
...
}
First, in Visual Studio 2017 create an empty solution to host the projects. Then, follow these steps to create each project.
- In the solution, create a new ASP.Net MVC web API project called
TodoListService
and while creating the project, click the Change Authentication button, select Organizational Accounts, Cloud - Single Organization, enter the name of your Azure AD tenant, and set the Access Level to Single Sign On. You will be prompted to sign in to your Azure AD tenant. NOTE: You must sign in with a user that is in the tenant; you cannot, during this step, sign in with a Microsoft account. - In the folder, add a new class called
TodoItem.cs
. Copy the implementation of TodoItem from this sample into the class. - Add a new, empty, Web API 2 controller called
TodoListController
. - Copy the implementation of the TodoListController from this sample into the controller. Don't forget to add the
[Authorize]
attribute to the class. - In
TodoListController
resolving missing references by addingusing
statements forSystem.Collections.Concurrent
,TodoListService.Models
,System.Security.Claims
.
- In the solution, create a new Windows --> WPF Application called TodoListClient.
- Add the (stable release) Active Directory Authentication Library (ADAL) NuGet,
Microsoft.IdentityModel.Clients.ActiveDirectory
to the project. - Add assembly references to
System.Net.Http
,System.Web.Extensions
,System.Security
, andSystem.Configuration
. - Add a new class to the project called
TodoItem.cs
. Copy the code from the sample project file of the same name into this class, completely replacing the code in the file in the new project. - Add a new class to the project called
FileCache.cs
. Copy the code from the sample project file of the same name into this class, completely replacing the code in the file in the new project. - Copy the markup from `MainWindow.xaml' in the sample project into the file of the same name in the new project, completely replacing the markup in the file in the new project.
- Copy the code from
MainWindow.xaml.cs
in the sample project into the file of the same name in the new project, completely replacing the code in the file in the new project. - In
app.config
create keys forida:AADInstance
,ida:Tenant
,ida:ClientId
,ida:RedirectUri
,todo:TodoListResourceId
, andtodo:TodoListBaseAddress
and set them accordingly. For the global Azure cloud, the value ofida:AADInstance
ishttps://login.microsoftonline.com/{0}
.
Finally, in the properties of the solution itself, set both projects as startup projects.
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 [adal
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, see ADAL.NET's conceptual documentation:
- Recommended pattern to acquire a token
- Acquiring tokens interactively in public client applications
- Customizing Token cache serialization
For more information about how OAuth 2.0 protocols work in this scenario and other scenarios, see Authentication Scenarios for Azure AD.