This sample demonstrates a .Net WPF application calling a web API that is secured using Azure AD. The .Net application uses the Active Directory Authentication Library (ADAL) to obtain a JWT access token through the OAuth 2.0 protocol. The access token is sent to the web API to authenticate the user.
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 will need: - Visual Studio 2013 - An Internet connection - An Azure subscription (a free trial is sufficient)
Every Azure subscription has an associated Azure Active Directory tenant. If you don't already have an Azure subscription, you can get a free subscription by signing up at https://azure.microsoft.com. All of the Azure AD features used by this sample are available free of charge.
From your shell or command line:
git clone https://github.com/Azure-Samples/active-directory-dotnet-native-client.git
If you already have a user account in your Azure Active Directory tenant, you can skip to the next step. This sample will not work with a Microsoft account, so 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. If you create an account and want to use it to sign-in to the Azure portal, don't forget to add the user account as a co-administrator of your Azure subscription.
There are two projects in this sample. Each needs to be separately registered in your Azure AD tenant.
<your_tenant_name>with the name of your Azure AD tenant. Click OK to complete the registration.
http://TodoListClient. Click finish.
ida:Tenantand replace the value with your AAD tenant name.
ida:Audienceand replace the value with the App ID URI you registered earlier, for example
ida:Tenantand replace the value with your AAD tenant name.
ida:ClientIdand replace the value with the Client ID for the TodoListClient from the Azure portal.
ida:RedirectUriand replace the value with the Redirect URI for the TodoListClient from the Azure portal, for example
todo:TodoListResourceIdand replace the value with the App ID URI of the TodoListService, for example
todo:TodoListBaseAddressand replace the value with the base address of the TodoListService project.
Since the web API is SSL protected, the client of the API (the web app) will refuse the SSL connection to the web API unless it trusts the API's SSL certificate. Use the following steps in Windows Powershell to trust the IIS Express SSL certificate. You only need to do this once. If you fail to do this step, calls to the TodoListService will always throw an unhandled exception where the inner exception message is:
"The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel."
To configure your computer to trust the IIS Express SSL certificate, begin by opening a Windows Powershell command window as Administrator.
Query your personal certificate store to find the thumbprint of the certificate for
PS C:\windows\system32> dir Cert:\LocalMachine\My Directory: Microsoft.PowerShell.Security\Certificate::LocalMachine\My Thumbprint Subject ---------- ------- C24798908DA71693C1053F42A462327543B38042 CN=localhost
Next, add the certificate to the Trusted Root store:
PS C:\windows\system32> $cert = (get-item cert:\LocalMachine\My\C24798908DA71693C1053F42A462327543B38042) PS C:\windows\system32> $store = (get-item cert:\Localmachine\Root) PS C:\windows\system32> $store.Open("ReadWrite") PS C:\windows\system32> $store.Add($cert) PS C:\windows\system32> $store.Close()
You can verify the certificate is in the Trusted Root store by running this command:
PS C:\windows\system32> dir Cert:\LocalMachine\Root
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, 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 - that is the sample implements a persistent cache for ADAL, and remembers the tokens from the previous run.
To deploy the TodoListService to Azure Web Sites, you will create a web site, publish the TodoListService to the web site, and update the TodoListClient to call the web site instead of IIS Express.
app.config. Only one change is needed - update the
todo:TodoListBaseAddresskey value to be the address of the website you published, e.g. https://todolistservice-skwantoso.azurewebsites.net.
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 and the To Do will not be the same on each instance.
First, in Visual Studio 2013 create an empty solution to host the projects. Then, follow these steps to create each project.
Modelsfolder add a new class called
TodoItem.cs. Copy the implementation of TodoItem from this sample into the class.
[Authorize]attribute to the class.
TodoListControllerresolving missing references by adding
TodoItem.cs. Copy the code from the sample project file of same name into this class, completely replacing the code in the file in the new project.
FileCache.cs. Copy the code from the sample project file of same name into this class, completely replacing the code in the file in the new project.
MainWindow.xaml.csin the sample project into the file of same name in the new project, completely replacing the code in the file in the new project.
app.configcreate keys for
todo:TodoListBaseAddressand set them accordingly. For the public Azure cloud, the value of
Finally, in the properties of the solution itself, set both projects as startup projects.