ADAL 2.X Servicing Release Introduces Support for Windows Phone 8.1 Silverlight Apps

If you head to the NuGet gallery you’ll find that we just released an update to our ADAL .NET package.

This servicing update (we go from v2.13.112191810 to v2.14.201151115) fixes various bugs. The one you’re most likely to have stumbled upon is one issue with Windows Store apps publication – which is solved in the release.

This release also introduces a new feature: the ability to use ADAL in Windows Phone 8.1 Silverlight applications. Until now, ADAL only worked with Windows Phone 8.1 Store applications.

SL apps support was a feature you have been very vocal about: for example, see the twitter exchange I had with Ginny back in June.

image

In the last few months we heard more and more of that feedback – from internal and external customers. Although there are ways of getting tokens from those kind of apps (the code delta is non zero but pretty small, it’s more of a matter of creating an assembly for it rather than a winmd), we found ourselves spending cycles to help people understand their options – and we realized that it would have been more efficient for everybody if we would simply bite the bullet and include Windows Phone  8.1 Silverlight apps as a new target platform in our official NuGet. So, that’s exactly what we did Smile and given that the programming surface did not change, we were able to do this in a servicing release.

Using ADAL in a Windows Phone 8.1 Silverlight app

Using ADAL in a winphone 8.1 SL app is not very different from doing so in a winphone Windows Store one, which is why we are not releasing a new sample for it at this time. If you think you need one please let us know, though!

The main difference lies in the way in which SL apps deal with the continuation model (behind the scenes we use the WebAuthenticationBroker, which is why this only works with 8.1 and we still need to handle continuation).
The application events cycle is slightly different, which influences where you need to inject the continuation handling code.

Here there’s a quick walkthrough to show how to see ADAL in action in your Windows Phone 8.1 SL app.

  • Start from a blank Windows Phone 8.1 Silverlight app.
  • Add a reference to the latest ADAL: at this time you can do so via the following line in the package manager console.

Install-Package Microsoft.IdentityModel.Clients.ActiveDirectory -Version 2.14.201151115

  • Add some basic ADAL code: here I am using as is the same code I used for the Universal Apps post, where I make a simple Directory Graph call from a lone button on the default page. Note that you can use the exact same code, as the client is multitenant and will work with any directory tenant.
using System;
using System.Windows;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using Windows.ApplicationModel.Activation;

namespace WPSL_App1
{
    public partial class MainPage : PhoneApplicationPage, IWebAuthenticationContinuable
    {
        AuthenticationContext ac = null;
        // Constructor
        public MainPage()
        {
            InitializeComponent();            
        }

        protected override async void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            ac = await AuthenticationContext.CreateAsync("https://login.windows.net/common");        
        }

        private async void btnCallGraph_Click(object sender, RoutedEventArgs e)
        {
            AuthenticationResult result =
                 await ac.AcquireTokenSilentAsync("https://graph.windows.net",
                                                  "e11a0451-ac9d-4c89-afd8-d2fa3322ef68");
            if (result != null && result.Status == AuthenticationStatus.Success)
            {
                ShowGreeting(result);
            }
            else
            {
                ac.AcquireTokenAndContinue("https://graph.windows.net",
                                           "e11a0451-ac9d-4c89-afd8-d2fa3322ef68",
                                           new Uri("http://li"), ShowGreeting);
            } 
        }
        public async void ShowGreeting(AuthenticationResult ar)
        {            
            MessageBox.Show("hello, Mr/Ms " + ar.UserInfo.FamilyName);            
        }
        public async void ContinueWebAuthentication(WebAuthenticationBrokerContinuationEventArgs args)
        {
            await ac.ContinueAcquireTokenAsync(args);
        } 
    }
}

 

Same old, same old. Quick comments:

  • The page implements the usual IWebAuthenticationContinuable interface to handle continuation, via the ContinueWebAuthentication method toward the end of the class declaration
  • The init happens in OnNavigated given that we want an async method.
  • btn_CallGraph_CLick follows the usual pattern: if we have a token in our belly we get it silently and use it, otherwise we start the interactive token acquisition process and book the work to do (the ShowGreeting method) after that’s done

The slightly different part is in App.xaml.cs. Here there are the relevant parts:

namespace WPSL_App1
{
    interface IWebAuthenticationContinuable
    {
        /// <summary>
        /// This method is invoked when the web authentication broker returns
        /// with the authentication result
        /// </summary>
        /// <param name="args">Activated event args object that contains returned authentication token</param>
        void ContinueWebAuthentication(WebAuthenticationBrokerContinuationEventArgs args);
    }
    public partial class App : Application
    {
        /// ... stuff ...
        private void Application_ContractActivated(object sender, Windows.ApplicationModel.Activation.IActivatedEventArgs e)
        {
            var webAuthenticationBrokerContinuationEventArgs = e as WebAuthenticationBrokerContinuationEventArgs;
            if (webAuthenticationBrokerContinuationEventArgs != null)
            {
                var wabPage = RootFrame.Content as IWebAuthenticationContinuable;
                if (wabPage != null)
                {
                    wabPage.ContinueWebAuthentication(webAuthenticationBrokerContinuationEventArgs);
                }
            }
        }
    /// ... more stuff

 

  • At the top I declare the IWebAuthenticationContinuable interface. It can go anywhere, really. I just added it here ‘cause I am lazy and sloppy.
  • The method Application_ContractActivated is the functional equivalent of OnActivated in the Windows Store version of the app (see here) hence that’s where we handle continuation

That’s really all you need! Let’s give it a spin: hit F5.

image

Click on the big button. You’ll see the familiar sign in experience from AAD.

image

Sign in.

image

You’ll get the usual consent page. Accept, and…

image

…voila’! I securely accessed my previous corporate data from my Windows Phone 8.1 Silverlight app, with just few lines of code.

Giving Feedback Works!!!

As this new feature demonstrates, we do try to listen and act on your feedback to the best of our abilities. Silverlight support was not in the cards, but your relentless requests helped us to better understand the impact of not supporting it.

I hope this will inspire to be vocal about our libraries and services, because well… it works! Smile
In fact, if you want to be even more directly involved, I’ll take this opportunity to remind you that ADAL is open source and with it all of our libraries. Feel free to file issues, give us feedback and contribute… we *love* it when you do.

Happy coding!

Leave a Reply

Your email address will not be published. Required fields are marked *