예제 #1
0
        private async void SignUpButton_Click(object sender, RoutedEventArgs e)
        {
            ClearErrors();

            RegisterExternalUser registerExternalUser = new RegisterExternalUser()
            {
                UserName = this.UsernameTextBox.Text,
            };

            HttpResult result;

            using (AccountClient accountClient = ClientFactory.CreateAccountClient())
            {
                result = await accountClient.RegisterExternalAsync(registerExternalUser);
            }

            if (result.Succeeded)
            {
                // Need to login again now that we are registered - should happen with the existing cookie
                ExternalLoginResult externalLoginResult = await ExternalLoginManager.GetExternalAccessTokenAsync(externalLoginUri);

                bool completed = AccessTokenProvider.AccessTokenSource.TrySetResult(externalLoginResult.AccessToken);
                this.NavigationService.Navigate(new Uri("/TodoPage.xaml", UriKind.Relative));
            }
            else
            {
                DisplayErrors(result.Errors);
            }
        }
        private async void AddAccountButton_Click(object sender, RoutedEventArgs e)
        {
            Account account = (Account)((FrameworkElement)sender).DataContext;

            if (account.Provider == localProvider)
            {
                this.NavigationService.Navigate(new Uri("/SetPasswordPage.xaml", UriKind.Relative));
            }
            else
            {
                ExternalLoginResult loginExternalResult = await ExternalLoginManager.GetExternalAccessTokenAsync(account.ProviderUri);

                string accessToken = loginExternalResult.AccessToken;
                if (accessToken != null)
                {
                    HttpResult result;
                    using (AccountClient accountClient = ClientFactory.CreateAccountClient())
                    {
                        result = await accountClient.AddExternalLoginAsync(new AddExternalLogin()
                        {
                            ExternalAccessToken = accessToken
                        });
                    }

                    if (result.Succeeded)
                    {
                        this.NavigationService.Navigate(new Uri("/SettingsPage.xaml", UriKind.Relative));
                    }
                    else
                    {
                        ErrorDialog.ShowErrors(result.Errors);
                    }
                }
            }
        }
 public static async Task<ExternalLoginResult> GetExternalAccessTokenAsync(string externalLoginUri)
 {
     Uri authorizationRequestUri = new Uri(new Uri(ClientFactory.BaseAddress), externalLoginUri);
    
     Uri endUri = new Uri(authorizationRequestUri, "/");
     WebAuthenticationResult authenticationResult = await WebAuthenticationBroker.AuthenticateAsync(
         WebAuthenticationOptions.None, 
         authorizationRequestUri, 
         endUri);
     ExternalLoginResult loginExternalResult = new ExternalLoginResult() { WebAuthenticationResult = authenticationResult };
     if (authenticationResult.ResponseStatus == WebAuthenticationStatus.Success)
     {
         Uri responseDataUri = new Uri(authenticationResult.ResponseData);
         loginExternalResult.AccessToken = GetAccessTokenFromFragment(responseDataUri);
     }
     return loginExternalResult;
 }
        public static async Task <ExternalLoginResult> GetExternalAccessTokenAsync(string externalLoginUri)
        {
            Uri authorizationRequestUri = new Uri(new Uri(ClientFactory.BaseAddress), externalLoginUri);

            Uri endUri = new Uri(authorizationRequestUri, "/");
            WebAuthenticationResult authenticationResult = await WebAuthenticationBroker.AuthenticateAsync(
                WebAuthenticationOptions.None,
                authorizationRequestUri,
                endUri);

            ExternalLoginResult loginExternalResult = new ExternalLoginResult()
            {
                WebAuthenticationResult = authenticationResult
            };

            if (authenticationResult.ResponseStatus == WebAuthenticationStatus.Success)
            {
                Uri responseDataUri = new Uri(authenticationResult.ResponseData);
                loginExternalResult.AccessToken = GetAccessTokenFromFragment(responseDataUri);
            }
            return(loginExternalResult);
        }
예제 #5
0
        private async void ExternalLoginButton_Click(object sender, RoutedEventArgs e)
        {
            Account             account             = (Account)((FrameworkElement)sender).DataContext;
            ExternalLoginResult loginExternalResult = await ExternalLoginManager.GetExternalAccessTokenAsync(account.ProviderUri);

            if (loginExternalResult.AccessToken != null)
            {
                AppSettings settings = new AppSettings();
                settings.AccessToken = loginExternalResult.AccessToken;

                HttpResult <UserInfo> result;
                using (AccountClient accountClient = ClientFactory.CreateAccountClient())
                {
                    result = await accountClient.GetUserInfoAsync();
                }

                if (result.Succeeded)
                {
                    UserInfo userInfo = result.Content;
                    if (userInfo.HasRegistered)
                    {
                        bool completed = AccessTokenProvider.AccessTokenSource.TrySetResult(loginExternalResult.AccessToken);
                        this.NavigationService.Navigate(new Uri("/TodoPage.xaml", UriKind.Relative));
                    }
                    else
                    {
                        this.NavigationService.Navigate(RegisterExternalPage.GetNavigationUri(userInfo.LoginProvider, userInfo.UserName, account.ProviderUri));
                    }
                }
                else
                {
                    ErrorDialog.ShowErrors("Failed to connect to external account.", result.Errors);
                }
            }
            else
            {
                ErrorDialog.ShowError("Failed to connect to external account.");
            }
        }