This sample demonstrates a Windows Store application for Windows Phone 8.1 calling a web API that is secured using Azure AD. The 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.
NOTE: This sample uses version 2 of the ADAL library. Version 3 of ADAL is available, but does not support Windows Phone 8.1. For an enquivalent Windows Universal sample, go here.
For more information about how the protocols work in this scenario and other scenarios, see Authentication Scenarios for Azure AD.
To run this sample you will need: - Visual Studio 2013 Update 2 or later - Windows 8.1 or higher - A machine supporting Client Hyper-V and Second Level Address Translation (SLAT) - An Internet connection - A Microsoft account - An Azure Active Directory (Azure AD) tenant. For more information on how to get an Azure AD tenant, please 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, 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.
From your shell or command line:
git clone https://github.com/Azure-Samples/active-directory-dotnet-windowsphone-8.1.git
There are two projects in this sample. Each needs to be separately registered in your Azure AD tenant.
https://localhost:44321. Click on Create to create the application.
Before you can register the TodoListClient application in the Azure portal, you need to find out the application's redirect URI. Windows Phone 8.1 provides each application with a unique URI and ensures that messages sent to that URI are only sent to that application. To determine the redirect URI for your project:
Uri redirectURI = Windows.Security.Authentication.Web.WebAuthenticationBroker.GetCurrentApplicationCallbackUri();
The redirectURI value will look something like this:
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:ClientIdand replace the value with the Application ID for the TodoListService from the Azure portal.
tenantand replace the value with the name of your Azure AD tenant.
clientIdand replace the value with the Application ID from the Azure portal.
todoListBaseAddressand ensure their values are set properly for your TodoListService project.
Since the web API is SSL protected, the client of the API will refuse the SSL connection to the web API unless it trusts the API's SSL certificate. Use the following steps to add the IIS Express development certificate in the Windows Phone emulator. You will need to repeat the import operation every time you restart the emulator. If you fail to do this step, calls to the TodoListService will always fail with a 404 code. > Note: if you want to avoid having to repeat this task every time, you can deploy the web API to a host which uses an SSL certificate that the emulator already trusts. An Azure Web Site is a good example.
This task includes two operations: exporting the certificate from your development machine and importing it in the emulator. The first operation needs to be performed only once; the second operation will have to be repeated every time the emulator restarts.
From now on, your emulator instance will be able to perform web API calls against projects running on your local IIS Express. Please note that as soon as you will close the emulator, the settings will be lost and you'll need to repeat the import operation.
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.
On the main screen, hit the refresh button. You will be prompted to sign in. Once done so explore the sample by 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 because ADAL has a persistent cache, and remembers the tokens from the previous run.