Exemplo n.º 1
0
        /// <summary>
        /// 通过模式2提交到监听者
        /// 通过HTTP协议Post消息到ListenerWebUrl的web接口,提交的对象类型为SMessagePostData
        /// 其中Signature为Id+Type+Data+CurrentTime的字符串格式(yyyy-MM-dd hh:mm:ss)通过ListenerWebSignature签名后的字符串
        /// 接收方需要对此进行校验
        /// </summary>
        /// <param name="listener"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        private async Task <ValidateResult> PostToListenerByMode2(SMessageTypeListener listener, SMessage message)
        {
            ValidateResult result = new ValidateResult()
            {
                Result = true
            };
            var expireTime        = DateTime.UtcNow.AddSeconds(300);
            var strContent        = $"{message.ID.ToString()}{message.Type}{message.Data}{expireTime.ToString("yyyy-MM-dd hh:mm:ss")}";
            var strSignature      = _securityService.SignByKey(strContent, listener.ListenerWebSignature);
            SMessagePostData data = new SMessagePostData()
            {
                ID         = message.ID,
                ExpireTime = expireTime,
                Type       = message.Type,
                Data       = message.Data,
                Signature  = strSignature
            };

            try
            {
                await HttpClinetHelper.PostAsync(data, listener.ListenerWebUrl);
            }
            catch (Exception ex)
            {
                result.Result      = false;
                result.Description = ex.Message;
            }

            return(result);
        }
        public async Task <ValidateResult> ValidateSMessageData(ClientSMessageTypeListenerEndpoint endpoint, SMessagePostData data)
        {
            ValidateResult result = new ValidateResult()
            {
                Result = true
            };
            var strContent   = $"{data.ID.ToString()}{data.Type}{data.Data}{data.ExpireTime.ToString("yyyy-MM-dd hh:mm:ss")}";
            var strSignature = _securityService.SignByKey(strContent, endpoint.SignatureKey);

            if (strSignature != data.Signature)
            {
                result.Result      = false;
                result.Description = string.Format(StringLanguageTranslate.Translate(TextCodes.ClientSMessageTypeListenerEndpointSignatureError, "客户端消息类型监听终结点{0}在解析消息数据时消息数据的签名错误,请检查客户端的签名与服务端对应的签名是否一致"), endpoint.Name);
                return(result);
            }

            if (data.ExpireTime < DateTime.UtcNow)
            {
                result.Result      = false;
                result.Description = string.Format(StringLanguageTranslate.Translate(TextCodes.ClientSMessageTypeListenerEndpointMessageExpire, "客户端消息类型监听终结点{0}接收的消息已过期,过期时间为{1}"), endpoint.Name, data.ExpireTime.ToString());
                return(result);
            }

            return(await Task.FromResult(result));
        }
 /// <summary>
 /// 验证消息数据是否合法
 /// </summary>
 /// <param name="data"></param>
 /// <returns></returns>
 public async Task <ValidateResult> ValidateSMessageData(SMessagePostData data)
 {
     return(await _imp.ValidateSMessageData(this, data));
 }