private async void DoAuthFlow(LoginOptions loginOptions)
        {
            loginOptions.DisplayType = LoginOptions.DefaultStoreDisplayType;
            var loginUri    = new Uri(OAuth2.ComputeAuthorizationUrl(loginOptions));
            var callbackUri = new Uri(loginOptions.CallbackUrl);
            await SDKServiceLocator.Get <IAuthHelper>().ClearCookiesAsync(loginOptions);

            WebAuthenticationResult webAuthenticationResult = null;
            var hasWebAuthErrors = false;

            try
            {
                LoggingService.Log("Launching web authentication broker", LoggingLevel.Verbose);

                webAuthenticationResult =
                    await
                    WebAuthenticationBroker.AuthenticateAsync(WebAuthenticationOptions.None, loginUri, callbackUri);
            }
            // If a bad URI was passed in the user is shown an error message by the WebAuthenticationBroken, when user
            // taps back arrow we are then thrown a FileNotFoundException, but since user already saw error message we
            // should just swallow that exception
            catch (FileNotFoundException)
            {
                SetupAccountPage();
                return;
            }
            catch (Exception ex)
            {
                LoggingService.Log("Exception occurred during login flow", LoggingLevel.Critical);
                LoggingService.Log(ex, LoggingLevel.Critical);

                hasWebAuthErrors = true;
            }

            if (hasWebAuthErrors)
            {
                await DisplayErrorDialogAsync(LocalizedStrings.GetString("generic_error"));

                SetupAccountPage();
                return;
            }

            if (webAuthenticationResult.ResponseStatus == WebAuthenticationStatus.Success)
            {
                var responseUri = new Uri(webAuthenticationResult.ResponseData);
                if (!String.IsNullOrWhiteSpace(responseUri.Query) &&
                    responseUri.Query.IndexOf("error", StringComparison.CurrentCultureIgnoreCase) >= 0)
                {
                    await DisplayErrorDialogAsync(LocalizedStrings.GetString("generic_authentication_error"));

                    SetupAccountPage();
                }
                else
                {
                    AuthResponse authResponse = OAuth2.ParseFragment(responseUri.Fragment.Substring(1));

                    await SDKServiceLocator.Get <IAuthHelper>().OnLoginCompleteAsync(loginOptions, authResponse);
                }
            }
            else if (webAuthenticationResult.ResponseStatus == WebAuthenticationStatus.UserCancel)
            {
                SetupAccountPage();
            }
            else
            {
                await DisplayErrorDialogAsync(LocalizedStrings.GetString("generic_error"));

                SetupAccountPage();
            }
        }
 private HybridAccountManager()
 {
     RegisterServices();
     SDKServiceLocator.Get <IApplicationInformationService>().GenerateUserAgentHeaderAsync(true, String.Empty);
 }
Beispiel #3
0
        /// <summary>
        ///     Executes the HttpCall. This will generate the headers, create the request and populate the HttpCall properties with
        ///     relevant data.
        ///     The HttpCall may only be called once; further attempts to execute the same call will throw an
        ///     InvalidOperationException.
        /// </summary>
        /// <returns>HttpCall with populated data</returns>
        public async Task <HttpCall> ExecuteAsync()
        {
            if (Executed)
            {
                throw new InvalidOperationException("A HttpCall can only be executed once");
            }
            var req = new HttpRequestMessage(_method, new Uri(_url));

            // Setting header
            if (_headers != null)
            {
                if (_headers.Authorization != null)
                {
                    req.Headers.Authorization = _headers.Authorization;
                }
                foreach (var item in _headers.Headers)
                {
                    req.Headers.Add(item.Key, item.Value);
                }
            }
            // if the user agent has not yet been set, set it; we want to make sure this only really happens once since it requires an action that goes to the core thread.
            if (String.IsNullOrWhiteSpace(UserAgentHeader))
            {
                UserAgentHeader = await SDKServiceLocator.Get <IApplicationInformationService>().GenerateUserAgentHeaderAsync(false, String.Empty);
            }
            req.Headers.UserAgent.TryParseAdd(UserAgentHeader);
            if (!String.IsNullOrWhiteSpace(_requestBody))
            {
                switch (_contentType)
                {
                case ContentTypeValues.FormUrlEncoded:
                    req.Content = new FormUrlEncodedContent(_requestBody.ParseQueryString());
                    break;

                default:
                    req.Content = new StringContent(_requestBody);
                    req.Content.Headers.ContentType = new MediaTypeHeaderValue(_contentType.MimeType());
                    break;
                }
            }
            HttpResponseMessage message;

            try
            {
                message = await _httpClient.SendAsync(req);
            }
            catch (HttpRequestException ex)
            {
                _httpCallErrorException =
                    new DeviceOfflineException("Request failed to send, most likely because we were offline", ex);
                return(this);
            }
            catch (WebException ex)
            {
                _httpCallErrorException =
                    new DeviceOfflineException("Request failed to send, most likely because we were offline", ex);
                return(this);
            }

            await HandleMessageResponseAsync(message);

            return(this);
        }