Esempio n. 1
0
        /// <summary>
        /// 验证用户的正确性
        /// </summary>
        /// <param name="account"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public Task <AccountUser> ValidateAccountUserAsync(string account, string password)
        {
            if (string.IsNullOrWhiteSpace(password))
            {
                throw new ArgumentNullException("密码不能为空");
            }

            var customer = _customerService.GetAccountUserByAccount(account);

            if (customer != null)
            {
                bool passwordCorrect = false;
                switch (customer.PasswordFormat)
                {
                case PasswordFormat.Clear:
                {
                    passwordCorrect = password == customer.Password;
                }
                break;

                case PasswordFormat.Encrypted:
                {
                    passwordCorrect = _encryptionService.EncryptText(password) == customer.Password;
                }
                break;

                case PasswordFormat.Hashed:
                {
                    string saltKey = _encryptionService.CreateSaltKey(5);
                    passwordCorrect = _encryptionService.CreatePasswordHash(password, saltKey, _customerSettings.HashedPasswordFormat) == customer.Password;
                }
                break;

                default:
                    break;
                }

                if (passwordCorrect)
                {
                    return(Task.FromResult(customer));
                }
            }

            return(Task.FromResult <AccountUser>(null));;
        }