using System; using System.Net; using System.Windows; using Juick.Classes; using RestSharp; using RestSharp.Authenticators; namespace Juick.ViewModels { public class LoginViewModel : ViewModelBase { private string _username; private string _password; private DelegateCommand _signInCommand; public string Username { get { return _username ?? (_username = App.AppContext.Account.UserName); } set { _username = value; NotifyPropertyChanged("Username"); } } public string Password { get { return _password ?? (_password = App.AppContext.Account.Password); } set { _password = value; NotifyPropertyChanged("Password"); } } public Uri NextUri { get; set; } public DelegateCommand SignInCommand { get { return _signInCommand ?? (_signInCommand = new DelegateCommand(CheckAuth, () => true)); } } public void CheckAuth(RoutedEventArgs e) { App.AppContext.Client.Authenticator = new HttpBasicAuthenticator(Username, Password); App.AppContext.Client.ExecuteAsync(new RestRequest("/post", Method.POST), response => { if (response.StatusCode == HttpStatusCode.BadRequest) { App.AppContext.Account.UserName = Username; App.AppContext.Account.Password = Password; App.AppContext.Account.IsAuthenticated = true; App.AppContext.EnableNotifications(); ((App)Application.Current).NavigateTo(NextUri); } else if (response.StatusCode == HttpStatusCode.Forbidden) { MessageBox.Show( "Invalid username or password", "Error", MessageBoxButton.OK); } else { MessageBox.Show( response.StatusDescription, "Error", MessageBoxButton.OK); } }); } } }