예제 #1
0
        public void Register(LinUser user)
        {
            bool isExistGroup = _groupRepository.Select.Any(r => r.Id == user.GroupId);

            if (!isExistGroup)
            {
                throw new LinCmsException("分组不存在", ErrorCode.NotFound);
            }

            bool isRepeatNickName = _userRepository.Select.Any(r => r.Nickname == user.Nickname);

            if (isRepeatNickName)
            {
                throw new LinCmsException("用户名重复,请重新输入", ErrorCode.RepeatField);
            }

            if (!string.IsNullOrEmpty(user.Email.Trim()))
            {
                var isRepeatEmail = _userRepository.Select.Any(r => r.Email == user.Email.Trim());
                if (isRepeatEmail)
                {
                    throw new LinCmsException("注册邮箱重复,请重新输入", ErrorCode.RepeatField);
                }
            }

            user.Active   = 1;
            user.Admin    = 1;
            user.Password = LinCmsUtils.Get32Md5(user.Password);

            _userRepository.Insert(user);
        }
예제 #2
0
        public void ChangePassword(ChangePasswordDto passwordDto)
        {
            string oldPassword = LinCmsUtils.Get32Md5(passwordDto.OldPassword);

            _userRepository.Select.Any(r => r.Password == oldPassword && r.Id == _currentUser.Id);

            string newPassword = LinCmsUtils.Get32Md5(passwordDto.NewPassword);

            _freeSql.Update <LinUser>(_currentUser.Id).Set(a => new LinUser()
            {
                Password = newPassword
            }).ExecuteAffrows();
        }
예제 #3
0
        public void ResetPassword(int id, ResetPasswordDto resetPasswordDto)
        {
            bool userExist = _userRepository.Where(r => r.Id == id).Any();

            if (userExist == false)
            {
                throw new LinCmsException("用户不存在", ErrorCode.NotFound);
            }

            string confirmPassword = LinCmsUtils.Get32Md5(resetPasswordDto.ConfirmPassword);

            _freeSql.Update <LinUser>(id).Set(a => new LinUser()
            {
                Password = confirmPassword
            }).ExecuteAffrows();
        }
        /// <summary>
        /// 验证密码是否正确,生成Claims,返回用户身份信息
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public Task ValidateAsync(ResourceOwnerPasswordValidationContext context)
        {
            LinUser user = _useRepository.Where(r => r.Username == context.UserName || r.Email == context.UserName).ToOne();

            //验证失败
            if (user == null)
            {
                context.Result = new GrantValidationResult(TokenRequestErrors.InvalidGrant, "用户不存在");
                return(Task.CompletedTask);
            }

            if (user.Password != LinCmsUtils.Get32Md5(context.Password))
            {
                context.Result = new GrantValidationResult(TokenRequestErrors.InvalidGrant, "请输入正确密码!");
                return(Task.CompletedTask);
            }

            _useRepository.UpdateDiy.Set(r => new LinUser()
            {
                LastLoginTime = DateTime.Now
            }).Where(r => r.Id == user.Id).ExecuteAffrows();

            //subjectId 为用户唯一标识 一般为用户id
            //authenticationMethod 描述自定义授权类型的认证方法
            //authTime 授权时间
            //claims 需要返回的用户身份信息单元
            context.Result = new GrantValidationResult(
                user.Id.ToString(),
                OidcConstants.AuthenticationMethods.Password,
                _clock.UtcNow.UtcDateTime,
                new List <Claim>()
            {
                new Claim(ClaimTypes.NameIdentifier, user.Id.ToString()),
                new Claim(ClaimTypes.Email, user.Email ?? ""),
                new Claim(ClaimTypes.GivenName, user.Nickname ?? ""),
                new Claim(ClaimTypes.Name, user.Username ?? ""),
                new Claim(LinCmsClaimTypes.GroupId, user.GroupId.ToString()),
                new Claim(LinCmsClaimTypes.IsAdmin, user.IsAdmin().ToString()),
                new Claim(ClaimTypes.Role, user.IsAdmin()?LinGroup.Admin:user.GroupId.ToString())
            });
            return(Task.CompletedTask);
        }
예제 #5
0
        /// <summary>
        /// 验证密码是否正确,生成Claims,返回用户身份信息
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public Task ValidateAsync(ResourceOwnerPasswordValidationContext context)
        {
            LinUser user = _fsql.Select <LinUser>().Where(r => r.Nickname == context.UserName).ToOne();

            //验证失败
            if (user == null)
            {
                context.Result = new GrantValidationResult(TokenRequestErrors.InvalidGrant, "用户不存在");
                return(Task.CompletedTask);
            }

            if (user.Password != LinCmsUtils.Get32Md5(context.Password))
            {
                context.Result = new GrantValidationResult(TokenRequestErrors.InvalidGrant, "请输入正确密码!");
                return(Task.CompletedTask);
            }

            //subjectId 为用户唯一标识 一般为用户id
            //authenticationMethod 描述自定义授权类型的认证方法
            //authTime 授权时间
            //claims 需要返回的用户身份信息单元
            context.Result = new GrantValidationResult(
                user.Id.ToString(),
                OidcConstants.AuthenticationMethods.Password,
                _clock.UtcNow.UtcDateTime,
                new List <Claim>()
            {
                new Claim(ClaimTypes.NameIdentifier, user.Id.ToString()),
                new Claim(ClaimTypes.Email, user.Email),
                new Claim(ClaimTypes.SerialNumber, user.Nickname),
                new Claim(ClaimTypes.Name, user.Username),
                new Claim(LinCmsClaimTypes.GroupId, user.GroupId.ToString()),
                new Claim(LinCmsClaimTypes.IsAdmin, user.IsAdmin().ToString()),
                new Claim(ClaimTypes.Role, user.IsAdmin()?LinGroup.Administrator:user.GroupId.ToString())
            });
            return(Task.CompletedTask);
        }
예제 #6
0
        public LinUser Authorization(string username, string password)
        {
            LinUser user = _userRepository.Select.Where(r => r.Nickname == username && r.Password == LinCmsUtils.Get32Md5(password)).First();

            return(user);
        }
예제 #7
0
        public void Get32Md5One()
        {
            string result = LinCmsUtils.Get32Md5("123qwe");

            _testOutputHelper.WriteLine(result);
        }