Example #1
0
 private void InitComponentMessage(object first, object second, int status)
 {
     this.m1         = SerializeAndDeserialize.Serialize(first);
     this.m2         = SerializeAndDeserialize.Serialize(second);
     cm.First        = m1;
     cm.Second       = m2;
     cm.NumberStatus = status;
     m         = SerializeAndDeserialize.Serialize(cm);
     this.data = m.Data;
 }
Example #2
0
        private void EnterButtton_Click(object sender, EventArgs e)
        {
            this.m1              = SerializeAndDeserialize.Serialize(textBox1.Text);
            this.m2              = SerializeAndDeserialize.Serialize(textBox2.Text);
            this.cm.First        = this.m1;
            this.cm.Second       = this.m2;
            this.cm.NumberStatus = 1;
            this.m = SerializeAndDeserialize.Serialize(this.cm);
            data   = this.m.Data;
            stream.Write(data, 0, data.Length);
            if (stream.CanRead)
            {
                int    numberOfBytesRead = 0;
                byte[] readingData       = new byte[6297630];
                do
                {
                    numberOfBytesRead = stream.Read(readingData, 0, readingData.Length);
                } while (stream.DataAvailable);
                this.m.Data = readingData;
                this.cm     = (ComplexMessage)SerializeAndDeserialize.Deserialize(m);

                // Успешная авторизация
                if (cm.NumberStatus == 2)
                {
                    ComplexMessage complexMessage = (ComplexMessage)SerializeAndDeserialize.Deserialize(m);
                    User           user1          = (User)SerializeAndDeserialize.Deserialize(complexMessage.First);
                    Bonus          bonus          = (Bonus)SerializeAndDeserialize.Deserialize(complexMessage.Second);
                    if (user1.Role == "Admin")
                    {
                        AdminForm adminForm = new AdminForm();
                        adminForm.form1       = this;
                        adminForm.label1.Text = user1.Login;
                        adminForm.Show();
                    }
                    else
                    {
                        Form4 userForm = new Form4();
                        userForm.labelLogin.Text = user1.Login;
                        userForm.currentUser     = user1;
                        userForm.Show();
                        userForm.form1 = this;
                    }
                    // Здесь осуществляется вход в новую прогу (для 6 лабы)

                    this.Hide();
                }

                // Ошибка авторизации
                else if (cm.NumberStatus == 3)
                {
                    MessageBox.Show("Неверный логин или пароль");
                }
            }
        }
Example #3
0
        private void changePasButton_Click(object sender, EventArgs e)
        {
            this.m1              = SerializeAndDeserialize.Serialize(newPasTextBox.Text);
            this.m2              = SerializeAndDeserialize.Serialize(textBoxEmail.Text);
            this.cm.First        = this.m1;
            this.cm.Second       = this.m2;
            this.cm.NumberStatus = 2;
            this.m = SerializeAndDeserialize.Serialize(this.cm);
            data   = this.m.Data;
            stream.Write(data, 0, data.Length);
            if (stream.CanRead)
            {
                int    numberOfBytesRead = 0;
                byte[] readingData       = new byte[6297630];
                do
                {
                    numberOfBytesRead = stream.Read(readingData, 0, readingData.Length);
                } while (stream.DataAvailable);
                this.m.Data = readingData;
                this.cm     = (ComplexMessage)SerializeAndDeserialize.Deserialize(m);

                // Успешная смена пароля
                if (cm.NumberStatus == 2)
                {
                    MessageBox.Show("Пароль успешно изменён");
                    form1.Show();
                    this.Hide();
                }

                // Ошибка смены пароля
                else if (cm.NumberStatus == 3)
                {
                    MessageBox.Show("Произошла ошибка при смене пароля");
                }
            }
        }
Example #4
0
        public void Process()
        {
            while (true)
            {
                if (Stream.CanRead)
                {
                    byte[] myReadBuffer = new byte[6297630];
                    do
                    {
                        Stream.Read(myReadBuffer, 0, myReadBuffer.Length);
                    }while (Stream.DataAvailable);
                    Bonus bonus;
                    User  user;
                    MyLib.ComplexMessage complexMessage = new ComplexMessage();
                    MyLib.Message        message        = new MyLib.Message();
                    message.Data   = myReadBuffer;
                    complexMessage = (ComplexMessage)SerializeAndDeserialize.Deserialize(message);
                    // Регистрация
                    if (complexMessage.NumberStatus == 0)
                    {
                        try
                        {
                            bonus = (Bonus)SerializeAndDeserialize.Deserialize(complexMessage.First);
                        }
                        catch
                        {
                            bonus = null;
                        }

                        using (UserContainer db = new UserContainer())
                        {
                            user = (User)SerializeAndDeserialize.Deserialize(complexMessage.Second);
                            Bonus newBonus = new Bonus()
                            {
                                AmountBonus = bonus.AmountBonus
                            };
                            User newUser = new User()
                            {
                                Login = user.Login, Email = user.Email, Balance = user.Balance, Password = user.Password, Role = user.Role, Bonus = newBonus
                            };
                            db.UserSet.Add(newUser);
                            db.SaveChanges();
                        }
                    }
                    // Авторизация
                    else if (complexMessage.NumberStatus == 1)
                    {
                        using (UserContainer db = new UserContainer())
                        {
                            byte[] responseData;
                            for (int i = 0; i < db.UserSet.ToList().Count; i++)
                            {
                                if (db.UserSet.ToList()[i].Login == Convert.ToString(SerializeAndDeserialize.Deserialize(complexMessage.First)) && db.UserSet.ToList()[i].Password == CryptoService.GetHashString(Convert.ToString(SerializeAndDeserialize.Deserialize(complexMessage.Second))))
                                {
                                    Bonus bonus1 = db.UserSet.ToList()[i].Bonus;
                                    Bonus bonus2 = new Bonus()
                                    {
                                        Id          = bonus1.Id,
                                        AmountBonus = bonus1.AmountBonus
                                    };
                                    User user1 = db.UserSet.ToList()[i];
                                    User user2 = new User()
                                    {
                                        Id = user1.Id, Login = user1.Login, Password = user1.Password, Role = user1.Role, Balance = user1.Balance, basket = user1.basket, Email = user1.Email, Bonus = bonus2
                                    };
                                    m1 = SerializeAndDeserialize.Serialize(user2);

                                    m2 = SerializeAndDeserialize.Serialize(bonus2);

                                    cm.First        = m1;
                                    cm.Second       = m2;
                                    cm.NumberStatus = 2;
                                    m            = SerializeAndDeserialize.Serialize(cm);
                                    responseData = m.Data;
                                    Stream.Write(responseData, 0, responseData.Length);
                                    goto label;
                                }
                            }
                            cm.NumberStatus = 3;
                            m = SerializeAndDeserialize.Serialize(cm);

                            responseData = m.Data;
                            Stream.Write(responseData, 0, responseData.Length);
label:
                            responseData = null;
                        }
                    }
                    // Восстановление пароля
                    else if (complexMessage.NumberStatus == 2)
                    {
                        // First - новый пароль, Second - почта
                        string newPassword = Convert.ToString(SerializeAndDeserialize.Deserialize(complexMessage.First));
                        using (UserContainer db = new UserContainer())
                        {
                            byte[] responseData;
                            foreach (User usr in db.UserSet)
                            {
                                if (usr.Email == Convert.ToString(SerializeAndDeserialize.Deserialize(complexMessage.Second)))
                                {
                                    usr.Password    = CryptoService.GetHashString(newPassword);
                                    cm.First        = m1;
                                    cm.Second       = m2;
                                    cm.NumberStatus = 2;
                                    m            = SerializeAndDeserialize.Serialize(cm);
                                    responseData = m.Data;
                                    Stream.Write(responseData, 0, responseData.Length);
                                    goto labelSucess;
                                }
                            }
                            cm.NumberStatus = 3;
                            m = SerializeAndDeserialize.Serialize(cm);

                            responseData = m.Data;
                            Stream.Write(responseData, 0, responseData.Length);
labelSucess:
                            responseData = null;
                            db.SaveChanges();
                        }
                    }
                }
            }
        }