private void Register()
        {
            try
            {
                string username        = regLoginTextBox.Text;
                string password        = regPasswordTextBox.Password;
                string confirmPassword = regConfirmPasswordTextBox.Password;

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

                if (Validation.TryValidateObject(registerModel, regLoginTextBox, regPasswordTextBox, regConfirmPasswordTextBox))
                {
                    SaltedHash saltedHash = new SaltedHash(password);
                    bool       isTeacher  = (bool)isTeacherCheckBox.IsChecked;

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

                            db.Users.Add(user);
                            db.SaveChanges();
                            OpenLoginWindow();
                        }
                        else
                        {
                            regLoginTextBox.BorderBrush = new SolidColorBrush(Colors.Red);
                            regLoginTextBox.ToolTip     = new ToolTip()
                            {
                                Content = "This username is already taken"
                            };
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK);
            }
        }
Example #2
0
        private void Login()
        {
            try
            {
                string username = logLoginTextBox.Text;
                string password = logPasswordTextBox.Password;

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

                if (Validation.TryValidateObject(loginModel, logLoginTextBox, logPasswordTextBox, 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.IsTeacher)
                                {
                                    ConstructorWindow constructorWindow = new ConstructorWindow();
                                    Close();
                                    constructorWindow.Show();
                                }
                                else
                                {
                                    PerformWindow performWindow = new PerformWindow();
                                    Close();
                                    performWindow.Show();

                                    if (user.Level == null)
                                    {
                                        LevelWindow levelWindow = new LevelWindow(performWindow);
                                        levelWindow.Show();
                                    }
                                }
                            }
                            else
                            {
                                logPasswordTextBox.BorderBrush = new SolidColorBrush(Colors.Red);
                                logPasswordTextBox.ToolTip     = new ToolTip()
                                {
                                    Content = "Wrong password"
                                };
                            }
                        }
                        else
                        {
                            logLoginTextBox.BorderBrush = new SolidColorBrush(Colors.Red);
                            logLoginTextBox.ToolTip     = new ToolTip()
                            {
                                Content = "User with this username is not found"
                            };
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK);
            }
        }