/// <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 rockUrl = new Uri(txtRockUrl.Text);
var validSchemes = new string[] { Uri.UriSchemeHttp, Uri.UriSchemeHttps };
if (!validSchemes.Contains(rockUrl.Scheme))
{
txtRockUrl.Text = "https://" + rockUrl.AbsoluteUri;
}
RockRestClient rockRestClient = new RockRestClient(txtRockUrl.Text);
string userName = txtUsername.Text;
string password = txtPassword.Password;
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;
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;
}
Person person = rockRestClient.GetData <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();
BatchPage batchPage = new BatchPage(person);
if (this.NavigationService.CanGoBack)
{
// if we got here from some other Page, go back
this.NavigationService.GoBack();
}
else
{
try
{
batchPage.LoadLookups();
batchPage.LoadFinancialBatchesGrid();
}
catch (HttpErrorException ex)
{
if (ex.Response != null && ex.Response.StatusCode.Equals(HttpStatusCode.Unauthorized))
{
lblLoginWarning.Content = "Not Authorized for Financial Batches";
lblLoginWarning.Visibility = Visibility.Visible;
return;
}
else
{
throw ex;
}
}
this.NavigationService.Navigate(batchPage);
}
}
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();
}