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.
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
- An Azure subscription (a free trial is sufficient)
- A Microsoft account
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 http://wwww.windowsazure.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/AzureADSamples/NativeClient-WindowsPhone8.1.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.
- Sign in to the Azure management portal.
- Click on Active Directory in the left hand nav.
- Click the directory tenant where you wish to register the sample application.
- Click the Applications tab.
- In the drawer, click Add.
- Click "Add an application my organization is developing".
- Enter a friendly name for the application, for example "TodoListService", select "Web Application and/or Web API", and click next.
- For the sign-on URL, enter the base URL for the sample, which is by default
https://localhost:44321
. - For the App ID URI, enter
https://<your_tenant_name>/TodoListService
, replacing<your_tenant_name>
with the name of your Azure AD tenant. Click OK to complete the registration. - While still in the Azure portal, click the Configure tab of your application.
- Find the Client ID value and copy it aside, you will need this later when configuring your 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:
- Open the solution in Visual Studio 2013.
- In the TodoListClient project, open the
MainPage.xaml.cs
file. - Find this line of code and set a breakpoint on it.
Uri redirectURI = Windows.Security.Authentication.Web.WebAuthenticationBroker.GetCurrentApplicationCallbackUri();
- Right-click on the TodoListClient project and Debug --> Start New Instance.
- When the breakpoint is hit, use the debugger to determine the value of redirectURI, and copy it aside for the next step.
- Stop debugging, and clear the breakpoint.
The redirectURI value will look something like this:
ms-app://s-1-15-2-2123189467-1366327299-2057240504-936110431-2588729968-1454536261-950042884/
- Sign in to the Azure management portal.
- Click on Active Directory in the left hand nav.
- Click the directory tenant where you wish to register the sample application.
- Click the Applications tab.
- In the drawer, click Add.
- Click "Add an application my organization is developing".
- Enter a friendly name for the application, for example "TodoListClient-WindowsPhone", select "Native Client Application", and click next.
- Enter the Redirect URI value that you obtained during the previous step. Click finish.
- Click the Configure tab of the application.
- Find the Client ID value and copy it aside, you will need this later when configuring your application.
- In "Permissions to Other Applications", click "Add Application." Select "Other" in the "Show" dropdown, and click the upper check mark. Locate & click on the TodoListService, and click the bottom check mark to add the application. Select "Access TodoListService" from the "Delegated Permissions" dropdown, and save the configuration.
- Open the solution in Visual Studio 2013.
- Open the
web.config
file. - Find the app key
ida:Tenant
and replace the value with your AAD tenant name. - Find the app key
ida:Audience
and replace the value with the App ID URI you registered earlier, for examplehttps://<your_tenant_name>/TodoListService
. - Find the app key
ida:ClientId
and replace the value with the Client ID for the TodoListService from the Azure portal.
- Open `MainPage.xaml.cs'.
- Find the declaration of
tenant
and replace the value with the name of your Azure AD tenant. - Find the declaration of
clientId
and replace the value with the Client ID from the Azure portal. - Find the declaration of
todoListResourceId
andtodoListBaseAddress
and 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.
- While on the Start screen, type "certificates". Choose "Manage Computer Certificates" from the suggested results list.
- On the left side tree, open Personal/Certificates. On the right pane you will see a list of certificates, including one entry named "localhost".
- Right click on "localhost", choose "All Tasks->Export...".
- Click "next" without changing the defaults until you reach the "Export file format" screen. Choose the 3rd option (.P7B) and click "next"
- Save the certificate as "localhost.p7b" in \NativeClient-WindowsPhone8.1\TodoListService\Content.
- Start the TodoListService project in the debugger (right-click on the project in Solution Explorer, select "Debug->Start New Instance"). Ensure that the browser appears and is correctly populated with the default ASP.NET UI
- Start the emulator by starting the client project (right-click on the project in Solution Explorer, select "Debug->Start New Instance").
- As soon as the emulator boots, hit the Windows button to get to the tiles list. Launch Mobile Internet Explorer.
- Navigate to "https://localhost:44321/Content/localhost.p7b". Choose "continue to web site".
- When prompted, choose to open the file. You will be asked if you want to install the certificate. Choose "Install". If the operation succeeds, you will see a confirmation dialog.
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.
Coming soon.
Coming soon.
Coming soon.