public LoginViewModel() { LoginClickCommand = new Command(arg => LoginClick()); RegisterClickCommand = new Command(arg => RegisterClick()); TestClickCommand = new Command(arg => TestClick()); User = new User(); }
private async void SendButton_Click(object sender, RoutedEventArgs e) { if (string.IsNullOrEmpty(loginTextBox.Text) || string.IsNullOrEmpty(pwTextBox.Text)) { MessageBox.Show("Fill all fields"); return; } string login = loginTextBox.Text; string password = pwTextBox.Text; User user = new User { Username = login, PasswordHash = GetMD5(password) }; using (HttpClient client = new HttpClient()) { string uri = $"{_apiUri}/api/User/register"; HttpContent content = new StringContent(JsonConvert.SerializeObject(user), Encoding.UTF8, "application/json"); var response = await client.PostAsync(uri, content); if (response.IsSuccessStatusCode) { MessageBox.Show($"User {user.Username} Registered Successfully", "Registered"); } else if (response.StatusCode == HttpStatusCode.Conflict) { MessageBox.Show("User with this username already created or connection failed!"); } else { MessageBox.Show("Some error occured!"); } } }
private async void TestClick() { using (HttpClient client = new HttpClient()) { var user = new User() { Username = "******", PasswordHash = "hash2hash", Token = "890" }; var response = client.PostAsync("http://localhost:13790/api/User/PostTest", new StringContent(JsonConvert.SerializeObject(user), Encoding.UTF8, "application/json")).Result; if (response.IsSuccessStatusCode) { string res = await response.Content.ReadAsStringAsync(); User us = JsonConvert.DeserializeObject<User>(res); MessageBox.Show(us.Username); } } }