示例#1
0
        public async Task <string> MakeRequest(string path, string query = "")
        {
            for (var count = 0; count < RequestRetries; count++)
            {
                using (var http = new HttpClient())
                {
                    var token = await _tokenGenerator.GetHealthAuthenticationToken();

                    http.DefaultRequestHeaders.Authorization = new HttpCredentialsHeaderValue("Bearer", token);

                    var ub = new UriBuilder(HealthApiUri)
                    {
                        Path  = ApiVersion + "/" + path,
                        Query = query
                    };

                    using (var resp = await http.GetAsync(ub.Uri).AsTask().ConfigureAwait(false))
                    {
                        if (resp.StatusCode == HttpStatusCode.Unauthorized)
                        {
                            if (count == 1)
                            {
                                _tokenGenerator.ClearVault();
                            }
                            await _tokenGenerator.RefreshTokenInVault();

                            continue;
                        }

                        if (resp.IsSuccessStatusCode)
                        {
                            var resStr = await resp.Content.ReadAsStringAsync().AsTask().ConfigureAwait(false);

                            return(resStr);
                        }

                        throw new HealthRequestException($"Unsuccessful request - {resp.ReasonPhrase}\nContent: {resp.Content}");
                    }
                }
            }

            throw new UnauthorizedRequestExeption("Failed sending request 3 times");
        }
示例#2
0
        public async void log_in_button_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var _tokenGenerator = new HealthTokenGenerator();
                await _tokenGenerator.GetHealthAuthenticationToken();

                var userType = _signInFlow.SignIn();

                if (userType == UserType.CoachAndPlayer)
                {
                    var messageDialog = new MessageDialog("Hey! We noticed you are both a coach and a player. As what would you like to log in as?");
                    messageDialog.Commands.Add(new UICommand("Coach")
                    {
                        Id = 0
                    });
                    messageDialog.Commands.Add(new UICommand("Player")
                    {
                        Id = 1
                    });

                    var result = await messageDialog.ShowAsync();

                    if ((int)result.Id == 0)
                    {
                        userType = UserType.Coach;
                    }
                    else
                    {
                        userType = UserType.Player;
                    }
                    ((SignInFlow)_signInFlow).SaveUserToLocalStorage(userType);
                }
                switch (userType)
                {
                case UserType.None:

                    this.Frame.Navigate(typeof(New_User));
                    break;

                case UserType.Player:

                    Player player = _signInFlow.GetPlayer();
                    if (player.FirstName == null)                             //The coach doesn't have a health account
                    {
                        var messageDialog = new MessageDialog("Hi new player, we noticed you don't have a microsoft health account. Please go to microsoft's health app and create one.");
                        messageDialog.Commands.Add(new UICommand("OK")
                        {
                            Id = 0
                        });
                        var result = await messageDialog.ShowAsync();

                        _signInFlow.SignOut();
                        return;
                    }
                    this.Frame.Navigate(typeof(Game_Hub));
                    break;

                case UserType.Coach:

                    Coach coach = _signInFlow.GetCoach();
                    if (coach.FirstName == null)                            //The coach doesn't have a health account
                    {
                        var messageDialog = new MessageDialog("Hi new coach, we noticed you don't have a microsoft health account. Please go to microsoft's health app and create one.");
                        messageDialog.Commands.Add(new UICommand("OK")
                        {
                            Id = 0
                        });
                        var result = await messageDialog.ShowAsync();

                        _signInFlow.SignOut();
                        return;
                    }

                    else if (coach.PaymentLastDigits == "")                             //the coach hasn't completed his sign up
                    {
                        var messageDialog = new MessageDialog("Hey! We noticed you haven't completed your Sign In, so we will now continue!");
                        messageDialog.Commands.Add(new UICommand("OK")
                        {
                            Id = 0
                        });
                        var result = await messageDialog.ShowAsync();

                        this.Frame.Navigate(typeof(New_Coach_CreditCard));
                    }
                    else                             //the coach is OK.
                    {
                        this.Frame.Navigate(typeof(Coach_Lobby));
                    }
                    break;

                case UserType.UnassignedPlayer:

                    this.Frame.Navigate(typeof(New_Player));
                    break;
                }
            }
            catch (UserDeniedAccessExeption)
            {
                var messageDialog = new MessageDialog("An error occured:  User denied access. Please try again.");
                messageDialog.Commands.Add(new UICommand("OK")
                {
                    Id = 0
                });
                var result = await messageDialog.ShowAsync();

                _signInFlow.SignOut();
                return;
            }
            catch (UserCancelSignInExeption)
            {
                var messageDialog = new MessageDialog("An error occured:  User canceled Sign In. Please try again.");
                messageDialog.Commands.Add(new UICommand("OK")
                {
                    Id = 0
                });
                var result = await messageDialog.ShowAsync();

                _signInFlow.SignOut();
                return;
            }
            catch (TokenAcquiringException)
            {
                var messageDialog = new MessageDialog("An error occured:  Token acquiring failed. Please try again.");
                messageDialog.Commands.Add(new UICommand("OK")
                {
                    Id = 0
                });
                var result = await messageDialog.ShowAsync();

                _signInFlow.SignOut();
                return;
            }
            catch (Exception exc)
            {
                var messageDialog = new MessageDialog("An unknown error occured. Please try again.");
                messageDialog.Commands.Add(new UICommand("OK")
                {
                    Id = 0
                });
                var result = await messageDialog.ShowAsync();

                _signInFlow.SignOut();
                return;
            }
        }