コード例 #1
0
        private HttpResponseMessage EncryptResponse(HttpResponseMessage response)
        {
            if (!response.IsSuccessStatusCode)
            {
                return(response);
            }
            if (response.Content == null)
            {
                return(response);
            }
            string data = response.Content.ReadAsStringAsync().Result;

            try
            {
                if (_cryptor != null)
                {
                    data = _cryptor.EncryptData(data);
                }
                HttpContent content = new StringContent(data);
                content.Headers.ContentType = response.Content.Headers.ContentType;
                response.Content            = content;
                return(response);
            }
            catch (Exception ex)
            {
                Logger.Error(Resources.Http_Security_Host_EncryptResponse_Failt, ex);
                HttpError error = new HttpError(Resources.Http_Security_Host_EncryptResponse_Failt);
                return(response.RequestMessage.CreateErrorResponse(HttpStatusCode.BadRequest, error));
            }
        }
コード例 #2
0
        /// <summary>
        /// 客户端往服务端发送数据时对数据进行加密
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public override Task Send(string data)
        {
            if (_cryptor == null)
            {
                throw new InvalidOperationException("通信加密尚未初始化。");
            }
            //加密传输数据
            HubInvocation hubData = this.JsonDeserializeObject <HubInvocation>(data);

            JToken[] args = hubData.Args;
            if (args != null && args.Length > 0)
            {
                string        encrypt = _cryptor.EncryptData(this.JsonSerializeObject(args));
                List <JToken> @params = new List <JToken>()
                {
                    JToken.FromObject(encrypt, JsonSerializer)
                };
                if (args.Length > 1)
                {
                    @params = @params.Concat(new JToken[args.Length - 1]).ToList();
                }
                hubData.Args = @params.ToArray();
                data         = this.JsonSerializeObject(hubData);
            }

            return(base.Send(data));
        }
コード例 #3
0
        /// <summary>
        /// 将Hub处理完之后的数据加密以进行传输
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        protected override bool OnBeforeOutgoing(IHubOutgoingInvokerContext context)
        {
            //if (!_canCrypto)
            //{
            //    return base.OnBeforeOutgoing(context);
            //}
            //数据加密
            if (_cryptor == null)
            {
                return(false);
            }
            object[] args    = context.Invocation.Args;
            string   encrypt = _cryptor.EncryptData(JsonConvert.SerializeObject(args));

            context.Invocation.Args = new object[] { encrypt };
            return(base.OnBeforeOutgoing(context));
        }
コード例 #4
0
ファイル: HostCryptoHandler.cs プロジェクト: daywrite/OBear
 private Task <HttpResponseMessage> CreateResponseTask(HttpRequestMessage request,
                                                       HttpStatusCode statusCode,
                                                       string message,
                                                       Exception ex = null)
 {
     return(Task <HttpResponseMessage> .Factory.StartNew(() =>
     {
         if (statusCode >= HttpStatusCode.OK && statusCode <= (HttpStatusCode)299)
         {
             if (_publicKey != null)
             {
                 message = CommunicationCryptor.EncryptData(message, _privateKey, _publicKey, _hashType);
                 return request.CreateResponse(statusCode, message);
             }
         }
         HttpResponseMessage response = request.CreateErrorResponse(statusCode, ex == null ? new HttpError(message) : new HttpError(ex, false));
         return response;
     }));
 }
コード例 #5
0
        /// <summary>
        /// 客户端往服务端发送数据时对数据进行加密
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public override Task Send(string data)
        {
            if (_cryptor == null)
            {
                throw new InvalidOperationException("通信加密尚未初始化。");
            }
            //加密传输数据
            HubInvocation hubData = this.JsonDeserializeObject <HubInvocation>(data);

            JToken[] args = hubData.Args;
            if (args.Length > 0)
            {
                string encrypt = _cryptor.EncryptData(this.JsonSerializeObject(args));
                hubData.Args = new[] { JToken.FromObject(encrypt, JsonSerializer) };
                data         = this.JsonSerializeObject(hubData);
            }

            return(base.Send(data));
        }
コード例 #6
0
ファイル: ClientCryptoHandler.cs プロジェクト: daywrite/OBear
        private Task <HttpResponseMessage> EncryptRequest(HttpRequestMessage request)
        {
            request.Headers.Add(HttpHeaderNames.OBearClientPublicKey, _clientPublicKey);

            if (request.Method == HttpMethod.Get || request.Content == null)
            {
                return(null);
            }
            string data = request.Content.ReadAsStringAsync().Result;

            try
            {
                data = CommunicationCryptor.EncryptData(data, _clientPrivateKey, _publicKey, _hashType);
                HttpContent content = new StringContent(data);
                content.Headers.ContentType = request.Content.Headers.ContentType;
                request.Content             = content;
                return(null);
            }
            catch (Exception ex)
            {
                return(CreateResponseTask(request, HttpStatusCode.BadRequest, Resources.Http_Security_Client_EncryptRequest_Failt, ex));
            }
        }