Example #1
0
        private void Register()
        {
            try
            {
                string username        = regLoginTextBox.Text;
                string password        = regPasswordBox.Password;
                string confirmPassword = regConfirmPasswordBox.Password;

                RegisterModel registerModel = new RegisterModel()
                {
                    Login           = username,
                    Password        = password,
                    ConfirmPassword = confirmPassword
                };

                if (Validation.TryValidateObject(registerModel, regLoginTextBox, regPasswordBox, regConfirmPasswordBox))
                {
                    SaltedHash saltedHash = new SaltedHash(password);
                    bool       isAdmin    = username == "Admin";

                    using (AppDbContext db = new AppDbContext())
                    {
                        var sameUser = db.Users.FirstOrDefault(u => u.Username == username);
                        if (sameUser == null && !isAdmin)
                        {
                            User user = new User()
                            {
                                Username = username,
                                Salt     = saltedHash.Salt,
                                Hash     = saltedHash.Hash,
                            };
                            db.Users.Add(user);

                            Log log = new Log()
                            {
                                Time = DateTime.Now, Action = "Пользователь " + user.Username + " зарегистрировался"
                            };
                            db.Logs.Add(log);
                            db.SaveChanges();
                            ShowLoginWindow();
                        }
                        else
                        if (sameUser == null && isAdmin)
                        {
                            User user = new User()
                            {
                                Username = username,
                                Salt     = saltedHash.Salt,
                                Hash     = saltedHash.Hash,
                                IsAdmin  = isAdmin
                            };
                            db.Users.Add(user);

                            Log log = new Log()
                            {
                                Time = DateTime.Now, Action = "Аккаунт админа зарегистрирован"
                            };
                            db.Logs.Add(log);
                            db.SaveChanges();
                            ShowLoginWindow();
                        }
                        else
                        {
                            regLoginTextBox.BorderBrush = new SolidColorBrush(Colors.Red);
                            regLoginTextBox.ToolTip     = new ToolTip()
                            {
                                Content = "Это имя пользователя уже занято"
                            };
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MaterialMessageBox.Show(ex.Message, "Ошибка");
            }
        }
Example #2
0
        private void Authentification()
        {
            try
            {
                string username = logLoginTextBox.Text;
                string password = logPasswordBox.Password;

                AuthentificationModel loginModel = new AuthentificationModel()
                {
                    Login = username, Password = password
                };

                if (Validation.TryValidateObject(loginModel, logLoginTextBox, logPasswordBox, null))
                {
                    using (AppDbContext db = new AppDbContext())
                    {
                        var user = db.Users.FirstOrDefault(u => u.Username == username);
                        if (user != null)
                        {
                            if (SaltedHash.Verify(user.Salt, user.Hash, password))
                            {
                                App.CurrentUser = user;

                                if (user.IsAdmin)
                                {
                                    AdminWindow adminWindow = new AdminWindow();
                                    Close();
                                    //------------LOADER---------------
                                    Thread myThread = new Thread(new ThreadStart(ShowLoader));
                                    myThread.SetApartmentState(ApartmentState.STA);
                                    myThread.Start();
                                    Thread.Sleep(1000);
                                    myThread.Abort();
                                    adminWindow.Show();
                                    //---------------------------------
                                }
                                else
                                {
                                    HomeWindow homeWindow = new HomeWindow();
                                    Close();
                                    //------------LOADER---------------
                                    Thread myThread = new Thread(new ThreadStart(ShowLoader));
                                    myThread.SetApartmentState(ApartmentState.STA);
                                    myThread.Start();
                                    Thread.Sleep(1000);
                                    myThread.Abort();
                                    homeWindow.Show();
                                    //---------------------------------
                                }
                            }
                            else
                            {
                                logPasswordBox.BorderBrush = new SolidColorBrush(Colors.Red);
                                logPasswordBox.ToolTip     = new ToolTip()
                                {
                                    Content = "Неверный пароль"
                                };
                            }
                        }
                        else
                        {
                            logLoginTextBox.BorderBrush = new SolidColorBrush(Colors.Red);
                            logLoginTextBox.ToolTip     = new ToolTip()
                            {
                                Content = "Пользователь с таким именем не найден"
                            };
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MaterialMessageBox.Show(ex.Message, "Ошибка");
            }
        }