示例#1
0
        /// <summary>
        ///     检查请求签名合法性
        /// </summary>
        /// <param name="signature">加密签名字符串</param>
        /// <param name="timestamp">时间戳</param>
        /// <param name="nonce">随机数</param>
        /// <param name="appConfig">应用接入配置信息</param>
        /// <returns>CheckResult</returns>
        public CheckResult CheckRequestSignature(string signature, string timestamp, string nonce, AppConfig appConfig)
        {
            ValidateOperator.Begin()
            .NotNullOrEmpty(signature, "加密签名字符串")
            .NotNullOrEmpty(timestamp, "时间戳")
            .NotNullOrEmpty(nonce, "随机数")
            .NotNull(appConfig, "AppConfig");
            var appSecret        = appConfig.AppSecret;
            var signatureExpired = appConfig.SignatureExpiredMinutes;

            string[] data = { appSecret, timestamp, nonce };
            Array.Sort(data);
            var signatureText = string.Join("", data);

            signatureText = Md5Encryptor.Encrypt(signatureText);

            if (!signature.CompareIgnoreCase(signatureText) && CheckHelper.IsNumber(timestamp))
            {
                return(CheckResult.Success());
            }
            var timestampMillis =
                UnixEpochHelper.DateTimeFromUnixTimestampMillis(timestamp.ToDoubleOrDefault());
            var minutes = DateTime.UtcNow.Subtract(timestampMillis).TotalMinutes;

            return(minutes > signatureExpired?CheckResult.Fail("签名时间戳失效") : CheckResult.Success());
        }
示例#2
0
        /// <summary>
        ///     生成签名字符串
        /// </summary>
        /// <param name="appSecret">签名密钥</param>
        /// <param name="timestamp">时间戳</param>
        /// <param name="nonce">随机数</param>
        /// <returns>WebApi签名</returns>
        public static string Create(Guid appSecret, string timestamp, string nonce)
        {
            ValidateOperator.Begin()
            .NotNullOrEmpty(appSecret.ToString(), "签名密钥")
            .NotNullOrEmpty(timestamp, "时间戳")
            .NotNullOrEmpty(nonce, "随机数");
            string[] array = { appSecret.ToString(), timestamp, nonce };
            Array.Sort(array);
            var signatureText = string.Join("", array);

            signatureText = Md5Encryptor.Encrypt(signatureText);
            return(signatureText);
        }
        /// <summary>
        /// 创建员工
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task <Unit> Handle(CreateStaffCommand request, CancellationToken cancellationToken)
        {
            try
            {
                // 命令验证
                if (!request.IsValid())
                {
                    // 错误信息收集
                    NotifyValidationErrors(request);
                    return(Task.FromResult(new Unit()));
                }
                var staffInfo = _mapper.Map <CreateStaffCommand, Staff>(request);
                staffInfo.Password = _md5Encryptor.Encrypt(staffInfo.Password);
                // 判断组织机构编码或名称是否存在
                // 这些业务逻辑,当然要在领域层中(领域命令处理程序中)进行处理

                if (_staffRepository.GetAll(x => x.Account == request.Account).Any())
                {
                    _bus.RaiseEvent(new DomainNotification("", "Account已存在!"));
                    return(Task.FromResult(new Unit()));
                }
                if (!string.IsNullOrWhiteSpace(request.Mobile) && _staffRepository.GetAll(x => x.Mobile == request.Mobile).Any())
                {
                    _bus.RaiseEvent(new DomainNotification("", "手机号已存在!"));
                    return(Task.FromResult(new Unit()));
                }
                if (!string.IsNullOrWhiteSpace(request.Email) && _staffRepository.GetAll(x => x.Email == request.Email).Any())
                {
                    _bus.RaiseEvent(new DomainNotification("", "邮箱已存在!"));
                    return(Task.FromResult(new Unit()));
                }
                if (!_officeRepository.GetAll(x => x.Id == request.OfficeId).Any())
                {
                    _bus.RaiseEvent(new DomainNotification("", "所选组织机构不存在!"));
                    return(Task.FromResult(new Unit()));
                }
                if (request.RoleList != null && request.RoleList.Any() && _roleRepository.GetAll(x => request.RoleList.Contains(x.Id)).Count() == request.RoleList.Length)
                {
                    _bus.RaiseEvent(new DomainNotification("", "所选角色不存在!"));
                    return(Task.FromResult(new Unit()));
                }
                _staffRepository.Add(staffInfo);
                //存在角色则插入角色
                if (request.RoleList != null && request.RoleList.Any())
                {
                    var staffRoleList =
                        request.RoleList.Select(x => new StaffRole()
                    {
                        RoleId = x, StaffId = staffInfo.Id
                    });
                    _staffRoleRepository.Add(staffRoleList);
                }
                // 统一提交
                if (!Commit())
                {
                    throw new AggregateException("提交失败");
                }
            }
            catch (Exception e)
            {
                _bus.RaiseEvent(new DomainNotification("", $"系统异常,发生未知错误:{e.Message}"));
            }
            return(Task.FromResult(new Unit()));
        }