private void SocrataLogin(string username, string password) { try { socrataLoginCredentials = new NetworkCredential(username, password); string httpBase = Constant.SocrataLoginHttpBase; string appToken = Constant.SocrataLoginAppToken; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(httpBase + Constant.SocrataUserDetailUrl); //request.PreAuthenticate = true; request.Credentials = socrataLoginCredentials; request.Headers["X-App-Token"] = appToken; request.Headers["Authorization"] = getAuthorization(); string temp = null; int count = 0; byte[] buffer; StringBuilder sb = new StringBuilder(); request.BeginGetResponse(delegate(IAsyncResult responseData) { try { HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(responseData); using (var stream = response.GetResponseStream()) { buffer = new byte[stream.Length]; // Read to the end of the response do { count = stream.Read(buffer, 0, buffer.Length); if (count != 0) { temp = Encoding.UTF8.GetString(buffer, 0, count); sb.Append(temp); } } while (count > 0); } socrataUserDetails = Newtonsoft.Json.JsonConvert.DeserializeObject<SocrataUserDetail>(sb.ToString()); if (socrataUserDetails != null) { this.Dispatcher.BeginInvoke(() => { CommentData commentData = new CommentData(); commentData.Author = socrataUserDetails.displayName; commentData.CityName = this.city; commentData.ReportName = this.dataSetName; commentData.Id = this.compositeKey; commentData.CommentPublishAt = DateTime.Now.ToUniversalTime(); commentData.CommentTitle = ""; commentData.CommentMessage = this.txtComment.Text.Trim(); App.ViewModel.CommentViewModel.AddComment(commentData); App.ViewModel.CommentViewModel.CommentList.Add(commentData); this.tbkNoResult.Visibility = Visibility.Collapsed; IncreaseCommentsCount(); txtComment.Text = ""; this.popUpSocrataLogin.IsOpen = false; this.ApplicationBar.IsVisible = true; App.ViewModel.CommentViewModel.IsDataLoading = false; (this.ApplicationBar.Buttons[0] as ApplicationBarIconButton).IsEnabled = true; (this.ApplicationBar.Buttons[1] as ApplicationBarIconButton).IsEnabled = true; txtComment.IsEnabled = true; IsSocrataLoginRemember = true; socrataUserName = username; socrataPassword = password; }); } else { MessageBox.Show(AppResources.SocrataLoginErrorMessageText, AppResources.LoginErrorMessageTitle, MessageBoxButton.OK); App.ViewModel.CommentViewModel.IsDataLoading = false; (this.ApplicationBar.Buttons[0] as ApplicationBarIconButton).IsEnabled = true; (this.ApplicationBar.Buttons[1] as ApplicationBarIconButton).IsEnabled = true; txtComment.IsEnabled = true; } } catch (Exception) { this.Dispatcher.BeginInvoke(() => { MessageBox.Show(AppResources.SocrataLoginErrorMessageText, AppResources.LoginErrorMessageTitle, MessageBoxButton.OK); App.ViewModel.CommentViewModel.IsDataLoading = false; (this.ApplicationBar.Buttons[0] as ApplicationBarIconButton).IsEnabled = true; (this.ApplicationBar.Buttons[1] as ApplicationBarIconButton).IsEnabled = true; txtComment.IsEnabled = true; }); } }, null); } catch (Exception) { MessageBox.Show(AppResources.SocrataLoginErrorMessageText, AppResources.LoginErrorMessageTitle, MessageBoxButton.OK); App.ViewModel.CommentViewModel.IsDataLoading = false; (this.ApplicationBar.Buttons[0] as ApplicationBarIconButton).IsEnabled = true; (this.ApplicationBar.Buttons[1] as ApplicationBarIconButton).IsEnabled = true; txtComment.IsEnabled = true; } }
private void SocrataLogin(string username, string password) { try { socrataLoginCredentials = new NetworkCredential(username, password); string httpBase = Constant.SocrataLoginHttpBase; string appToken = Constant.SocrataLoginAppToken; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(httpBase + Constant.SocrataUserDetailUrl); //request.PreAuthenticate = true; request.Credentials = socrataLoginCredentials; request.Headers["X-App-Token"] = appToken; request.Headers["Authorization"] = getAuthorization(); string temp = null; int count = 0; byte[] buffer; StringBuilder sb = new StringBuilder(); request.BeginGetResponse(delegate(IAsyncResult responseData) { try { HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(responseData); using (var stream = response.GetResponseStream()) { buffer = new byte[stream.Length]; // Read to the end of the response do { count = stream.Read(buffer, 0, buffer.Length); if (count != 0) { temp = Encoding.UTF8.GetString(buffer, 0, count); sb.Append(temp); } } while (count > 0); } socrataUserDetails = Newtonsoft.Json.JsonConvert.DeserializeObject <SocrataUserDetail>(sb.ToString()); if (socrataUserDetails != null) { this.Dispatcher.BeginInvoke(() => { CommentData commentData = new CommentData(); commentData.Author = socrataUserDetails.displayName; commentData.CityName = this.city; commentData.ReportName = this.dataSetName; commentData.Id = this.compositeKey; commentData.CommentPublishAt = DateTime.Now.ToUniversalTime(); commentData.CommentTitle = ""; commentData.CommentMessage = this.txtComment.Text.Trim(); App.ViewModel.CommentViewModel.AddComment(commentData); App.ViewModel.CommentViewModel.CommentList.Add(commentData); this.tbkNoResult.Visibility = Visibility.Collapsed; IncreaseCommentsCount(); txtComment.Text = ""; this.popUpSocrataLogin.IsOpen = false; this.ApplicationBar.IsVisible = true; App.ViewModel.CommentViewModel.IsDataLoading = false; (this.ApplicationBar.Buttons[0] as ApplicationBarIconButton).IsEnabled = true; (this.ApplicationBar.Buttons[1] as ApplicationBarIconButton).IsEnabled = true; txtComment.IsEnabled = true; IsSocrataLoginRemember = true; socrataUserName = username; socrataPassword = password; }); } else { MessageBox.Show(AppResources.SocrataLoginErrorMessageText, AppResources.LoginErrorMessageTitle, MessageBoxButton.OK); App.ViewModel.CommentViewModel.IsDataLoading = false; (this.ApplicationBar.Buttons[0] as ApplicationBarIconButton).IsEnabled = true; (this.ApplicationBar.Buttons[1] as ApplicationBarIconButton).IsEnabled = true; txtComment.IsEnabled = true; } } catch (Exception) { this.Dispatcher.BeginInvoke(() => { MessageBox.Show(AppResources.SocrataLoginErrorMessageText, AppResources.LoginErrorMessageTitle, MessageBoxButton.OK); App.ViewModel.CommentViewModel.IsDataLoading = false; (this.ApplicationBar.Buttons[0] as ApplicationBarIconButton).IsEnabled = true; (this.ApplicationBar.Buttons[1] as ApplicationBarIconButton).IsEnabled = true; txtComment.IsEnabled = true; }); } }, null); } catch (Exception) { MessageBox.Show(AppResources.SocrataLoginErrorMessageText, AppResources.LoginErrorMessageTitle, MessageBoxButton.OK); App.ViewModel.CommentViewModel.IsDataLoading = false; (this.ApplicationBar.Buttons[0] as ApplicationBarIconButton).IsEnabled = true; (this.ApplicationBar.Buttons[1] as ApplicationBarIconButton).IsEnabled = true; txtComment.IsEnabled = true; } }