using System; using System.Net; using System.Windows; using Juick.Classes; using Juick.ViewModels.Validation; using RestSharp; namespace Juick.ViewModels { public class LoginViewModel : DataViewModelBase { 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)Application.Current).NavigateTo(NextUri, true); } else { AddError("Username", "Invalid username or password", false); } }); } } }