private void Authentification()
        {
            //try
            //{
            string username = Textbox.Text;
            string password = Passwordbox.Password;

            Credentials loginModel = new Credentials()
            {
                Username = username, Password = password
            };

            if (Validtion.TryValidateObject(loginModel, Textbox, Passwordbox))
            {
                string hash = Hash(Passwordbox.Password);
                using (My_KPEntities db = new My_KPEntities())
                {
                    var user = db.Users.FirstOrDefault(u => u.Credentials.Username == username);
                    if (user != null)
                    {
                        if (user.Credentials.Password == hash)
                        {
                            if (user.RoleId == 2)
                            {
                                //Current.Users = users;
                                Hide();
                                Thread myThread = new Thread(new ThreadStart(ShowLoader));
                                myThread.SetApartmentState(ApartmentState.STA);
                                myThread.Start();
                                Thread.Sleep(2000);
                                myThread.Abort();

                                GeneralWindow gen = new GeneralWindow();
                                gen.Show();
                                gen.UserControl1.Minimized = true;
                                gen.UserControl2.Minimized = true;
                                // gen.UserControl3.Minimized = true;
                                //gen.UserControl4.Minimized = true;
                            }
                            else if (user.RoleId == 1)
                            {
                                Current.Users = user;
                                Hide();
                                Thread myThread = new Thread(new ThreadStart(ShowLoader));
                                myThread.SetApartmentState(ApartmentState.STA);
                                myThread.Start();
                                Thread.Sleep(2000);
                                myThread.Abort();

                                GeneralWindow2 gen2 = new GeneralWindow2();
                                gen2.Show();
                            }
                        }
                        else
                        {
                            MessageBox.Show("Wrong password");
                        }
                    }
                    else
                    {
                        MessageBox.Show("such user not found");
                    }
                }
            }
            //}
            //catch (exception ex)
            //{
            //    messagebox.show(ex.message, "ошибка");
            //}
        }
예제 #2
0
        private void Register()
        {
            try
            {
                string username = TextBox.Text;
                string password = PasswordBox.Password;


                Credentials registerModel = new Credentials()
                {
                    Username = username,
                    Password = password,
                };

                if (Validtion.TryValidateObject(registerModel, TextBox, PasswordBox))

                {
                    string hash = Hash(PasswordBox.Password);
                    //bool isAdmin = (bool)isAdminCheckBox.IsChecked;

                    using (My_KPEntities db = new My_KPEntities())
                    {
                        var sameUser = db.Users.FirstOrDefault(u => u.Credentials.Username == TextBox.Text);
                        if (sameUser == null)
                        {
                            Credentials credentials = new Credentials()
                            {
                                Username = TextBox.Text, Password = Hash(PasswordBox.Password)
                            };
                            Addresses addresses = new Addresses()
                            {
                                City = City.Text, Street = Street.Text, House = House.Text
                            };
                            Users user = db.Users.Add(new Users()
                            {
                                Credentials = credentials, LastName = LastNameBox.Text, FirstName = FirstNameBox.Text, Addresses = addresses, Birthday = BirthdayBox.SelectedDate.Value, RoleId = 1
                            });
                            for (int i = 0; i < WrapPanel.Children.Count; i++)
                            {
                                CheckBox checkBox = (CheckBox)WrapPanel.Children[i];
                                if (checkBox.IsChecked == true)
                                {
                                    db.UserInterests.Add(new UserInterests()
                                    {
                                        UserId = user.Id, InterestId = i + 1
                                    });
                                }
                            }
                            db.SaveChanges();
                        }
                        else
                        {
                            MessageBox.Show("user with the same name already exists");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK);
            }
        }