Пример #1
0
 private void saveButton_Click(object sender, EventArgs e)
 {
     try
     {
         if (loginTextBox.Text.Length >= 5 &&
             !(String.IsNullOrEmpty(loginTextBox.Text)) &&
             loginTextBox.Text.Trim() != string.Empty &&
             !Regex.IsMatch(loginTextBox.Text, @"[^a-zA-z\d_]"))
         {
             if (!(String.IsNullOrEmpty(FIORichTextBox.Text)) &&
                 FIORichTextBox.Text.Trim() != string.Empty)
             {
                 if (passTextBox.Text.Length >= 8 &&
                     !(String.IsNullOrEmpty(passTextBox.Text)) &&
                     passTextBox.Text.Trim() != string.Empty &&
                     !Regex.IsMatch(passTextBox.Text, @"[^a-zA-z\d_]"))
                 {
                     if (passConfirmTextBox.Text.Length >= 8 &&
                         !(String.IsNullOrEmpty(passConfirmTextBox.Text)) &&
                         passConfirmTextBox.Text.Trim() != string.Empty &&
                         !Regex.IsMatch(passConfirmTextBox.Text, @"[^a-zA-z\d_]"))
                     {
                         if (passTextBox.Text == passConfirmTextBox.Text)
                         {
                             var httpWebRequest = (HttpWebRequest)WebRequest.Create(APP_PATH + "/api/AttractionInfo/AddAdmin");
                             httpWebRequest.ContentType = "application/json";
                             httpWebRequest.Method      = "POST";
                             Admin newAdmin = new Admin();
                             newAdmin.FIO   = FIORichTextBox.Text;
                             newAdmin.login = loginTextBox.Text;
                             byte[] passHash = Encoding.Unicode.GetBytes(passTextBox.Text);
                             newAdmin.passwordEncript = null;
                             byte[] enc = RSA.Encrypt(passHash, false);
                             newAdmin.passwordEncript = (byte[])enc.Clone();
                             AdminInfo adminInfo = new AdminInfo();
                             adminInfo.loginedAdmin = admin;
                             adminInfo.admin        = newAdmin;
                             using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
                             {
                                 string json = JsonConvert.SerializeObject(adminInfo);
                                 streamWriter.Write(json);
                                 streamWriter.Flush();
                                 streamWriter.Close();
                             }
                             var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                             if (httpResponse.StatusCode == HttpStatusCode.Accepted)
                             {
                                 Close();
                             }
                             else if (httpResponse.StatusCode == HttpStatusCode.NoContent)
                             {
                                 MessageBox.Show("Ошибка добавления администратора");
                             }
                             else if (httpResponse.StatusCode == HttpStatusCode.NotAcceptable)
                             {
                                 MessageBox.Show("Администратор с таким параметрами уже существует");
                             }
                         }
                         else
                         {
                             throw new Exception("Пароль и подтверждение пароля не совпадают");
                         }
                     }
                     else
                     {
                         throw new Exception("Не верное значение поля Подтверждение пароля");
                     }
                 }
                 else
                 {
                     throw new Exception("Не верное значение поля Пароль");
                 }
             }
             else
             {
                 throw new Exception("Не верное значение поля ФИО");
             }
         }
         else
         {
             throw new Exception("Не верное значение поля Логин");
         }
     }
     catch (Exception exc)
     {
         MessageBox.Show(exc.Message);
     }
 }
Пример #2
0
 private void button1_Click(object sender, EventArgs e)
 {
     try
     {
         if (loginTextBox.Text.Length >= 5 &&
             !(String.IsNullOrEmpty(loginTextBox.Text)) &&
             loginTextBox.Text.Trim() != string.Empty &&
             !Regex.IsMatch(loginTextBox.Text, @"[^a-zA-z\d_]"))
         {
             if (passwordTextBox.Text.Length >= 5 &&
                 !(String.IsNullOrEmpty(passwordTextBox.Text)) &&
                 passwordTextBox.Text.Trim() != string.Empty &&
                 !Regex.IsMatch(passwordTextBox.Text, @"[^a-zA-z\d_]"))
             {
                 var httpWebRequest = (HttpWebRequest)WebRequest.Create(APP_PATH + "/api/AttractionInfo/GetKey");
                 httpWebRequest.ContentType = "application/json";
                 httpWebRequest.Method      = "POST";
                 Admin admin = new Admin();
                 admin.login = loginTextBox.Text;
                 using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
                 {
                     String json = JsonConvert.SerializeObject(admin);
                     streamWriter.Write(json);
                     streamWriter.Flush();
                     streamWriter.Close();
                 }
                 string result       = "";
                 var    httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                 using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                 {
                     result = streamReader.ReadToEnd();
                     if (result != "null")
                     {
                         string json = JsonConvert.DeserializeObject <string>(result);
                         RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
                         RSA.FromXmlString(json);
                         admin.password = passwordTextBox.Text;
                         if (checkPassword(RSA, admin))
                         {
                             admin.password = null;
                             Form1 form = new Form1(admin, RSA);
                             this.Visible = false;
                             form.ShowDialog();
                         }
                     }
                     else
                     {
                         throw new Exception("Ошибка данных администратора.");
                     }
                 }
             }
             else
             {
                 throw new Exception("Не верное значение поля Пароль");
             }
         }
         else
         {
             throw new Exception("Не верное значение поля Логин");
         }
     }
     catch (Exception exc)
     {
         MessageBox.Show(exc.Message);
     }
 }