While not technically a question, a recently new Windows Phone developer asked me why his conversion to the Universal app model wasn’t working with his previously working code in Silverlight. As the statement above shows, he wanted to know why his Azure mobile service authentication code was no longer working in his new universal app. The simple answer is that it is to do with the web authentication broker and there is a quick and easy fix to get this working.

Windows Phone Silverlight

Windows Phone Silverlight developers will be familiar with the image above when authenticating with social accounts using Azure mobile services. The authentication dialog is loaded within the app and is brought to the front and when the user has authenticated with the service, the app will return and process the authenticated user. However, Windows Phone universal apps use the web authentication broker which Windows 8 developers will be familiar with.

Windows UniversalThis is my app, Picstra, running the exact same code from a Silverlight implementation of Azure authentication. As you can tell, it looks slightly different in that we have the header. This isn’t the only difference though and you have to implement a callback in your app to return to the code it was running when it brought up the authentication dialog otherwise, like the developer who got in touch, you will return to your app and it won’t do anything.

To return to your running code, open your App.xaml.cs file and override the OnActivated method. We need to do this because the app is technically out of context when the authentication dialog is shown in universal apps and returning to your app afterwards requires us to handle that. Here is the code we need to add to handle the web authentication broker.

            if (args.Kind == ActivationKind.WebAuthenticationBrokerContinuation)
                ViewModelLocator.MobileService.LoginComplete(args as WebAuthenticationBrokerContinuationEventArgs);

When the user has finished authenticating using the dialog, the app will call this line of code and will carry on executing any code you have after you call for authentication of the user with Azure. That’s all you need to do! A very simple fix which will have your ported app up and running in no time!

If you have any questions on this, leave a comment in the section below and if you have a development question of your own, get in touch!

Join the discussion One Comment

Leave a Reply to How to handle Azure authentication in Windows Phone universal apps Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.