Introduction
This sample shows how to connect universal apps to Facebook, Google and Microsoft accounts using the MVVM pattern.
Building the Sample
You only need Visual Studio 2012 or Visual Studio 2013 and Windows 8 or Windows 8.1, both the RTM version.
Description
Recently published was Authentication using Facebook, Google and Microsoft account in WP8.0 App (MVVM) and this sample has the same goal, but now the target is Universal Apps. Both have the goal to use the MVVM pattern.
Before starting this sample, analize to see if the SDKs used in the first sample could be used in this new sample and in a first attempt didn´t find a package for all the targets (Windows 8.1 and Windows Phone 8.1 Runtime). Let's see that analysis.
The packages used was:
- Facebook SDK for Windows Phone (http://facebooksdk.net/docs/phone/ )
- Google APIs Auth Client and Google APIs OAuth2 Client (https://www.nuget.org/packages/Google.Apis.Auth/ and https://www.nuget.org/packages/Google.Apis.Authentication/1.6.0-beta )
- Live SDK (http://msdn.microsoft.com/en-US/onedrive/dn630256 )
The packages that were analized are:
- Facebook SDK for Windows Phone: there is a package for Windows 8.1 Store Apps but there isn't for Windows Phone 8.1 Runtime (we cannot use the version from Windows Phone 8.0 because it uses a namespace for controls that does not exist in the Windows Phone 8.1 Runtime)
- Google APIs Auth Client and Google APIs OAuth2 Client: there is a package compatible with Windows 8.1 Store Apps but it is a bit different from the sample created before, the API changed. And there isn't a package for the Windows Phone 8.1 Runtime.
- Live SDK: is compatible with the Windows Phone 8.1 Runtime and Windows 8.1.
The next step, was to try to port the package for Google from Windows 8.1 Store Apps to Windows Phone 8.1 Runtime, create the logic because there is a lot of code shared between them and then the hard work began.
After some attempts, the code started to throw the exception NotImplementedException because the WebAuthenticationBroker class does not work the same way for these targets. There is a sample that shows this difference, here is the source code and we will see this in this sample.
In conclusion of this analysis, I decided to use WebAuthenticationBroker for authentication using Facebook and Google accounts and Live SDK for Microsoft accounts.
Let's start the sample!
Note: This sample uses MVVM Light and Cimbalino Toolkit.
For each provider it is necessary to get the app id/client id/client secrect in their websites.
For Google go to https://console.developers.google.com/project and create a new project (APIs and auth > credentials).
For Facebook go to https://developers.facebook.com/ and create a new app.
For Live SDK go to https://account.live.com/developers/applications/index and create one or use an existing app.
Before you start you should change the Constant file to add client ids / client secret / app id, without it the app fails!!
- /// <summary>
- /// Defines the constants strings used in the app.
- /// </summary>
- public class Constants
- {
- /// <summary>
- /// The google callback url.
- /// </summary>
- #if !WINDOWS_PHONE_APP
- public const string GoogleCallbackUrl = "urn:ietf:wg:oauth:2.0:oob";
- #else
- public const string GoogleCallbackUrl = "http://localhost";
- #end
- /// <summary>
- /// The facebook app id.
- /// </summary>
- public const string FacebookAppId = "<app id>";
- /// <summary>
- /// The google client identifier.
- /// </summary>
- public const string GoogleClientId = "<client id>";
- /// <summary>
- /// The google client secret.
- /// </summary>
- public const string GoogleClientSecret = "<client secret";
- /// <summary>
- /// The login token.
- /// </summary>
- public const string LoginToken = "LoginToken";
- /// <summary>
- /// The facebook provider.
- /// </summary>
- public const string FacebookProvider = "facebook";
- /// <summary>
- /// The google provider.
- /// </summary>
- public const string GoogleProvider = "google";
- /// <summary>
- /// The microsoft provider.
- /// </summary>
- public const string MicrosoftProvider = "microsoft";
- }
The following are the classes created:
- FacebookService has all code related with authentication with Facebook account;
- MicrosoftService has all code related with authentication with Microsoft account;
- GoogleService has all code related with authentication with Google account;
- SessionService call the methods login or logout for the provide requested;
The Flow
To help to understood the difference in the flow in each platform some diagrams were created.
The flow for the Windows 8.1 Store apps will be:
The flow for Windows Phone 8.1 Runtime will be:
- using Microsoft account

- using Facebook Account or Google account

Note: Start in LoginView using the blue arrow and when the blue flow finnishes, go to FacebookService/GoogleService and follow the red flow.
Like we can see, the authentication for Windows Phone 8.1 is very complicated, it could be easier like in Windows 8.1 Store apps. And It breaks the MVVM Pattern!
This sample is a Universal App, for this reason the code can be found in the Shared Project and to add specific features for each target directives (#if #else #endif) are used, it can cause some difficulties for understanding the code but is a good way to have only one code in one place. Partial methods and classes can be used here, because in most cases directives are added to add a method for Windows Phone.
The FacebookService
- /// <summary>
- /// Defines the Facebook Service.
- /// </summary>
- public class FacebookService : IFacebookService
- {
- private readonly ILogManager _logManager;
- /// <summary>
- /// Initializes a new instance of the <see cref="FacebookService"/> class.
- /// </summary>
- /// <param name="logManager">
- /// The log manager.
- /// </param>
- public FacebookService(ILogManager logManager)
- {
- _logManager = logManager;
- }
- /// <summary>
- /// The login sync.
- /// </summary>
- /// <returns>
- /// The <see cref="Task"/> object.
- /// </returns>
- public async Task<Session> LoginAsync()
- {
- const string FacebookCallbackUrl = "https://m.facebook.com/connect/login_success.html";
- var facebookUrl = "https://www.facebook.com/dialog/oauth?client_id=" + Uri.EscapeDataString(Constants.FacebookAppId) + "&redirect_uri=" + Uri.EscapeDataString(FacebookCallbackUrl) + "&scope=public_profile,email&display=popup&response_type=token";
- var startUri = new Uri(facebookUrl);
- var endUri = new Uri(FacebookCallbackUrl);
- #if WINDOWS_PHONE_APP
- WebAuthenticationBroker.AuthenticateAndContinue(startUri, endUri, null, WebAuthenticationOptions.None);
- return null;
- #else
- var webAuthenticationResult = await WebAuthenticationBroker.AuthenticateAsync(WebAuthenticationOptions.None, startUri, endUri);
- return GetSession(webAuthenticationResult);
- #endif
- }
- private void GetKeyValues(string webAuthResultResponseData, out string accessToken, out string expiresIn)
- {
- string responseData = webAuthResultResponseData.Substring(webAuthResultResponseData.IndexOf("access_token", StringComparison.Ordinal));
- string[] keyValPairs = responseData.Split('&');
- accessToken = null;
- expiresIn = null;
- for (int i = 0; i < keyValPairs.Length; i++)
- {
- string[] splits = keyValPairs[i].Split('=');
- switch (splits[0])
- {
- case "access_token":
- accessToken = splits[1];
- break;
- case "expires_in":
- expiresIn = splits[1];
- break;
- }
- }
- }
- /// <summary>
- /// This function extracts access_token from the response returned from web authentication broker
- /// and uses that token to get user information using facebook graph api.
- /// </summary>
- /// <param name="accessToken">
- /// The access Token.
- /// </param>
- /// <returns>
- /// The <see cref="Task"/>.
- /// </returns>
- private async Task<UserInfo> GetFacebookUserNameAsync(string accessToken)
- {
- var httpClient = new HttpClient();
- var response = await httpClient.GetStringAsync(new Uri("https://graph.facebook.com/me?access_token=" + accessToken));
- var value = JsonValue.Parse(response).GetObject();
- var facebookUserName = value.GetNamedString("name");
- return new UserInfo
- {
- Name = facebookUserName,
- };
- }
- /// <summary>
- /// Logouts this instance.
- /// </summary>
- public async void Logout()
- {
- Exception exception = null;
- try
- {
- }
- catch (Exception ex)
- {
- exception = ex;
- }
- if (exception != null)
- {
- await _logManager.LogAsync(exception);
- }
- }
- #if WINDOWS_PHONE_APP
- public async Task<Session> Finalize(WebAuthenticationBrokerContinuationEventArgs args)
- {
- Exception exception = null;
- try
- {
- var result = args.WebAuthenticationResult;
- return GetSession(result);
- }
- catch (Exception e)
- {
- exception = e;
- }
- await _logManager.LogAsync(exception);
- return null;
- }
- #endif
- private Session GetSession(WebAuthenticationResult result)
- {
- if (result.ResponseStatus == WebAuthenticationStatus.Success)
- {
- string accessToken;
- string expiresIn;
- GetKeyValues(result.ResponseData, out accessToken, out expiresIn);
- return new Session
- {
- AccessToken = accessToken,
- ExpireDate = new DateTime(long.Parse(expiresIn)),
- Provider = Constants.FacebookProvider
- };
- }
- if (result.ResponseStatus == WebAuthenticationStatus.ErrorHttp)
- {
- throw new Exception("Error http");
- }
- if (result.ResponseStatus == WebAuthenticationStatus.UserCancel)
- {
- throw new Exception("User Canceled.");
- }
- return null;
- }
- }
public class GoogleService : IGoogleService { private readonly ILogManager _logManager; /// <summary> /// Initializes a new instance of the <see cref="GoogleService"/> class. /// </summary> /// <param name="logManager"> /// The log manager. /// </param> public GoogleService(ILogManager logManager) { _logManager = logManager; } /// <summary> /// The login async. /// </summary> /// <returns> /// The <see cref="Task"/> object. /// </returns> public async Task<Session> LoginAsync() { var googleUrl = new StringBuilder(); googleUrl.Append(Uri.EscapeDataString(Constants.GoogleClientId)); googleUrl.Append("&scope=openid%20email%20profile"); googleUrl.Append("&redirect_uri="); googleUrl.Append(Uri.EscapeDataString(Constants.GoogleCallbackUrl)); googleUrl.Append("&state=foobar"); googleUrl.Append("&response_type=code"); var startUri = new Uri(googleUrl.ToString()); #if !WINDOWS_PHONE_APP var webAuthenticationResult = await WebAuthenticationBroker.AuthenticateAsync(WebAuthenticationOptions.UseTitle, startUri, endUri); return await GetSession(webAuthenticationResult);#else WebAuthenticationBroker.AuthenticateAndContinue(startUri, newUri(Constants.GoogleCallbackUrl), null, WebAuthenticationOptions.None); return null;#endif } private string GetCode(string webAuthResultResponseData) { // Success code=4/izytpEU6PjuO5KKPNWSB4LK3FU1c var split = webAuthResultResponseData.Split('&'); return split.FirstOrDefault(value => value.Contains("code")); } /// <summary> /// The logout. /// </summary> public void Logout() { } #if WINDOWS_PHONE_APP public async Task<Session> Finalize(WebAuthenticationBrokerContinuationEventArgs args) { Exception exception = null; try { return await GetSession(args.WebAuthenticationResult); } catch (Exception e) { exception = e; } await _logManager.LogAsync(exception); return null; }#endif private async Task<Session> GetSession(WebAuthenticationResult result) { if (result.ResponseStatus == WebAuthenticationStatus.Success) { var code = GetCode(result.ResponseData); var serviceRequest = await GetToken(code); return new Session { AccessToken = serviceRequest.access_token, ExpireDate = new DateTime(long.Parse(serviceRequest.expires_in)), Id = serviceRequest.id_token, Provider = Constants.GoogleProvider }; } if (result.ResponseStatus == WebAuthenticationStatus.ErrorHttp) { throw new Exception("Error http"); } if (result.ResponseStatus == WebAuthenticationStatus.UserCancel) { throw new Exception("User Canceled."); } return null; } private static async Task<ServiceResponse> GetToken(string code) { var body = new StringBuilder(); body.Append(code); body.Append("&client_id="); body.Append(Uri.EscapeDataString(Constants.GoogleClientId)); body.Append("&client_secret="); body.Append(Uri.EscapeDataString(Constants.GoogleClientSecret)); body.Append("&redirect_uri="); body.Append(Uri.EscapeDataString(Constants.GoogleCallbackUrl)); body.Append("&grant_type=authorization_code"); var client = new HttpClient(); var request = new HttpRequestMessage(HttpMethod.Post, new Uri(TokenUrl)) { Content = new StringContent(body.ToString(), Encoding.UTF8, "application/x-www-form-urlencoded") }; var response = await client.SendAsync(request); var content = await response.Content.ReadAsStringAsync(); var serviceTequest = JsonConvert.DeserializeObject<ServiceResponse>(content); return serviceTequest; } }Note: FacebookService and GoogleService are similar but the response and request are different for the reason that these classes are not joined.
Google changed the authentication and this sample uses the last version. See more about it here .
- /// <summary>
- /// The microsoft service.
- /// </summary>
- public class MicrosoftService : IMicrosoftService
- {
- private readonly ILogManager _logManager;
- private LiveAuthClient _authClient;
- private LiveConnectSession _liveSession;
- /// <summary>
- /// Defines the scopes the application needs.
- /// </summary>
- private List<string> _scopes;
- /// <summary>
- /// Initializes a new instance of the <see cref="MicrosoftService"/> class.
- /// </summary>
- /// <param name="logManager">
- /// The log manager.
- /// </param>
- public MicrosoftService(ILogManager logManager)
- {
- _scopes = new List<string> { "wl.signin", "wl.basic", "wl.offline_access" };
- _logManager = logManager;
- }
- /// <summary>
- /// The login async.
- /// </summary>
- /// <returns>
- /// The <see cref="Task"/> object.
- /// </returns>
- public async Task<Session> LoginAsync()
- {
- Exception exception = null;
- try
- {
- _authClient = new LiveAuthClient();
- var loginResult = await _authClient.InitializeAsync(_scopes);
- var result = await _authClient.LoginAsync(_scopes);
- if (result.Status == LiveConnectSessionStatus.Connected)
- {
- _liveSession = loginResult.Session;
- var session = new Session
- {
- AccessToken = result.Session.AccessToken,
- Provider = Constants.MicrosoftProvider,
- };
- return session;
- }
- }
- catch (LiveAuthException ex)
- {
- throw new InvalidOperationException("Login canceled.", ex);
- }
- catch (Exception e)
- {
- exception = e;
- }
- await _logManager.LogAsync(exception);
- return null;
- }
- /// <summary>
- /// The get user info.
- /// </summary>
- /// <returns>
- /// The <see cref="Task"/> object.
- /// </returns>
- public async Task<IDictionary<string, object>> GetUserInfo()
- {
- Exception exception = null;
- try
- {
- var liveClient = new LiveConnectClient(_liveSession);
- LiveOperationResult operationResult = await liveClient.GetAsync("me");
- return operationResult.Result;
- }
- catch (LiveConnectException e)
- {
- exception = e;
- }
- await _logManager.LogAsync(exception);
- return null;
- }
- /// <summary>
- /// The logout.
- /// </summary>
- public async void Logout()
- {
- if (_authClient == null)
- {
- _authClient = new LiveAuthClient();
- var loginResult = await _authClient.InitializeAsync(_scopes);
- }
- if (_authClient.CanLogout)
- {
- _authClient.Logout();
- }
- }
- }

That will result in an additional file in the project: as in the following:

Note
- These file are removed from the sample because each developer has their own.
- In devices that the Microsoft account is logged the application will do the login automatically.
The SessionService
- /// <summary>
- /// The service session.
- /// </summary>
- public class SessionService : ISessionService
- {
- private readonly IApplicationDataService _applicationSettings;
- private readonly IFacebookService _facebookService;
- private readonly IMicrosoftService _microsoftService;
- private readonly IGoogleService _googleService;
- private readonly ILogManager _logManager;
- /// <summary>
- /// Initializes a new instance of the <see cref="SessionService" /> class.
- /// </summary>
- /// <param name="applicationSettings">The application settings.</param>
- /// <param name="facebookService">The facebook service.</param>
- /// <param name="microsoftService">The microsoft service.</param>
- /// <param name="googleService">The google service.</param>
- /// <param name="logManager">The log manager.</param>
- public SessionService(IApplicationDataService applicationSettings,
- IFacebookService facebookService,
- IMicrosoftService microsoftService,
- IGoogleService googleService, ILogManager logManager)
- {
- _applicationSettings = applicationSettings;
- _facebookService = facebookService;
- _microsoftService = microsoftService;
- _googleService = googleService;
- _logManager = logManager;
- }
- /// <summary>
- /// Gets the session.
- /// </summary>
- /// <returns>The session object.</returns>
- public Session GetSession()
- {
- var expiryValue = DateTime.MinValue;
- string expiryTicks = LoadEncryptedSettingValue("session_expiredate");
- if (!string.IsNullOrWhiteSpace(expiryTicks))
- {
- long expiryTicksValue;
- if (long.TryParse(expiryTicks, out expiryTicksValue))
- {
- expiryValue = new DateTime(expiryTicksValue);
- }
- }
- var session = new Session
- {
- AccessToken = LoadEncryptedSettingValue("session_token"),
- Id = LoadEncryptedSettingValue("session_id"),
- ExpireDate = expiryValue,
- Provider = LoadEncryptedSettingValue("session_provider")
- };
- _applicationSettings.LocalSettings[Constants.LoginToken] = true;
- return session;
- }
- /// <summary>
- /// The save session.
- /// </summary>
- /// <param name="session">
- /// The session.
- /// </param>
- private void Save(Session session)
- {
- SaveEncryptedSettingValue("session_token", session.AccessToken);
- SaveEncryptedSettingValue("session_id", session.Id);
- SaveEncryptedSettingValue("session_expiredate", session.ExpireDate.Ticks.ToString(CultureInfo.InvariantCulture));
- SaveEncryptedSettingValue("session_provider", session.Provider);
- _applicationSettings.LocalSettings[Constants.LoginToken] = true;
- }
- /// <summary>
- /// The clean session.
- /// </summary>
- private void CleanSession()
- {
- _applicationSettings.LocalSettings.Remove("session_token");
- _applicationSettings.LocalSettings.Remove("session_id");
- _applicationSettings.LocalSettings.Remove("session_expiredate");
- _applicationSettings.LocalSettings.Remove("session_provider");
- _applicationSettings.LocalSettings.Remove(Constants.LoginToken);
- }
- /// <summary>
- /// The login async.
- /// </summary>
- /// <param name="provider">
- /// The provider.
- /// </param>
- /// <returns>
- /// The <see cref="Task"/> object.
- /// </returns>
- public async Task<bool?> LoginAsync(string provider)
- {
- Provider = provider;
- Exception exception;
- try
- {
- Session session = null;
- switch (provider)
- {
- case Constants.FacebookProvider:
- session = await _facebookService.LoginAsync();
- break;
- case Constants.MicrosoftProvider:
- session = await _microsoftService.LoginAsync();
- break;
- case Constants.GoogleProvider:
- session = await _googleService.LoginAsync();
- break;
- }
- if (session == null)
- {
- return null;
- }
- Save(session);
- return true;
- }
- catch (Exception ex)
- {
- exception = ex;
- }
- await _logManager.LogAsync(exception);
- return false;
- }
- /// <summary>
- /// Gets or sets the provider.
- /// </summary>
- /// <value>
- /// The provider.
- /// </value>
- public string Provider { get; set; }
- /// <summary>
- /// The logout.
- /// </summary>
- public async void Logout()
- {
- Exception exception = null;
- try
- {
- var session = GetSession();
- switch (session.Provider)
- {
- case Constants.FacebookProvider:
- _facebookService.Logout();
- break;
- case Constants.MicrosoftProvider:
- _microsoftService.Logout();
- break;
- case Constants.GoogleProvider:
- _googleService.Logout();
- break;
- }
- CleanSession();
- }
- catch (Exception ex)
- {
- exception = ex;
- }
- if (exception != null)
- {
- await _logManager.LogAsync(exception);
- }
- }
- WINDOWS_PHONE_APP
- public async Task<bool> Finalize(WebAuthenticationBrokerContinuationEventArgs args)
- {
- Exception exception = null;
- try
- {
- Session session = null;
- switch (Provider)
- {
- case Constants.FacebookProvider:
- session = await _facebookService.Finalize(args);
- break;
- case Constants.GoogleProvider:
- session = await _googleService.Finalize(args);
- break;
- }
- Save(session);
- return true;
- }
- catch (Exception e)
- {
- exception = e;
- }
- await _logManager.LogAsync(exception);
- return false;
- }
- if
- /// <summary>
- /// Loads an encrypted setting value for a given key.
- /// </summary>
- /// <param name="key">
- /// The key to load.
- /// </param>
- /// <returns>
- /// The value of the key.
- /// </returns>
- private string LoadEncryptedSettingValue(string key)
- {
- string value = null;
- var protectedBytes = _applicationSettings.LocalSettings[key];
- if (protectedBytes != null)
- {
- // todo use DataProtectionProvider
- // byte[] valueBytes = ProtectedData.Unprotect(protectedBytes, null);
- // value = Encoding.UTF8.GetString(valueBytes, 0, valueBytes.Length);
- value = protectedBytes.ToString();
- }
- return value;
- }
- /// <summary>
- /// Saves a setting value against a given key, encrypted.
- /// </summary>
- /// <param name="key">
- /// The key to save against.
- /// </param>
- /// <param name="value">
- /// The value to save against.
- /// </param>
- /// <exception cref="System.ArgumentOutOfRangeException">
- /// The key or value provided is unexpected.
- /// </exception>
- private void SaveEncryptedSettingValue(string key, string value)
- {
- if (!string.IsNullOrWhiteSpace(key) && !string.IsNullOrWhiteSpace(value))
- {
- // todo use DataProtectionProvider
- // byte[] valueBytes = Encoding.UTF8.GetBytes(value);
- // var dataProtectionProvider = new DataProtectionProvider();
- // // Encrypt the value by using the Protect() method.
- // byte[] protectedBytes = await dataProtectionProvider.ProtectAsync(valueBytes);
- _applicationSettings.LocalSettings[key] = value;
- }
- }
- }
Here is the class diagram with all classes and interfaces created in this sample:

Now to build the User Interface and because we are using the MVVM pattern, it created a LoginViewModel to do the binding to the LoginView.
The LoginViewModel
- /// <summary>
- /// The login view model.
- /// </summary>
- public class LoginViewModel : ViewModelBase
- {
- private readonly ILogManager _logManager;
- private readonly IMessageBoxService _messageBox;
- private readonly INetworkInformationService _networkInformationService;
- private readonly INavigationService _navigationService;
- private readonly ISessionService _sessionService;
- private bool _inProgress;
- /// <summary>
- /// Initializes a new instance of the <see cref="LoginViewModel"/> class.
- /// </summary>
- /// <param name="navigationService">
- /// The navigation service.
- /// </param>
- /// <param name="sessionService">
- /// The session service.
- /// </param>
- /// <param name="messageBox">
- /// The message box.
- /// </param>
- /// <param name="networkInformationService">
- /// The network connection.
- /// </param>
- /// <param name="logManager">
- /// The log manager.
- /// </param>
- public LoginViewModel(INavigationService navigationService,
- ISessionService sessionService,
- IMessageBoxService messageBox,
- INetworkInformationService networkInformationService,
- ILogManager logManager)
- {
- _navigationService = navigationService;
- _sessionService = sessionService;
- _messageBox = messageBox;
- _networkInformationService = networkInformationService;
- _logManager = logManager;
- LoginCommand = new RelayCommand<string>(LoginAction);
- }
- /// <summary>
- /// Gets the navigation service.
- /// </summary>
- /// <value>
- /// The navigation service.
- /// </value>
- public INavigationService NavigationService
- {
- get { return _navigationService; }
- }
- /// <summary>
- /// Gets or sets a value indicating whether in progress.
- /// </summary>
- /// <value>
- /// The in progress.
- /// </value>
- public bool InProgress
- {
- get { return _inProgress; }
- set { Set(() => InProgress, ref _inProgress, value); }
- }
- /// <summary>
- /// Gets the facebook login command.
- /// </summary>
- /// <value>
- /// The facebook login command.
- /// </value>
- public ICommand LoginCommand { get; private set; }
- /// <summary>
- /// Facebook's login action.
- /// </summary>
- /// <param name="provider">
- /// The provider.
- /// </param>
- private async void LoginAction(string provider)
- {
- Exception exception = null;
- bool isToShowMessage = false;
- try
- {
- if (!_networkInformationService.IsNetworkAvailable)
- {
- await _messageBox.ShowAsync("There isn´t network connection.",
- "Authentication Sample",
- new List<string> { "Ok" });
- return;
- }
- if (Constants.GoogleClientId.Contains("<") || Constants.GoogleClientSecret.Contains("<"))
- {
- await _messageBox.ShowAsync("Is missing the google client id and client secret. Search for Constant.cs file.",
- "Authentication Sample",
- new List<string> { "Ok" });
- return;
- }
- if (Constants.FacebookAppId.Contains("<"))
- {
- await _messageBox.ShowAsync("Is missing the facebook client id. Search for Constant.cs file.",
- "Authentication Sample",
- new List<string> { "Ok" });
- return;
- }
- InProgress = true;
- var auth = await _sessionService.LoginAsync(provider);
- if (auth == null)
- {
- return;
- }
- if (!auth.Value)
- {
- await ShowMessage();
- }
- else
- {
- _navigationService.Navigate<MainView>();
- InProgress = false;
- }
- InProgress = false;
- }
- catch (Exception ex)
- {
- InProgress = false;
- exception = ex;
- isToShowMessage = true;
- }
- if (isToShowMessage)
- {
- await _messageBox.ShowAsync("Application fails.",
- "Authentication Sample",
- new List<string> { "Ok" });
- }
- if (exception != null)
- {
- await _logManager.LogAsync(exception);
- }
- }
- private async Task ShowMessage()
- {
- await _messageBox.ShowAsync("Wasn´t possible to complete the login.",
- "Authentication Sample",
- new List<string>
- {
- "Ok"
- });
- }
- #if WINDOWS_PHONE_APP
- public async void Finalize(WebAuthenticationBrokerContinuationEventArgs args)
- {
- var result = await _sessionService.Finalize(args);
- if (!result)
- {
- await ShowMessage();
- }
- else
- {
- _navigationService.Navigate<MainView>();
- InProgress = false;
- }
- }
- #endif
- }
For the final the code, let's see the code for the page.
The LoginPage.xaml
- <Page
- x:Class="AuthenticationSample.UniversalApps.Views.LoginView"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- xmlns:converters="using:Cimbalino.Toolkit.Converters"
- mc:Ignorable="d">
- <Page.DataContext>
- <Binding Mode="OneWay"
- Path="LoginViewModel"
- Source="{StaticResource Locator}" />
- </Page.DataContext>
- <Page.Resources>
- <converters:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
- </Page.Resources>
- <!-- LayoutRoot is the root grid where all page content is placed -->
- <Grid x:Name="LayoutRoot" Background="LightGray">
- <Grid.RowDefinitions>
- <RowDefinition Height="{StaticResource HeaderHeigth}" />
- <RowDefinition Height="*" />
- </Grid.RowDefinitions>
- <!-- TitlePanel contains the name of the application and page title -->
- <StackPanel x:Name="TitlePanel"
- Margin="{StaticResource HeaderMargin}"
- VerticalAlignment="Center" Grid.Row="0">
- <TextBlock FontSize="30"
- Foreground="Black"
- Text="Login"/>
- </StackPanel>
- <!-- ContentPanel - place additional content here -->
- <Grid x:Name="ContentPanel"
- VerticalAlignment="Center"
- HorizontalAlignment="Center"
- Grid.Row="1">
- <Grid.RowDefinitions>
- <RowDefinition Height="Auto" />
- <RowDefinition Height="80" />
- <RowDefinition Height="80" />
- <RowDefinition Height="80" />
- <RowDefinition Height="80" />
- </Grid.RowDefinitions>
- <TextBlock Grid.Row="0"
- FontSize="20"
- Foreground="Black"
- Text="Use your account"/>
- <Button Grid.Row="1" Width="300"
- Margin="10"
- Command="{Binding LoginCommand}"
- CommandParameter="facebook"
- Background="{StaticResource FacebookBlueBackgroundBrush}" >
- <StackPanel Orientation="Horizontal">
- <TextBlock Text=""
- VerticalAlignment="Center"
- FontFamily="/Fonts/fontawesome-webfont.ttf#FontAwesome"
- HorizontalAlignment="Left"/>
- <TextBlock Margin="10,0,0,0" Text="Facebook"
- HorizontalAlignment="Center"/>
- </StackPanel>
- </Button>
- <Button Grid.Row="3"
- Margin="10" Width="300"
- Command="{Binding LoginCommand}"
- CommandParameter="microsoft"
- Background="{StaticResource MicrosoftBlueBackgroundBrush}" >
- <StackPanel Orientation="Horizontal">
- <TextBlock Text=""
- VerticalAlignment="Center"
- FontFamily="/Fonts/fontawesome-webfont.ttf#FontAwesome"
- HorizontalAlignment="Left"/>
- <TextBlock Margin="10,0,0,0" Text="Microsoft"
- HorizontalAlignment="Center"/>
- </StackPanel>
- </Button>
- <Button Grid.Row="2" Width="300"
- Margin="10"
- Command="{Binding LoginCommand}"
- CommandParameter="google"
- Background="{StaticResource GoogleRedBackgroundBrush}" >
- <StackPanel Orientation="Horizontal">
- <TextBlock Text=""
- VerticalAlignment="Center"
- FontFamily="/Fonts/fontawesome-webfont.ttf#FontAwesome"
- HorizontalAlignment="Left"/>
- <TextBlock Margin="10,0,0,0" Text="Google"
- HorizontalAlignment="Center"/>
- </StackPanel>
- </Button>
- </Grid>
- <Grid Visibility="{Binding InProgress, Converter={StaticResource BooleanToVisibilityConverter}}"
- Grid.Row="0"
- Grid.RowSpan="2">
- <Rectangle
- Fill="Black"
- Opacity="0.75" />
- <TextBlock
- HorizontalAlignment="Center"
- VerticalAlignment="Center"
- Text="Auth..." />
- <ProgressBar IsIndeterminate="True" IsEnabled="True" Margin="0,60,0,0"/>
- </Grid>
- </Grid>
- </Page>
The LoginView.xaml.cs
- /// <summary>
- /// An empty page that can be used on its own or navigated to within a Frame.
- /// </summary>
- #if WINDOWS_PHONE_APP
- public sealed partial class LoginView : Page, IWebAuthenticationContinuable
- #else
- public sealed partial class LoginView : Page
- #endif
- {
- /// <summary>
- /// Initializes a new instance of the <see cref="LoginView"/> class.
- /// </summary>
- public LoginView()
- {
- InitializeComponent();
- }
- /// <summary>
- /// The on navigated to.
- /// </summary>
- /// <param name="e">
- /// The e.
- /// </param>
- protected override void OnNavigatedTo(NavigationEventArgs e)
- {
- var viewModel = (LoginViewModel)DataContext;
- viewModel.NavigationService.RemoveBackEntry();
- base.OnNavigatedTo(e);
- }
- #if WINDOWS_PHONE_APP
- /// <summary>
- /// Continues the web authentication.
- /// </summary>
- /// <param name="args">The <see cref="Windows.ApplicationModel.Activation.WebAuthenticationBrokerContinuationEventArgs"/> instance containing the event data.</param>
- public void ContinueWebAuthentication(Windows.ApplicationModel.Activation.WebAuthenticationBrokerContinuationEventArgs args)
- {
- var viewModel = (LoginViewModel)DataContext;
- viewModel.Finalize(args);
- }
- #endif
- }
In conclusion, the flow is completely different for each platform and it has some impact in the development and in sharing the code. Maybe, for this reason there isn't any package for Windows Phone 8.1 that uses authentication.
Source Code
The source code can be found here.
Source Code Files
- IFacebookService interface for FacebookService
- IGoogleService interface for GoogleService
- ILogManager interface for LogManager
- IMicrosoftService interface for MicrosoftService
- ISessionProvider interface for all providers interface (common methods)
- ISessionService for SessionService
- Session class for save the information about the session (provider, token, expired date)
- FacebookService class that has all logic about the login / logout using Facebook provider
- GoogleService class that has all logic about the login / logout using Google provider
- MicrosoftService class that has all logic about the login / logout using Microsoft provider
- SessionService class for manage login/logout (it will use all services provides described before)
- LoginViewModel class for binding to LoginView.xaml
- LoginView class that represent the page for login
- MainViewModel class for binding to MainView.xaml
- MainView class that appear after the login is ok
- ViewModelLocator class contains static references to all the view model in application and provides an entry point for the bindings.
The solution
Build the Sample
- Start Visual Studio Express 2012 for Windows 8 and select File > Open > Project/Solution.
- Go to the directory in which you unzipped the sample. Go to the directory named for the sample and double-click the Visual Studio Express 2012 for Windows 8 Solution (.sln) file.
- Press F7 or use Build > Build Solution to build the sample.
Note: you can use Visual Studio 2013 in Windows 8.1.
Run the sample
To debug the app and then run it, press F5 or use Debug > Start Debugging. To run the app without debugging, press Ctrl+F5 or use Debug > Start Without Debugging
Output
The output for each target is:
- Windows 8.1 Store App
Login Page
Microsoft Authentication
Facebook Authentication
Google Authentication
- Windows Phone 8.1 App
Login View
Facebook Authentication
Google Authentication

SharadPosted Jul 17, 2015, 3:57 AM
good one...
Akash BhimaniPosted Aug 12, 2014, 5:34 AM
really helpful article for beginner
Rajeev KumarPosted Aug 2, 2014, 1:49 AM
One Of The Best Post Superb:>
Sara SilvaPosted Aug 1, 2014, 7:23 AM
I understood, for this reason I created the flow diagrams. The complicate is the use of WebAuthenticationBroker in WP... there is sample for WP8.0, maybe you should start with this sample...then see this.
bhargav batchuPosted Aug 1, 2014, 7:20 AM
little complicated to understand for learners
Chirag MakvanaPosted Jul 30, 2014, 12:12 PM
Very nice article..!!
Mohammad MirshahiPosted Jul 29, 2014, 3:27 PM
very very nice ... good!!!
Sara SilvaPosted Jul 29, 2014, 8:23 AM
Thanks :)
Ajay PatelPosted Jul 29, 2014, 8:03 AM
Really an awesome post !