}         // LogInAction()

        /// <summary>
        /// Safeguard if developer forget user password. In case of not existing admin1 account new one is created.
        /// </summary>
        public static void AdminExist()
        {
            User user = MainWindowViewModel.Context.Users.FirstOrDefault(u => u.Name == "Admin1");

            if (user == null)
            {
                MainWindowViewModel.NotifyUser("Admin1 accoutn doesn't exist.");

                var tempUser = new User();

                tempUser.Name          = "Admin1";
                tempUser.PasswordSalt  = PasswordEncryptor.GenerateSalt();
                tempUser.Password      = PasswordEncryptor.GeneratePassword("Sauron666", tempUser.PasswordSalt);
                tempUser.Type          = Enumerators.UserTypeEnum.Admin;
                tempUser.AccountActive = true;

                MainWindowViewModel.Context.Users.Add(tempUser);
                MainWindowViewModel.SaveContext();
                UsersListViewModel.Instance.Refresh();
            }

            else
            {
                return;
            }
        }
示例#2
0
        }         // Cancel_button_Click()

        public void SavePassword(object parameters)
        {
            var values               = (object[])parameters;
            var passwordText         = ((PasswordBox)values[0]).Password;
            var passwordConfirmation = ((PasswordBox)values[1]).Password;
            var regexExpression      = new Regex(@"!|@|#|\$|%|\^|&|\*|\(|\)|-|_|=|\+");

            if (passwordText.Any(char.IsDigit) || regexExpression.IsMatch(passwordText))
            {
                if (passwordText.Length >= 8)
                {
                    if (passwordText == passwordConfirmation)
                    {
                        User selectedUser = MainWindowViewModel.Context.Users.FirstOrDefault(u => u.Id == userId);
                        selectedUser.PasswordSalt = PasswordEncryptor.GenerateSalt();
                        selectedUser.Password     = PasswordEncryptor.GeneratePassword(passwordProposition.Password, selectedUser.PasswordSalt);
                        MainWindowViewModel.SaveContext();
                        selectedUser = new User();
                        this.Close();
                    }
                    else
                    {
                        MainWindowViewModel.NotifyUser("Those passwords didn't match. Try again.");
                    }
                }
                else
                {
                    MainWindowViewModel.NotifyUser("Password is to short, should contain at least 8 characters.");
                }
            }
            else
            {
                MainWindowViewModel.NotifyUser("Invalid password. Password should contain at least one number or special character.");
            }
        }         // Save_password()
        /// <summary>
        /// After checking user and password existence, check if it's correct, then login user.
        /// </summary>
        public static void LogInAction(object userPassword)
        {
            User user     = MainWindowViewModel.Context.Users.FirstOrDefault(u => u.Name == UserName);
            var  password = ((PasswordBox)userPassword).Password;

            if (user != null)
            {
                if (user.PasswordSalt != null)
                {
                    var encryptedPassword = PasswordEncryptor.GeneratePassword(password, user.PasswordSalt);

                    if (user.Password.SequenceEqual(encryptedPassword))
                    {
                        if (user.AccountActive == true)
                        {
                            MainWindowViewModel.LogIn(user);
                        }

                        else
                        {
                            MainWindowViewModel.NotifyUser("Unable to sign in. The account has been locked. Please contact your administrator.");
                        }
                    }
                    else
                    {
                        MainWindowViewModel.NotifyUser("Wrong login or password");
                    }
                }
                else
                {
                    MainWindowViewModel.NotifyUser("Unable to sign in. The account has no password assigned. Please contact your administrator.");
                }
            }
            else
            {
                MainWindowViewModel.NotifyUser("Wrong login or password");
            }
        }         // LogInAction()