/// <summary>
        /// Get the authentication session.
        /// </summary>
        /// <returns>The session object.</returns>
        /// 
        public AuthenticationSession GetAuthenticationSession()
        {
            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 authenticationSession = new AuthenticationSession
            {
                AccessToken = LoadEncryptedSettingValue("session_token"),
                UserInfo = LoadEncryptedSettingValue("session_user"),
                Id = LoadEncryptedSettingValue("session_id"),
                ExpireDate = expiryValue,
                Provider = LoadEncryptedSettingValue("session_provider")
            };

            _applicationSettings.LocalSettings[Constants.LoginToken] = true;
            return authenticationSession;
        }
 /// <summary>
 /// The authentication save session.
 /// </summary>
 /// <param name="session">
 /// The authentication session.
 /// </param>
 //private void Save(AuthenticationSession session)
 public void SaveAuthenticationSession(AuthenticationSession session)
 {
     SaveEncryptedSettingValue("session_user", session.UserInfo);
     SaveEncryptedSettingValue("session_token", session.AccessToken);
     SaveEncryptedSettingValue("session_id", session.Id);
     SaveEncryptedSettingValue("session_expiredate", session.ExpireDate.Ticks.ToString(CultureInfo.InvariantCulture));
     SaveEncryptedSettingValue("session_provider", session.Provider);
 }
        /// <summary>
        /// The login async.
        /// </summary>
        /// <returns>
        /// The <see cref="Task"/> object.
        /// </returns>
        /// 
        public async Task<AuthenticationSession> LoginAsync()
        {
            Exception rException = null;
            try
            {
                _authClient = new LiveAuthClient();
                // Rui: https://msdn.microsoft.com/en-us/library/hh694244.aspx
                //var loginResult = await _authClient.InitializeAsync(_scopes);
                
                // Rui: https://msdn.microsoft.com/en-us/library/hh533677.aspx
                var result = await _authClient.LoginAsync(_scopes);

                //if (loginResult.Status == LiveConnectSessionStatus.Connected)
                if (result.Status == LiveConnectSessionStatus.Connected)
                {
                    
                    //_liveSession = loginResult.Session;
                    _liveSession = result.Session;
                    var meResult = await GetUserInfo();
                    var session = new AuthenticationSession
                    {
                        AccessToken = result.Session.AccessToken,
                        //AccessToken = loginResult.Session.AccessToken,
                        Provider = Constants.MicrosoftProvider,
                        UserInfo = meResult["name"].ToString()
                    };
                    return session;
                }

            }

            catch (LiveAuthException authExp)
            {
                System.Diagnostics.Debug.WriteLine("LiveAuthException = " + authExp.ToString());
            }

            catch (LiveConnectException connExp)
            {
                System.Diagnostics.Debug.WriteLine("LiveConnectException = " + connExp.ToString());
            }

            catch (Exception ex)
            {
                rException = ex;
                System.Diagnostics.Debug.WriteLine("rException = " + rException.ToString());
            }
            await _logManager.LogAsync(rException);

            return null;
        }