Adding WPF Client with Azure Active Directory Authentication and Azure Mobile Service

In today’s post I was going to cover adding Azure AD authentication to my iOS project but I don’t have my Mac build machine handy so there’s a bit of a change of plans. Today I’m going to add a WPF desktop application to my solution and catch it up to where the other client applications are up to. I’ll start by creating the WPF Application.


At this point I realised that I missed setting the framework in the Add New Project dialog, so I opened the Properties pane of the application and on the Application tab I set the Target framework to .Net Framework 4.5.


Next we need to add references to the following solution projects:

– RealEstateInspector.Core

– RealEstateInspector.Shared.Client

And then add reference to both the ADAL and Mobile Service SqliteStore packages. From the downloads page on you’ll also need to download the “32-bit DLL (x86) for SQLite” which is under the Precompiled Binaries for Windows heading. Unblock and Extract the zip file and add the sqlite3.dll file to the new WPF project, setting the Build Action to Content and setting the Copy to Output Directory to Copy always.

I also need to check the Prefer 32-bit checkbox and define the DESKTOP compilation symbol for All Configurations.


I’ll add the following XAML markup to the MainWindow.xaml

<Window x_Class=”RealEstateInspector.Desktop.MainWindow”
        Title=”MainWindow” Height=”350″ Width=”525″>
                    Text=”{Binding Address}”
                    Foreground=”WhiteSmoke” />
                Height=”Auto” />
            <RowDefinition />
            Click=”AuthenticateClick” />
            ItemTemplate=”{StaticResource PropertyItemTemplate}”
            ItemsSource=”{Binding Properties}” />

And the following code to the MainWindow.xaml.cs

public partial class MainWindow : IWin32Window
    public IntPtr Handle
            var interopHelper = new WindowInteropHelper(this);
            return interopHelper.Handle;
    public MainWindow()

        Loaded += MainWindow_Loaded;
    public MainViewModel CurrentViewModel
        get { return DataContext as MainViewModel; }
    void MainWindow_Loaded(object sender, RoutedEventArgs e)
        var vm = new MainViewModel();
        DataContext = vm;

    private async void AuthenticateClick(object sender, RoutedEventArgs e)
        var token = await AuthenticationHelper.Authenticate(Handle);
        await CurrentViewModel.LoadPropertyData(token);

I also had to make some minor changes to the AuthenticationHelper

public static class AuthenticationHelper

    public static async Task<string> Authenticate(
    Android.App.Activity callerActivity
IntPtr callerHandle
            var authContext = new AuthenticationContext(Constants.ADAuthority);
            if (authContext.TokenCache.ReadItems().Count() > 0)
                authContext = new AuthenticationContext(authContext.TokenCache.ReadItems().First().Authority);
            var authResult =
                    new Uri(Constants.ADRedirectUri),
                    new AuthorizationParameters()
#elif DROID
                    new AuthorizationParameters(callerActivity)
                        new AuthorizationParameters(PromptBehavior.Auto, callerHandle)
                        new AuthorizationParameters(PromptBehavior.Auto, false)
            Debug.WriteLine(authResult != null);

            return authResult.AccessToken;

        catch (Exception ex)
            return null;

And that’s it, the WPF desktop application can be run up, the user can sign in and properties are synchronized before being displayed.

Leave a comment