private async void ChangePasswordButton_Click(object sender, RoutedEventArgs e)
        {
            ResetDisplay();
            ChangePassword changePassword = new ChangePassword()
            {
                OldPassword = OldPasswordBox.Password,
                NewPassword = NewPasswordBox.Password,
                ConfirmPassword = ConfirmPasswordBox.Password
            };

            HttpResult result;
            using (AccountClient accountClient = ClientFactory.CreateAccountClient())
            {
                result = await accountClient.ChangePasswordAsync(changePassword);
            }

            if (result.Succeeded)
            {
                AppSettings settings = new AppSettings();
                settings.ChangePasswordCredential(username, changePassword.NewPassword);
                DisplaySuccess(); 
            }
            else
            {
                DisplayErrors(result.Errors);
            }
            ClearPasswords();
        }
        private async void SetPasswordButton_Click(object sender, RoutedEventArgs e)
        {
            ResetDisplay();
            SetPassword setPassword = new SetPassword()
            {
                NewPassword = NewPasswordBox.Password,
                ConfirmPassword = ConfirmPasswordBox.Password
            };

            HttpResult result;
            using (AccountClient accountClient = ClientFactory.CreateAccountClient())
            {
                result = await accountClient.SetPasswordAsync(setPassword);
            }

            if (result.Succeeded)
            {
                AppSettings settings = new AppSettings();
                settings.SavePasswordCredential(this.username, setPassword.NewPassword);
                AccountsSettingsPane.Show();
            }
            else
            {
                DisplayErrors(result.Errors);
            }
            ClearPasswords();

        }
 public static HttpClient CreateHttpClient()
 {
     AppSettings settings = new AppSettings();
     AccessTokenProvider loginProvider = new AccessTokenProvider();
     OAuth2BearerTokenHandler oauth2Handler = new OAuth2BearerTokenHandler(settings, loginProvider);
     HttpClient httpClient = HttpClientFactory.Create(oauth2Handler);
     httpClient.BaseAddress = new Uri(BaseAddress);
     httpClient.Timeout = TimeSpan.FromDays(1);
     return httpClient;
 }
        private async void LoginButton_Click(object sender, RoutedEventArgs e)
        {
            ClearErrors();

            string username = UsernameTextBox.Text;
            string password = PasswordBox.Password;
            HttpResult<AccessTokenResponse> result;
            using (AccountClient accountClient = ClientFactory.CreateAccountClient())
            {
                result = await accountClient.LoginAsync(username, password);
            }
            if (result.Succeeded)
            {
                AppSettings settings = new AppSettings();
                settings.SavePasswordCredential(username, password);
                bool completed = accessTokenSource.TrySetResult(result.Content.AccessToken);
                this.Frame.GoBack();
            }
            else
            {
                DisplayErrors(result.Errors);
            }
        }
        private async void WebAccountProviderInvokedHandler(WebAccountProviderCommand command)
        {
            string externalLoginUri = command.WebAccountProvider.Id;
            ExternalLoginResult loginExternalResult = await ExternalLoginManager.GetExternalAccessTokenAsync(externalLoginUri);
            if (loginExternalResult.AccessToken != null)
            {
                // Save the access token so we can check if the user has registered
                AppSettings settings = new AppSettings();
                settings.AccessToken = loginExternalResult.AccessToken;

                HttpResult<UserInfo> result;
                using (AccountClient accountClient = ClientFactory.CreateAccountClient())
                {
                    result = await accountClient.GetUserInfoAsync();
                }
                if (result.Succeeded)
                {
                    if (result.Content.HasRegistered)
                    {
                        accessTokenSource.TrySetResult(loginExternalResult.AccessToken);
                        this.Frame.GoBack();
                    }
                    else
                    {
                        RegisterExternalPageParameters parameters = new RegisterExternalPageParameters()
                        {
                            AccessTokenSource = accessTokenSource,
                            ExternalLoginUri = externalLoginUri,
                            UserInfo = result.Content
                        };
                        this.Frame.Navigate(typeof(RegisterExternalPage), parameters);
                    }
                }
                else
                {
                    await ErrorDialog.ShowErrorsAsync("Failed to connected to external account.", result.Errors);
                }
            }
            else
            {
                await ErrorDialog.ShowErrorAsync("Failed to connected to external account.");
            }
        }
        private async void SignUpButton_Click(object sender, RoutedEventArgs e)
        {
            ClearErrors();

            // TODO: Add input validation
            string username = UsernameTextBox.Text;
            string password = PasswordBox.Password;
            RegisterUser registerUser = new RegisterUser()
            {
                UserName = username,
                Password = password,
                ConfirmPassword = this.ConfirmPasswordBox.Password
            };

            HttpResult registerResult;
            using (AccountClient accountClient = ClientFactory.CreateAccountClient())
            {
                registerResult = await accountClient.RegisterAsync(registerUser);
                if (!registerResult.Succeeded)
                {
                    DisplayErrors(registerResult.Errors);
                    return;
                }

                HttpResult<AccessTokenResponse> loginResult = await accountClient.LoginAsync(username, password);
                if (loginResult.Succeeded)
                {
                    AppSettings settings = new AppSettings();
                    settings.SavePasswordCredential(username, password);
                    bool resultSet = accessTokenSource.TrySetResult(loginResult.Content.AccessToken);
                    this.Frame.Navigate(typeof(TodoPage));
                }
                else
                {
                    DisplayErrors(loginResult.Errors);
                }
            }
        }