private void WriteOpsLog(dynamic logInfo)
 {
     try
     {
         var properties = _mqProducer.CreateBasicProperties();
         //设置消息持久化
         properties.Persistent = true;
         _mqProducer.BasicPublish(BaseMqExchanges.Logs, BaseMqRoutingKeys.OpsLog, logInfo, properties);
     }
     catch (Exception ex)
     {
         _logger.LogError(ex.Message, ex);
     }
 }
Exemple #2
0
        public void Intercept(IInvocation invocation)
        {
            invocation.Proceed();

            var serviceMethod = invocation.Method ?? invocation.MethodInvocationTarget;
            var attribute     = serviceMethod.GetCustomAttribute <OpsLogAttribute>();

            if (attribute == null)
            {
                return;
            }

            if (_isLoging)
            {
                return;
            }
            else
            {
                _isLoging = true;
            }

            var logInfo = new
            {
                ClassName       = serviceMethod.DeclaringType.FullName,
                CreateTime      = DateTime.Now,
                LogName         = attribute.LogName,
                LogType         = "操作日志",
                Message         = JsonSerializer.Serialize(invocation.Arguments),
                Method          = serviceMethod.Name,
                Succeed         = "",
                UserId          = _userContext.ID,
                UserName        = _userContext.Name,
                Account         = _userContext.Account,
                RemoteIpAddress = _userContext.RemoteIpAddress
            };


            var properties = _mqProducer.CreateBasicProperties();

            //设置消息持久化
            properties.Persistent = true;
            _mqProducer.BasicPublish(BaseMqExchanges.Logs, BaseMqRoutingKeys.OpsLog, logInfo, properties);
        }
Exemple #3
0
        public async Task <UserValidateDto> Login(UserValidateInputDto inputDto, CurrenUserInfoDto currentUser)
        {
            //var user4 = _userRepository.GetAll<SysMenu>().FirstOrDefault();
            //var user0 = _rsp.GetAll<SysUser>().FirstOrDefault();
            var user = await _userRepository.FetchAsync(x => new { x.Password, x.Salt, x.Name, x.Email, x.RoleId, x.Account, x.ID, x.Status }, x => x.Account == inputDto.Account);

            dynamic log = new ExpandoObject();

            log.ID              = IdGenerater.GetNextId(IdGenerater.DatacenterId, IdGenerater.WorkerId);
            log.Account         = inputDto.Account;
            log.CreateTime      = DateTime.Now;
            log.Device          = currentUser.Device;
            log.RemoteIpAddress = currentUser.RemoteIpAddress;
            log.Message         = string.Empty;
            log.Succeed         = false;
            log.UserId          = user?.ID;
            log.UserName        = user?.Name;

            if (user == null)
            {
                var errorModel = new ErrorModel(ErrorCode.NotFound, "用户名或密码错误");
                log.Message = JsonSerializer.Serialize(errorModel);
                throw new BusinessException(errorModel);
            }
            else
            {
                if (user.Status != 1)
                {
                    var errorModel = new ErrorModel(ErrorCode.TooManyRequests, "账号已锁定");
                    log.Message = JsonSerializer.Serialize(errorModel);
                    _mqProducer.BasicPublish(MqExchanges.Logs, MqRoutingKeys.Loginlog, log);
                    throw new BusinessException(errorModel);
                }

                //var logins = await _loginLogRepository.SelectAsync(5, x => new { x.ID, x.Succeed,x.CreateTime }, x => x.UserId == user.ID, x => x.ID, false);
                //var failLoginCount = logins.Count(x => x.Succeed == false);

                var failLoginCount = 2;

                if (failLoginCount == 5)
                {
                    var errorModel = new ErrorModel(ErrorCode.TooManyRequests, "连续登录失败次数超过5次,账号已锁定");
                    log.Message = JsonSerializer.Serialize(errorModel);
                    await _userRepository.UpdateAsync(new SysUser()
                    {
                        ID = user.ID, Status = 2
                    }, x => x.Status);

                    throw new BusinessException(errorModel);
                }

                if (HashHelper.GetHashedString(HashType.MD5, inputDto.Password, user.Salt) != user.Password)
                {
                    var errorModel = new ErrorModel(ErrorCode.NotFound, "用户名或密码错误");
                    log.Message = JsonSerializer.Serialize(errorModel);
                    _mqProducer.BasicPublish(MqExchanges.Logs, MqRoutingKeys.Loginlog, log);
                    throw new BusinessException(errorModel);
                }

                if (string.IsNullOrEmpty(user.RoleId))
                {
                    var errorModel = new ErrorModel(ErrorCode.Forbidden, "未分配任务角色,请联系管理员");
                    log.Message = JsonSerializer.Serialize(errorModel);
                    _mqProducer.BasicPublish(MqExchanges.Logs, MqRoutingKeys.Loginlog, log);
                    throw new BusinessException(errorModel);
                }
            }

            log.Message = "登录成功";
            log.Succeed = true;
            _mqProducer.BasicPublish(MqExchanges.Logs, MqRoutingKeys.Loginlog, log);
            return(_mapper.Map <UserValidateDto>(user));
        }
Exemple #4
0
        public async Task <AppSrvResult <UserValidateDto> > LoginAsync(UserLoginDto inputDto)
        {
            var user = await _userRepository.FetchAsync(x => new
            {
                x.Password
                ,
                x.Salt
                ,
                x.Status
                ,
                UserValidateInfo = new UserValidateDto
                {
                    Account = x.Account
                    ,
                    Email = x.Email
                    ,
                    Id = x.Id
                    ,
                    Name = x.Name
                    ,
                    RoleIds = x.RoleIds
                }
            }
                                                        , x => x.Account == inputDto.Account);

            if (user == null)
            {
                return(Problem(HttpStatusCode.NotFound, "用户名或密码错误"));
            }

            dynamic log = new ExpandoObject();

            log.Account    = inputDto.Account;
            log.CreateTime = DateTime.Now;
            var httpContext = HttpContextUtility.GetCurrentHttpContext();

            log.Device          = httpContext.Request.Headers["device"].FirstOrDefault() ?? "web";
            log.RemoteIpAddress = httpContext.Connection.RemoteIpAddress.MapToIPv4().ToString();
            log.Succeed         = false;
            log.UserId          = user.UserValidateInfo.Id;
            log.UserName        = user.UserValidateInfo.Name;

            if (user.Status != 1)
            {
                var problem = Problem(HttpStatusCode.TooManyRequests, "账号已锁定");
                log.Message    = problem.Detail;
                log.StatusCode = problem.Status;
                _mqProducer.BasicPublish(MqExchanges.Logs, MqRoutingKeys.Loginlog, log);
                return(problem);
            }

            //var logins = await _loginLogRepository.SelectAsync(5, x => new { x.Id, x.Succeed,x.CreateTime }, x => x.UserId == user.Id, x => x.Id, false);
            //var failLoginCount = logins.Count(x => x.Succeed == false);

            var failLoginCount = 2;

            if (failLoginCount == 5)
            {
                var problem = Problem(HttpStatusCode.TooManyRequests, "连续登录失败次数超过5次,账号已锁定");
                log.Message    = problem.Detail;
                log.StatusCode = problem.Status;
                await _userRepository.UpdateAsync(new SysUser()
                {
                    Id = user.UserValidateInfo.Id, Status = 2
                }, UpdatingProps <SysUser>(x => x.Status));

                _mqProducer.BasicPublish(MqExchanges.Logs, MqRoutingKeys.Loginlog, log);
                return(problem);
            }

            if (HashHelper.GetHashedString(HashType.MD5, inputDto.Password, user.Salt) != user.Password)
            {
                var problem = Problem(HttpStatusCode.BadRequest, "用户名或密码错误");
                log.Message    = problem.Detail;
                log.StatusCode = problem.Status;
                _mqProducer.BasicPublish(MqExchanges.Logs, MqRoutingKeys.Loginlog, log);
                return(problem);
            }

            if (user.UserValidateInfo.RoleIds.IsNullOrEmpty())
            {
                var problem = Problem(HttpStatusCode.Forbidden, "未分配任务角色,请联系管理员");
                log.Message    = problem.Detail;
                log.StatusCode = problem.Status;
                _mqProducer.BasicPublish(MqExchanges.Logs, MqRoutingKeys.Loginlog, log);
                return(problem);
            }

            log.Message    = "登录成功";
            log.StatusCode = (int)HttpStatusCode.Created;
            log.Succeed    = true;
            _mqProducer.BasicPublish(MqExchanges.Logs, MqRoutingKeys.Loginlog, log);

            return(user.UserValidateInfo);
        }