Exemplo n.º 1
0
        public async Task <(bool IsValid, string ErrorMessage)> InsertValidateAsync(string userName, string password, string email, string mobile)
        {
            var config = await _configRepository.GetAsync();

            if (string.IsNullOrEmpty(userName))
            {
                return(false, "用户名不能为空");
            }
            if (userName.Length < config.AdminUserNameMinLength)
            {
                return(false, $"用户名长度必须大于等于{config.AdminUserNameMinLength}");
            }
            if (string.IsNullOrEmpty(password))
            {
                return(false, "密码不能为空");
            }
            if (password.Length < config.AdminPasswordMinLength)
            {
                return(false, $"密码长度必须大于等于{config.AdminPasswordMinLength}");
            }
            if (
                !PasswordRestrictionUtils.IsValid(password,
                                                  config.AdminPasswordRestriction))
            {
                return(false, $"密码不符合规则,请包含{config.AdminPasswordRestriction.GetDisplayName()}");
            }

            return(true, string.Empty);
        }
Exemplo n.º 2
0
        public async Task <(bool success, string errorMessage)> IsPasswordCorrectAsync(string password)
        {
            var config = await _configRepository.GetAsync();

            if (string.IsNullOrEmpty(password))
            {
                return(false, "密码不能为空");
            }
            if (password.Length < config.UserPasswordMinLength)
            {
                return(false, $"密码长度必须大于等于{config.UserPasswordMinLength}");
            }
            if (!PasswordRestrictionUtils.IsValid(password, config.UserPasswordRestriction))
            {
                return(false, $"密码不符合规则,请包含{config.UserPasswordRestriction.GetDisplayName()}");
            }
            return(true, string.Empty);
        }
Exemplo n.º 3
0
        private async Task <(bool success, string errorMessage)> InsertValidateAsync(string userName, string email, string mobile, string password, string ipAddress)
        {
            var config = await _configRepository.GetAsync();

            if (await IsIpAddressCachedAsync(ipAddress))
            {
                return(false, $"同一IP在{config.UserRegistrationMinMinutes}分钟内只能注册一次");
            }
            if (string.IsNullOrEmpty(password))
            {
                return(false, "密码不能为空");
            }
            if (password.Length < config.UserPasswordMinLength)
            {
                return(false, $"密码长度必须大于等于{config.UserPasswordMinLength}");
            }
            if (!PasswordRestrictionUtils.IsValid(password, config.UserPasswordRestriction))
            {
                return(false, $"密码不符合规则,请包含{config.UserPasswordRestriction.GetDisplayName()}");
            }
            if (string.IsNullOrEmpty(userName))
            {
                return(false, "用户名为空,请填写用户名");
            }
            if (!string.IsNullOrEmpty(userName) && await IsUserNameExistsAsync(userName))
            {
                return(false, "用户名已被注册,请更换用户名");
            }
            if (!IsUserNameCompliant(userName.Replace("@", string.Empty).Replace(".", string.Empty)))
            {
                return(false, "用户名包含不规则字符,请更换用户名");
            }

            if (!string.IsNullOrEmpty(email) && await IsEmailExistsAsync(email))
            {
                return(false, "电子邮件地址已被注册,请更换邮箱");
            }
            if (!string.IsNullOrEmpty(mobile) && await IsMobileExistsAsync(mobile))
            {
                return(false, "手机号码已被注册,请更换手机号码");
            }

            return(true, string.Empty);
        }
Exemplo n.º 4
0
        public async Task <(bool success, string errorMessage)> ChangePasswordAsync(int userId, string password)
        {
            var config = await _configRepository.GetAsync();

            if (password.Length < config.UserPasswordMinLength)
            {
                return(false, $"密码长度必须大于等于{config.UserPasswordMinLength}");
            }
            if (!PasswordRestrictionUtils.IsValid(password, config.UserPasswordRestriction))
            {
                return(false, $"密码不符合规则,请包含{config.UserPasswordRestriction.GetDisplayName()}");
            }

            var passwordSalt = GenerateSalt();

            password = EncodePassword(password, PasswordFormat.Encrypted, passwordSalt);
            await ChangePasswordAsync(userId, PasswordFormat.Encrypted, passwordSalt, password);

            return(true, string.Empty);
        }
Exemplo n.º 5
0
        public async Task <(bool IsValid, string ErrorMessage)> ChangePasswordAsync(Administrator adminEntity, string password)
        {
            var config = await _configRepository.GetAsync();

            if (string.IsNullOrEmpty(password))
            {
                return(false, "密码不能为空");
            }
            if (password.Length < config.AdminPasswordMinLength)
            {
                return(false, $"密码长度必须大于等于{config.AdminPasswordMinLength}");
            }
            if (
                !PasswordRestrictionUtils.IsValid(password, config.AdminPasswordRestriction))
            {
                return(false, $"密码不符合规则,请包含{config.AdminPasswordRestriction.GetDisplayName()}");
            }

            password = EncodePassword(password, PasswordFormat.Encrypted, out var passwordSalt);
            await ChangePasswordAsync(adminEntity, PasswordFormat.Encrypted, passwordSalt, password);

            return(true, string.Empty);
        }