{"id":19455,"date":"2018-03-14T21:35:00","date_gmt":"2018-03-14T21:35:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/premier_developer\/?p=19455"},"modified":"2019-02-21T12:53:09","modified_gmt":"2019-02-21T19:53:09","slug":"client-directed-authentication-with-azure-mobile-apps","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/premier-developer\/client-directed-authentication-with-azure-mobile-apps\/","title":{"rendered":"Client Directed Authentication with Azure Mobile Apps"},"content":{"rendered":"<p>App Dev Manager Nicholas McCollum walks through creating an Azure Mobile App that uses client directed authentication via Azure AD.<\/p>\n<hr \/>\n<p>Azure Mobile Apps are built on Azure App Services. Through the Azure portal you can configure your Azure Mobile App to provide sign in, push notifications, and data synchronization. When you configure sign in through the Azure portal for your Azure Mobile App by default you are leveraging the \u201cserver directed\u201d or \u201cserver managed\u201d authentication flow. While this approach will suffice for simple application scenarios \u201cclient directed\u201d or \u201cclient managed\u201d authentication is the preferred method for authenticating with an Azure Mobile App.<\/p>\n<p>Using client directed authentication your mobile application independently contacts the identity provider and then includes the returned access token during login with your Azure Mobile App rather than relying on the Azure Mobile App service to handle the exchange with the identity provider. Client directed authentication is preferred to server directed authentication as the identity provider SDK provides a more native user experience, supports single sign-on, and refresh support.<\/p>\n<p>In this article we will walk through using client directed authentication with Azure Active Directory to authenticate to an Azure Mobile App from a Xamarin.Forms mobile application.<\/p>\n<h2>Setting Up the Environment<\/h2>\n<p>Prior to writing our code we need to perform two actions in the Azure portal. First, we must create an Azure Mobile App and register that web application with Azure Active Directory. Second, we must register a native client application with Azure Active Directory and grant it access to call the Azure Mobile App. An example of each Azure Active Directory application registration is shown below.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/31\/2019\/04\/clip_image0026.jpg\"><img decoding=\"async\" title=\"clip_image002\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/31\/2019\/04\/clip_image002_thumb5.jpg\" alt=\"clip_image002\" width=\"804\" height=\"1128\" border=\"0\" \/><\/a><\/p>\n<p>While registering your applications also make sure to note the directory ID on the Properties blade of your Azure Active Directory. You will need this later.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/31\/2019\/04\/clip_image0044.jpg\" name=\"_Hlk507011180\"><img decoding=\"async\" title=\"clip_image004\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/31\/2019\/04\/clip_image004_thumb3.jpg\" alt=\"clip_image004\" width=\"1028\" height=\"491\" border=\"0\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>With our mobile app and native client registered in Azure Active Directory we need to enable authentication on our mobile app in the Azure portal. When configuring our Azure Mobile App we will enable App Service Authentication and then configure Azure Active Directory as an Authentication Provider. On the Active Directory Authentication blade we will select the Advanced option and enter the client ID of the web application registered earlier and the issuer URL. The issuer URL will be <a href=\"https:\/\/sts.windows.net\/\">https:\/\/sts.windows.net\/<\/a> followed by your Azure Active Directory ID. In the Allowed Token Audiences field we will enter the App ID URI of the web application registered earlier.<\/p>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-35363\" src=\"http:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2018\/03\/mobile1.png\" alt=\"\" width=\"1318\" height=\"515\" srcset=\"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2018\/03\/mobile1.png 1318w, https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2018\/03\/mobile1-300x117.png 300w, https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2018\/03\/mobile1-768x300.png 768w, https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2018\/03\/mobile1-1024x400.png 1024w\" sizes=\"(max-width: 1318px) 100vw, 1318px\" \/><\/p>\n<p><i><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/31\/2019\/04\/clip_image0082.jpg\"><img decoding=\"async\" title=\"clip_image008\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/31\/2019\/04\/clip_image008_thumb2.jpg\" alt=\"clip_image008\" width=\"1028\" height=\"728\" border=\"0\" \/><\/a><\/i><i><\/i><\/p>\n<p>With our mobile app created, registered, and configured we are ready to begin work on our code.<\/p>\n<h2><b>The Visual Studio Solution<\/b><\/h2>\n<p>Our Visual Studio solution is made up of two projects. The first is an ASP.Net web application that leverages the Azure Mobile App project template. The second project is a Xamarin.Forms application that uses the Blank App project template with Shared Project as the Code Sharing Strategy.<\/p>\n<p>Since the goal of this exercise is to authenticate with the Azure Mobile App using client-directed authentication we will only make a minor change to the ASP.Net application. Open the ValuesController.cs file in the Controllers folder and add an [Authorize] attribute to the class definition. This will ensure that only authenticated users can call the methods of the ValuesController class. Publish your application to the Azure Mobile App Service you created in your Azure subscription.<\/p>\n<p>With work on the Azure Mobile App complete we can turn our attention to the Xamarin.Forms project. Prior to doing anything else open the NuGet package manager and update all of the packages used by the Xamarin.Forms project to their latest version. Once that is complete rebuild the solution and verify that it builds without errors. Open the NuGet package manager again and add a reference to Microsoft.Azure.Mobile.Client and Microsoft.IdentityModel.Clients.ActiveDirectory. Again rebuild the solution and verify that it builds without errors.<\/p>\n<p>Open the MainPage.xaml file in the shared project and modify its contents as follows.<\/p>\n<p><code>&lt;?xml version=\"1.0\" encoding=\"utf-8\" ?&gt;<\/code><\/p>\n<p><code>\u00a0\u00a0 &lt;ContentPage xmlns=\"http:\/\/xamarin.com\/schemas\/2014\/forms\"<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 xmlns:x=\"http:\/\/schemas.microsoft.com\/winfx\/2009\/xaml\"<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 xmlns:local=\"clr-namespace:MobileAppClient\"<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 x:Class=\"MobileAppClient.MainPage\"&gt;<\/code><\/p>\n<p>&nbsp;<\/p>\n<p><code>\u00a0\u00a0 &lt;StackLayout Orientation=\"Vertical\"&gt;<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &lt;Button Text=\"Login\" Clicked=\"OnLoginClicked\"&gt;&lt;\/Button&gt;<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &lt;Label x:Name=\"lblHello\"&gt;&lt;\/Label&gt;<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &lt;Label x:Name=\"lblMobileServiceStatus\"&gt;&lt;\/Label&gt;<\/code><\/p>\n<p><code>\u00a0\u00a0 &lt;\/StackLayout&gt;<\/code><\/p>\n<p>&nbsp;<\/p>\n<p><code>&lt;\/ContentPage&gt;<\/code><\/p>\n<p>The Login button will be used to trigger our authentication with Azure Active Directory. The lblHello will display the user\u2019s displayable ID when they authenticate successfully. The lblMobileServiceStatus will display the results of our calls to the Azure Mobile App.<\/p>\n<p>Next we will create a simple class named ServiceManager in the shared project that provides a singleton instance of the Microsoft.WindowsAzure.MobileServices.MobileServiceClient class. The implementation of the ServiceManager class is shown below. Make sure to update the value of the _serviceUrl variable to contain the URL of your Azure Mobile App Service.<\/p>\n<p><code>using Microsoft.WindowsAzure.MobileServices;<\/code><\/p>\n<p>&nbsp;<\/p>\n<p><code>namespace MobileAppClient<\/code><\/p>\n<p><code>{<\/code><\/p>\n<p><code>\u00a0\u00a0 public class ServiceManager<\/code><\/p>\n<p><code>\u00a0\u00a0 {<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 private readonly string _serviceUrl = @\"https:\/\/your-mobile-app.azurewebsites.net\";<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 private static ServiceManager _defaultInstance = new ServiceManager();<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 private MobileServiceClient _client;<\/code><\/p>\n<p>&nbsp;<\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 private ServiceManager()<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 _client = new MobileServiceClient(_serviceUrl);<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<\/code><\/p>\n<p>&nbsp;<\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 public static ServiceManager DefaultManager<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 get { return _defaultInstance; }<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 private set { _defaultInstance = value; }<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<\/code><\/p>\n<p>&nbsp;<\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 public MobileServiceClient CurrentClient<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 get { return _client; }<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<\/code><\/p>\n<p><code>\u00a0\u00a0 }<\/code><\/p>\n<p><code>}<\/code><\/p>\n<p>With our ServiceManager created we can add implementation details to the MainPage.xaml.cs file. Open the MainPage.xaml.cs file in the shared project and make the following modifications.<\/p>\n<p>1. Add private variables to store our Azure Active Directory settings and the default instance of our ServiceManager class.<\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 private readonly string _authority = \"https:\/\/login.microsoftonline.com\";<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 private readonly string _tenant = \"your-aad-tenant.onmicrosoft.com\";<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 private readonly string _resourceID = \"https:\/\/your-aad-tenant.onmicrosoft.com\/mobiletest1\";<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 private readonly string _clientID = \"e1ab7561-e676-4ee0-bc30-5d3bc87c2fd9\";<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 private readonly string _redirectUrl = \"http:\/\/localhost\";<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 private ServiceManager _manager = ServiceManager.DefaultManager;<\/code><\/p>\n<p>2. Create a public property named PlatformParameters of type IPlatformParameters.\n<code>\u00a0 \u00a0 \u00a0\u00a0 public IPlatformParameters PlatformParameters { get; set; }<\/code><\/p>\n<p>3. Add code to the OnLoginClicked event handler to trigger authentication with Azure Active Directory.<\/p>\n<p><code>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 JObject payload = null;<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 try<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ Authenticate with Azure Active Directory and obtain an access token for the mobile app service<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 AuthenticationContext context = new AuthenticationContext(string.Format(\"{0}\/{1}\", _authority, _tenant));<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 AuthenticationResult result = await context.AcquireTokenAsync(_resourceID, _clientID, new Uri(_redirectUrl), PlatformParameters);<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 payload = new JObject();<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 payload[\"access_token\"] = result.AccessToken;<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 lblHello.Text = string.Format(\"Hello {0}!\", result.UserInfo.DisplayableId);<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 lblHello.TextColor = Color.Green;<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 catch (Exception)<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 lblHello.Text = \"Login failed!\";<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 lblHello.TextColor = Color.Red;<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<\/code><\/p>\n<p>4. If authentication is successful, login to the Azure Mobile App using the access token returned by Azure Active Directory and call the default Get method on the ValuesController of the Azure Mobile App.<\/p>\n<p><code>\u00a0 \u00a0 \u00a0\u00a0 if (payload != null)<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 try<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ Login to the Azure Mobile App Service<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 MobileServiceUser user = await _manager.CurrentClient.LoginAsync(MobileServiceAuthenticationProvider.WindowsAzureActiveDirectory, payload);<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 lblMobileServiceStatus.Text = \"Connected to Azure Mobile App Service\";<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 lblMobileServiceStatus.TextColor = Color.Green;<\/code><\/p>\n<p>&nbsp;<\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 try<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ Call the ValuesController<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 IEnumerable&lt;string&gt; values = await _manager.CurrentClient.InvokeApiAsync&lt;IEnumerable&lt;string&gt;&gt;(\"values\");<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 lblMobileServiceStatus.Text = string.Join(\",\", values);<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 lblMobileServiceStatus.TextColor = Color.Green;<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 catch (MobileServiceInvalidOperationException ex)<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 lblMobileServiceStatus.Text = string.Format(\"Call to Azure Mobile App Service ValuesController failed with the following information {0} {1}\", ex.Response.StatusCode, ex.Response.ReasonPhrase);<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 lblMobileServiceStatus.TextColor = Color.Red;<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<\/code><\/p>\n<p>&nbsp;<\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 catch (MobileServiceInvalidOperationException ex)<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 lblMobileServiceStatus.Text = string.Format(\"Login to Azure Mobile App Service failed with the following information {0} {1}\", ex.Response.StatusCode, ex.Response.ReasonPhrase);<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 lblMobileServiceStatus.TextColor = Color.Red;<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<\/code><\/p>\n<p>That completes the changes to the MainPage.xaml.cs file. Now we need to make some minor changes to each platform specific project to support authentication to Azure Active Directory.<\/p>\n<p>Add a new class named MainPageRenderer to each platform specific project (MobileAppClient.Android, MobileAppClient.iOS, and MobileAppClient.UWP). That class should inherit from PageRenderer. Add the following assembly attribute above the namespace declaration in each class\n<code>[assembly: ExportRenderer(typeof(MainPage), typeof(MainPageRenderer))]<\/code>\nThe first parameter references the Xamarin.Forms page in the shared project and the second parameter identifies the page renderer that will be used to render the page on the platform. We use this approach because the implementation of the IPlatformParameters interface is slightly different on each platform.<\/p>\n<p>Make the following changes to the MainPageRenderer.cs file in the Android project.\n<code>private MainPage _page;<\/code>\n<code>protected override void OnElementChanged(ElementChangedEventArgs&lt;Page&gt; e)<\/code><\/p>\n<p><code>{<\/code><\/p>\n<p><code>base.OnElementChanged(e);<\/code><\/p>\n<p><code>_page = e.NewElement as MainPage;<\/code><\/p>\n<p><code>Activity activity = this.Context as Activity;<\/code><\/p>\n<p><code>_page.PlatformParameters = new PlatformParameters(activity);<\/code><\/p>\n<p><code>}<\/code><\/p>\n<p>Make the following changes to the MainActivity.cs file in the Android project.\n<code>protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)<\/code>\n<code>{<\/code><\/p>\n<p><code>base.OnActivityResult(requestCode, resultCode, data);<\/code><\/p>\n<p><code>AuthenticationAgentContinuationHelper.SetAuthenticationAgentContinuationEventArgs(requestCode, resultCode, data);<\/code><\/p>\n<p><code>}<\/code><\/p>\n<p>Make the following changes to the MainPageRenderer.cs file in iOS project.\nMainPage _page;\n<code>protected override void OnElementChanged(VisualElementChangedEventArgs e)<\/code><\/p>\n<p><code>{<\/code><\/p>\n<p><code>base.OnElementChanged(e);<\/code><\/p>\n<p><code>_page = e.NewElement as MainPage;<\/code><\/p>\n<p><code>}<\/code><\/p>\n<p><code>public override void ViewDidLoad()<\/code><\/p>\n<p><code>{<\/code><\/p>\n<p><code>base.ViewDidLoad();<\/code><\/p>\n<p><code>_page.PlatformParameters = new PlatformParameters(this);<\/code><\/p>\n<p><code>_page.DeviceRegistration = new DeviceRegistrationManager();<\/code><\/p>\n<p><code>_page.NotificationHandle = AppDelegate.Instance.DeviceToken;<\/code><\/p>\n<p><code>}<\/code><\/p>\n<p>Make the following changes to the MainPageRenderer.cs file in the UWP project.\n<code>private MobileAppClient.MainPage _page;<\/code>\n<code>protected override void OnElementChanged(ElementChangedEventArgs&lt;Page&gt; e)<\/code><\/p>\n<p><code>{<\/code><\/p>\n<p><code>base.OnElementChanged(e);<\/code><\/p>\n<p><code>_page = e.NewElement as MobileAppClient.MainPage;<\/code><\/p>\n<p><code>_page.PlatformParameters = new PlatformParameters(PromptBehavior.Auto, true);<\/code><\/p>\n<p><code>}<\/code><\/p>\n<p>With these changes in place the PlatformParameters property will be correctly populated on each platform before the code attempts to authenticate with Azure Active Directory. Rebuild the solution and run it on each platform. You should be able to successfully authenticate and invoke the Values API on the Azure Mobile App Service.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/31\/2019\/04\/clip_image0102.jpg\"><img decoding=\"async\" title=\"clip_image010\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/31\/2019\/04\/clip_image010_thumb2.jpg\" alt=\"clip_image010\" width=\"1028\" height=\"580\" border=\"0\" \/><\/a><\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/31\/2019\/04\/clip_image0122.jpg\"><img decoding=\"async\" title=\"clip_image012\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/31\/2019\/04\/clip_image012_thumb2.jpg\" alt=\"clip_image012\" width=\"1028\" height=\"612\" border=\"0\" \/><\/a><\/p>\n<h2>Wrapping Up<\/h2>\n<p>This post has provided you with the basic information needed to leverage client directed authentication with Azure Active Directory to authenticate with an Azure Mobile App Service. This approach can be used with any of the identity providers supported by the Azure Mobile App Service such as Azure Active Directory, Facebook, Google, Microsoft Account, and Twitter.<\/p>\n<hr \/>\n<p><a href=\"https:\/\/blogs.msdn.com\/b\/premier_developer\/archive\/2014\/09\/15\/welcome.aspx\"><strong>Premier Support for Developers<\/strong><\/a> provides strategic technology guidance, critical support coverage, and a range of essential services to help teams optimize development lifecycles and improve software quality.\u00a0 Contact your Application Development Manager (ADM) or <a href=\"https:\/\/blogs.msdn.microsoft.com\/premier_developer\/contact-us\/\"><b>email us<\/b><\/a> to learn more about what we can do for you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>App Dev Manager Nicholas McCollum walks through creating an Azure Mobile App that uses client directed authentication via Azure AD. Azure Mobile Apps are built on Azure App Services. Through the Azure portal you can configure your Azure Mobile App to provide sign in, push notifications, and data synchronization. When you configure sign in through [&hellip;]<\/p>\n","protected":false},"author":582,"featured_media":37840,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[25],"tags":[69,70,71,3],"class_list":["post-19455","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-azure","tag-azure-ad","tag-azure-mobile-apps","tag-mobile-development","tag-team"],"acf":[],"blog_post_summary":"<p>App Dev Manager Nicholas McCollum walks through creating an Azure Mobile App that uses client directed authentication via Azure AD. Azure Mobile Apps are built on Azure App Services. Through the Azure portal you can configure your Azure Mobile App to provide sign in, push notifications, and data synchronization. When you configure sign in through [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/posts\/19455","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/users\/582"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/comments?post=19455"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/posts\/19455\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/media\/37840"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/media?parent=19455"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/categories?post=19455"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/tags?post=19455"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}