private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
        {
            var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://localhost:5000/api/Login");

            httpWebRequest.ContentType = "application/json";
            httpWebRequest.Method      = "POST";

            var regData = new RegData()
            {
                Username = LoginBox.Text, Password = PasswordBox.Password
            };
            var json         = JsonConvert.SerializeObject(regData);
            var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream());

            streamWriter.Write(json);
            streamWriter.Close();

            string result = "";

            try
            {
                var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                var streamReader = new StreamReader(httpResponse.GetResponseStream());
                result = streamReader.ReadToEnd();
            }
            catch (Exception exception)
            {
                WarningText.Text = "Unauthorized";
                return;
            }

            var temp = JsonConvert.DeserializeObject <TokenResponse>(result);


            ConfigManager.Config.Token = temp.Token;

            ConfigManager.Config.RegData = regData;
            WarningText.Text             = "Success!";
            ConfigManager.WriteConfig();
        }
示例#2
0
        /// <summary>
        ///     Обработка нажатия кнопки регистрации
        /// </summary>
        private async void Register_OnClick(object sender, RoutedEventArgs e)
        {
            RegisterButton.IsEnabled = false;
            if (LoginBox.Text.Length >= 20 || LoginBox.Text == "" || LoginBox.Text.Contains(" "))
            {
                WarningText.Text         = "Invalid nickname format";
                RegisterButton.IsEnabled = true;
                return;
            }

            if (await CheckNickUnicall())
            {
                WarningText.Text         = "Nickname is busy";
                RegisterButton.IsEnabled = true;
                return;
            }

            if (PasswordBox1.Password != PasswordBox2.Password)
            {
                WarningText.Text         = "Passwords do not match";
                RegisterButton.IsEnabled = true;
                return;
            }

            var httpWebRequest =
                (HttpWebRequest)WebRequest.Create(
                    $"http://{ConfigManager.Config.IP}:{ConfigManager.Config.Port}/api/Login");

            httpWebRequest.ContentType = "application/json";
            httpWebRequest.Method      = "POST";

            var regData = new RegData {
                Username = LoginBox.Text, Password = PasswordBox1.Password
            };
            var json         = JsonConvert.SerializeObject(regData);
            var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream());

            streamWriter.Write(json);
            streamWriter.Close();

            string result;

            try
            {
                var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                var streamReader = new StreamReader(httpResponse.GetResponseStream());
                result = streamReader.ReadToEnd();
            }
            catch (Exception)
            {
                WarningText.Text         = "Unauthorized";
                RegisterButton.IsEnabled = true;
                return;
            }

            var temp = JsonConvert.DeserializeAnonymousType(result, new Config {
                Token = ""
            });

            ConfigManager.Config.Token = temp.Token;

            ConfigManager.Config.RegData = regData;
            WarningText.Text             = "Success!";
            RegisterButton.IsEnabled     = true;
            ConfigManager.WriteConfig();
            var nvMain = (NavigationView)Frame.FindName("nvMain");

            nvMain.SelectedItem = nvMain.MenuItems.OfType <NavigationViewItem>().Last();
        }
        /// <summary>
        ///     Обработка нажатия кнопки авторизации
        /// </summary>
        private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
        {
            if (LoginBox.Text.Length >= 20 || LoginBox.Text == "" || LoginBox.Text.Contains(" "))
            {
                WarningText.Text = "Подозрительно длинное имя";
                return;
            }

            if (!CheckNickUnicall())
            {
                WarningText.Text = "В базе данных нет такого имени";
                return;
            }

            var httpWebRequest =
                (HttpWebRequest)WebRequest.Create(
                    $"http://{ConfigManager.Config.IP}:{ConfigManager.Config.Port}/api/Login");

            httpWebRequest.ContentType = "application/json";
            httpWebRequest.Method      = "POST";

            var regData = new RegData {
                Username = LoginBox.Text, Password = PasswordBox.Password
            };
            var json         = JsonConvert.SerializeObject(regData);
            var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream());

            streamWriter.Write(json);
            streamWriter.Close();

            string result;

            try
            {
                var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                var streamReader = new StreamReader(httpResponse.GetResponseStream());
                result = streamReader.ReadToEnd();
            }
            catch (Exception)
            {
                WarningText.Text = "Авторизация не прошла";
                return;
            }

            var temp = JsonConvert.DeserializeAnonymousType(result, new Config {
                Token = ""
            });


            ConfigManager.Config.Token = temp.Token;

            ConfigManager.Config.RegData = regData;
            WarningText.Text             = "Подключение установлено!";
            ConfigManager.WriteConfig();
            var nvMain = (NavigationView)Frame.FindName("nvMain");

            if (nvMain != null)
            {
                nvMain.SelectedItem = nvMain.MenuItems.OfType <NavigationViewItem>().Last();
            }
        }
示例#4
0
        /// <summary>
        ///     Обработка нажатия кнопки регистрации
        /// </summary>
        private void Register_OnClick(object sender, RoutedEventArgs e)
        {
            if (LoginBox.Text.Length >= 20 || LoginBox.Text == "" || LoginBox.Text.Contains(" "))
            {
                WarningText.Text = "Нам нужны ваши ДОКУМЕНТЫ.";
                return;
            }

            if (CheckNickUnicall())
            {
                WarningText.Text = "Нам нужны ВАШИ документы.";
                return;
            }

            if (PasswordBox1.Password != PasswordBox2.Password)
            {
                WarningText.Text = "Контрольные суммы кодов не совпали";
                return;
            }

            var httpWebRequest =
                (HttpWebRequest)WebRequest.Create(
                    $"http://{ConfigManager.Config.IP}:{ConfigManager.Config.Port}/api/Login");

            httpWebRequest.ContentType = "application/json";
            httpWebRequest.Method      = "POST";

            var regData = new RegData {
                Username = LoginBox.Text, Password = PasswordBox1.Password
            };
            var json         = JsonConvert.SerializeObject(regData);
            var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream());

            streamWriter.Write(json);
            streamWriter.Close();

            string result;

            try
            {
                var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                var streamReader = new StreamReader(httpResponse.GetResponseStream());
                result = streamReader.ReadToEnd();
            }
            catch (Exception)
            {
                WarningText.Text = "Шпион!";
                return;
            }

            var temp = JsonConvert.DeserializeAnonymousType(result, new Config {
                Token = ""
            });

            ConfigManager.Config.Token = temp.Token;

            ConfigManager.Config.RegData = regData;
            WarningText.Text             = "Соединение успешно.";
            ConfigManager.WriteConfig();
            var nvMain = (NavigationView)Frame.FindName("nvMain");

            nvMain.SelectedItem = nvMain.MenuItems.OfType <NavigationViewItem>().Last();
        }