Interaction logic for StartPage.xaml
Inheritance: System.Windows.Controls.Page
コード例 #1
0
        /// <summary>
        /// Handles the Click event of the btnLogin control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param>
        private void btnLogin_Click( object sender, RoutedEventArgs e )
        {
            txtUsername.Text = txtUsername.Text.Trim();
            txtRockUrl.Text = txtRockUrl.Text.Trim();
            Uri rockUrl = new Uri( txtRockUrl.Text );
            var validSchemes = new string[] { Uri.UriSchemeHttp, Uri.UriSchemeHttps };
            if ( !validSchemes.Contains(rockUrl.Scheme) )
            {
                txtRockUrl.Text = "http://" + rockUrl.AbsoluteUri;
            }

            RockRestClient rockRestClient = new RockRestClient( txtRockUrl.Text );

            string userName = txtUsername.Text;
            string password = txtPassword.Password;

            // start a background thread to Login since this could take a little while and we want a Wait cursor
            BackgroundWorker bw = new BackgroundWorker();
            bw.DoWork += delegate( object s, DoWorkEventArgs ee )
            {
                ee.Result = null;
                rockRestClient.Login( userName, password );
            };

            // when the Background Worker is done with the Login, run this
            bw.RunWorkerCompleted += delegate( object s, RunWorkerCompletedEventArgs ee )
            {
                this.Cursor = null;
                btnLogin.IsEnabled = true;
                try
                {
                    if ( ee.Error != null )
                    {
                        throw ee.Error;
                    }

                    Rock.Client.Person person = rockRestClient.GetData<Rock.Client.Person>( string.Format( "api/People/GetByUserName/{0}", userName ) );
                    RockConfig rockConfig = RockConfig.Load();
                    rockConfig.RockBaseUrl = txtRockUrl.Text;
                    rockConfig.Username = txtUsername.Text;
                    rockConfig.Password = txtPassword.Password;
                    rockConfig.Save();

                    if ( this.NavigationService.CanGoBack )
                    {
                        // if we got here from some other Page, go back
                        this.NavigationService.GoBack();
                    }
                    else
                    {
                        StartPage startPage = new StartPage();
                        this.NavigationService.Navigate( startPage );
                    }
                }
                catch ( WebException wex )
                {
                    // show WebException on the form, but any others should end up in the ExceptionDialog
                    HttpWebResponse response = wex.Response as HttpWebResponse;
                    if ( response != null )
                    {
                        if ( response.StatusCode.Equals( HttpStatusCode.Unauthorized ) )
                        {
                            lblLoginWarning.Content = "Invalid Login";
                            lblLoginWarning.Visibility = Visibility.Visible;
                            return;
                        }
                    }

                    string message = wex.Message;
                    if ( wex.InnerException != null )
                    {
                        message += "\n" + wex.InnerException.Message;
                    }

                    lblRockUrl.Visibility = Visibility.Visible;
                    txtRockUrl.Visibility = Visibility.Visible;
                    lblLoginWarning.Content = message;
                    lblLoginWarning.Visibility = Visibility.Visible;
                    return;
                }
            };

            // set the cursor to Wait, disable the login button, and start the login background process
            this.Cursor = Cursors.Wait;
            btnLogin.IsEnabled = false;
            bw.RunWorkerAsync();
        }
コード例 #2
0
        /// <summary>
        /// Handles the Click event of the btnLogin control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param>
        private void btnLogin_Click(object sender, RoutedEventArgs e)
        {
            lblLoginWarning.Visibility = Visibility.Hidden;
            txtUsername.Text           = txtUsername.Text.Trim();
            txtRockUrl.Text            = txtRockUrl.Text.Trim();
            Uri rockUri      = new Uri(txtRockUrl.Text);
            var validSchemes = new string[] { Uri.UriSchemeHttp, Uri.UriSchemeHttps };

            if (!validSchemes.Contains(rockUri.Scheme))
            {
                txtRockUrl.Text = "https://" + rockUri.AbsoluteUri;
            }

            RestClient restClient = new RestClient(txtRockUrl.Text);

            string userName = txtUsername.Text;
            string password = txtPassword.Password;
            string rockUrl  = txtRockUrl.Text;

            if (string.IsNullOrWhiteSpace(userName))
            {
                lblLoginWarning.Content    = "Username cannot be blank";
                lblLoginWarning.Visibility = Visibility.Visible;
                return;
            }

            // start a background thread to Login since this could take a little while and we want a Wait cursor
            BackgroundWorker bw = new BackgroundWorker();

            bw.DoWork += delegate(object s, DoWorkEventArgs ee)
            {
                ee.Result = null;
                restClient.LoginToRock(userName, password);
            };

            // when the Background Worker is done with the Login, run this
            bw.RunWorkerCompleted += delegate(object s, RunWorkerCompletedEventArgs ee)
            {
                this.Cursor        = null;
                btnLogin.IsEnabled = true;

                if (ee.Error != null)
                {
                    lblRockUrl.Visibility      = Visibility.Visible;
                    txtRockUrl.Visibility      = Visibility.Visible;
                    lblLoginWarning.Content    = ee.Error.Message;
                    lblLoginWarning.Visibility = Visibility.Visible;
                    return;
                }

                var getByUserNameRequest  = new RestRequest(string.Format("api/People/GetByUserName/{0}", userName));
                var getByUserNameResponse = restClient.Execute <Rock.Client.Person>(getByUserNameRequest);
                if (getByUserNameResponse.ErrorException != null)
                {
                    string message = getByUserNameResponse.ErrorException.Message;
                    if (getByUserNameResponse.ErrorException.InnerException != null)
                    {
                        message += "\n" + getByUserNameResponse.ErrorException.InnerException.Message;
                    }

                    lblRockUrl.Visibility      = Visibility.Visible;
                    txtRockUrl.Visibility      = Visibility.Visible;
                    lblLoginWarning.Content    = message;
                    lblLoginWarning.Visibility = Visibility.Visible;
                    return;
                }

                Rock.Client.Person person     = getByUserNameResponse.Data;
                RockConfig         rockConfig = RockConfig.Load();
                rockConfig.RockBaseUrl = rockUrl;
                rockConfig.Username    = userName;
                rockConfig.Password    = password;

                // Load any stored configuration settings
                rockConfig = this.LoadConfigurationFromSystemSetting(rockConfig);
                rockConfig.Save();

                if (this.NavigationService.CanGoBack)
                {
                    // if we got here from some other Page, go back
                    this.NavigationService.GoBack();
                }
                else
                {
                    StartPage startPage = new StartPage();
                    this.NavigationService.Navigate(startPage);
                }
            };

            // set the cursor to Wait, disable the login button, and start the login background process
            this.Cursor        = Cursors.Wait;
            btnLogin.IsEnabled = false;
            bw.RunWorkerAsync();
        }
コード例 #3
0
ファイル: LoginPage.xaml.cs プロジェクト: leandrowd/Rock
        /// <summary>
        /// Handles the Click event of the btnLogin control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param>
        private void btnLogin_Click(object sender, RoutedEventArgs e)
        {
            txtUsername.Text = txtUsername.Text.Trim();
            txtRockUrl.Text  = txtRockUrl.Text.Trim();
            RockRestClient rockRestClient = new RockRestClient(txtRockUrl.Text);

            string userName = txtUsername.Text;
            string password = txtPassword.Password;

            // start a background thread to Login since this could take a little while and we want a Wait cursor
            BackgroundWorker bw = new BackgroundWorker();

            bw.DoWork += delegate(object s, DoWorkEventArgs ee)
            {
                ee.Result = null;
                rockRestClient.Login(userName, password);
            };

            // when the Background Worker is done with the Login, run this
            bw.RunWorkerCompleted += delegate(object s, RunWorkerCompletedEventArgs ee)
            {
                this.Cursor        = null;
                btnLogin.IsEnabled = true;
                try
                {
                    if (ee.Error != null)
                    {
                        throw ee.Error;
                    }

                    Rock.Client.Person person     = rockRestClient.GetData <Rock.Client.Person>(string.Format("api/People/GetByUserName/{0}", userName));
                    RockConfig         rockConfig = RockConfig.Load();
                    rockConfig.RockBaseUrl = txtRockUrl.Text;
                    rockConfig.Username    = txtUsername.Text;
                    rockConfig.Password    = txtPassword.Password;
                    rockConfig.Save();

                    if (this.NavigationService.CanGoBack)
                    {
                        // if we got here from some other Page, go back
                        this.NavigationService.GoBack();
                    }
                    else
                    {
                        StartPage startPage = new StartPage();
                        this.NavigationService.Navigate(startPage);
                    }
                }
                catch (WebException wex)
                {
                    // show WebException on the form, but any others should end up in the ExceptionDialog
                    HttpWebResponse response = wex.Response as HttpWebResponse;
                    if (response != null)
                    {
                        if (response.StatusCode.Equals(HttpStatusCode.Unauthorized))
                        {
                            lblLoginWarning.Content    = "Invalid Login";
                            lblLoginWarning.Visibility = Visibility.Visible;
                            return;
                        }
                    }

                    string message = wex.Message;
                    if (wex.InnerException != null)
                    {
                        message += "\n" + wex.InnerException.Message;
                    }

                    lblRockUrl.Visibility      = Visibility.Visible;
                    txtRockUrl.Visibility      = Visibility.Visible;
                    lblLoginWarning.Content    = message;
                    lblLoginWarning.Visibility = Visibility.Visible;
                    return;
                }
            };

            // set the cursor to Wait, disable the login button, and start the login background process
            this.Cursor        = Cursors.Wait;
            btnLogin.IsEnabled = false;
            bw.RunWorkerAsync();
        }