Exemple #1
0
 public App()
 {
     if (string.IsNullOrEmpty(ZipDoc.Properties.Settings.Default.Token))
     {
         LoginViewModel logVM = new LoginViewModel();
         var loginVM = new LoginView
         {
             DataContext = logVM
         };
         logVM.thisview = loginVM;
         loginVM.Show();
     }
     else
     {
         var mainVM = new MainWindowView
         {
             DataContext = new MainWindowViewModel()
         };
         mainVM.Show();
     }
 }
Exemple #2
0
        private async void LoginClick()
        {
            _password = thisview.passwordBox.Password;
            //_user.PasswordHash = GetMD5(_password);
            using (HttpClient client = new HttpClient()){
                string uri = $"{_apiUri}api/User/login";
                if (string.IsNullOrEmpty(_password)||string.IsNullOrEmpty(_user.Username))
                {
                    MessageBox.Show("Empty Field", "Error");
                    return;
                }

                _user.PasswordHash = GetMD5(_password);
                HttpContent content = new StringContent(JsonConvert.SerializeObject(_user), Encoding.UTF8, "application/json");
                var response = client.PostAsync(uri, content).Result;

                if (response.IsSuccessStatusCode)
                {
                    //MessageBox.Show("All OK");
                    string result = await response.Content.ReadAsStringAsync();
                    User user = JsonConvert.DeserializeObject<User>(result);
                    if (_rememberMe)
                    {
                        Properties.Settings.Default.Token = user.Token;
                        Properties.Settings.Default.Save();
                        var mainVM = new MainWindowView()
                        {
                            DataContext = new MainWindowViewModel(user)
                        };
                        mainVM.Show();
                        Application.Current.MainWindow.Close();
                    }
                    else
                    {
                        MainWindowViewModel mainWindowVM = new MainWindowViewModel(user);
                        //Opening main window
                        var mainVM = new MainWindowView()
                        {
                            DataContext = mainWindowVM
                        };
                        mainWindowVM.thisView = mainVM;
                        mainVM.Show();
                        Application.Current.MainWindow.Close();
                    }
                }
                else
                {
                    MessageBox.Show("Error with authorization!");
                }
            }
        }
Exemple #3
0
        private async void RegisterClick()
        {
            using (HttpClient client = new HttpClient())
            {
                _user.PasswordHash = GetMD5(Password);
                string uri = $"{_apiUri}/api/User/register";
                if (string.IsNullOrEmpty(_password) || string.IsNullOrEmpty(_user.Username))
                {
                    MessageBox.Show("Empty Field", "Error");
                    return;
                }
                
                HttpContent content = new StringContent(JsonConvert.SerializeObject(_user), Encoding.UTF8, "application/json");
                var response = await client.PostAsync(uri, content);

                if (response.IsSuccessStatusCode)
                {
                    string result = await response.Content.ReadAsStringAsync();
                    User user = JsonConvert.DeserializeObject<User>(result);
                    if (_rememberMe)
                    {
                        Properties.Settings.Default.Token = user.Token;
                    }
                    //Opening main window
                    var mainVM = new MainWindowView()
                    {
                        DataContext = new MainWindowViewModel(/*_user*/)};
                    mainVM.Show();

                    Application.Current.MainWindow.Close();
                }
                else if (response.StatusCode == HttpStatusCode.Conflict)
                {
                    MessageBox.Show("User with this username already created or connection failed!");
                }
            }
        }