AuthenticateUserAsync() private method

private AuthenticateUserAsync ( [ request ) : UserAuthenticationOperation
request [
return UserAuthenticationOperation
Esempio n. 1
0
        /// <summary>
        /// Create connection with scope Basic, SkyDrive.
        /// </summary>
        /// <returns>Return true if connect successful. Otherwise, return false.</returns>
        public async Task<bool> CreateConnectionAsync()
        {
            if (AccessToken != null)
            {
                return true;
            }

            try
            {
                var targetArray = new List<OnlineIdServiceTicketRequest>
                                      {
                                          new OnlineIdServiceTicketRequest("wl.basic wl.signin wl.contacts_photos",
                                                                           "DELEGATION")
                                      };

                _authenticator = new OnlineIdAuthenticator();

                var promptType = (ApplicationData.Current.LocalSettings.Values["UserFirstName"] + string.Empty != string.Empty) ? CredentialPromptType.PromptIfNeeded : CredentialPromptType.RetypeCredentials;


                var result = await _authenticator.AuthenticateUserAsync(targetArray, promptType);

                if (result.Tickets[0].Value != string.Empty)
                {
                    AccessToken = result.Tickets[0].Value;
                    return true;
                }
                else
                {
                    // errors are to be handled here.
                    return false;
                }
                //var loginResult = await _auth.LoginAsync(new[] { "wl.basic, wl.logout" });

                //if (loginResult.Status == LiveConnectSessionStatus.Connected)
                //{
                //    _liveConnectClient = new LiveConnectClient(loginResult.Session);
                //    eSECID = ApplicationData.Current.RoamingSettings.Values[ESEC_ID] + string.Empty;

                //    return true;
                //}

                //return false;
            }
            catch
            {
                return false;
            }
        }
        public async Task<AuthResult> AuthAsync(string startUrl, string endUrlPrefix)
        {
            AuthResult result = new AuthResult(WebAuthenticationStatus.UserCancel);
            Uri start = null;

            OnlineIdServiceTicketRequest[] tickets = new OnlineIdServiceTicketRequest[]
            {
                new OnlineIdServiceTicketRequest(
                    new Uri(startUrl).Host,
                    String.IsNullOrEmpty(LiveIdAuthPolicy)
                        ? ServiceDefinition.DefaultLiveIdAuthPolicy
                        : LiveIdAuthPolicy)
            };

            try
            {
                var onlineIdAuthenticator = new OnlineIdAuthenticator();
                UserIdentity useridentity =
                    await onlineIdAuthenticator.AuthenticateUserAsync(tickets, CredentialPromptType.PromptIfNeeded);

                if (useridentity != null && useridentity.Tickets != null && useridentity.Tickets.Count > 0)
                {
                    OnlineIdServiceTicket ticket = useridentity.Tickets.First();
                    start = new Uri(startUrl + WebUtility.UrlEncode("&" + ticket.Value) + "&mobile=true");
                }
            }
            catch (TaskCanceledException)
            {
                result.Status = WebAuthenticationStatus.UserCancel;
            }
            catch
            {
                start = new Uri(startUrl + "&mobile=true");
            }
            
            if (start != null)
            {
                WebAuthenticationResult webAuthResult = (await WebAuthenticationBroker.AuthenticateAsync(WebAuthenticationOptions.None, start, new Uri(endUrlPrefix)));
                result = new AuthResult(webAuthResult);
            }

            return result;
        }
Esempio n. 3
0
        private static async Task<MobileServiceUser> DoLoginAsync(CredentialPromptType promptType)
        {
            MobileServiceUser user = null;
            var authenticator = new OnlineIdAuthenticator();
            var mobileServicesTicket = new OnlineIdServiceTicketRequest("wl.signin", "DELEGATION");

            var ticketRequests = new List<OnlineIdServiceTicketRequest> {mobileServicesTicket};

            var authResult = await authenticator.AuthenticateUserAsync(ticketRequests, promptType);

            if ((authResult.Tickets.Count == 1) && (authResult.Tickets[0].ErrorCode == 0))
            {
                var accessToken = authResult.Tickets[0];

                var payload = new JObject
                {
                    ["access_token"] = accessToken.Value
                };

                user = await MobileService.Client.LoginAsync(MobileServiceAuthenticationProvider.MicrosoftAccount, payload);

                LegacyUserId.Value = authResult.SafeCustomerId;
            }

            return user;
        }