/// <summary> /// 数据传到Hub之前进行数据解密 /// </summary> /// <param name="context"></param> /// <returns></returns> protected override bool OnBeforeIncoming(IHubIncomingInvokerContext context) { //_canCrypto = CanCrypto(context.Hub.Context); //if (!_canCrypto) //{ // return base.OnBeforeIncoming(context); //} //数据解密 string facePublicKey = context.Hub.Context.Headers.Get(HttpHeaderNames.MesClientPublicKey); if (string.IsNullOrEmpty(facePublicKey)) { return false; } _cryptor = new CommunicationCryptor(_ownPrivateKey, facePublicKey, _hashType); if (context.Args.Count == 1) { string encrypt = (string)context.Args[0]; string json = _cryptor.DecryptAndVerifyData(encrypt); IList<object> args = JsonConvert.DeserializeObject<IList<object>>(json); context.Args.Clear(); IList<object> values = context.MethodDescriptor.Parameters.Zip(args, (desc, arg) => ResolveParameter(desc, arg)).ToList(); foreach (object arg in values) { context.Args.Add(arg); } } return base.OnBeforeIncoming(context); }
private Task<HttpResponseMessage> DecryptRequest(HttpRequestMessage request) { if (!request.Headers.Contains(HttpHeaderNames.MesClientPublicKey)) { return CreateResponseTask(request, HttpStatusCode.BadRequest, "在请求头中客户端公钥信息无法找到。"); } string publicKey = request.Headers.GetValues(HttpHeaderNames.MesClientPublicKey).First(); _cryptor = new CommunicationCryptor(_privateKey, publicKey, _hashType); if (request.Content == null) { return null; } string data = request.Content.ReadAsStringAsync().Result; if (string.IsNullOrEmpty(data)) { return null; } try { if (_cryptor != null) { data = _cryptor.DecryptAndVerifyData(data); } if (data == null) { throw new MesException("服务器解析请求数据时发生异常。"); } HttpContent content = new StringContent(data); content.Headers.ContentType = request.Content.Headers.ContentType; request.Content = content; return null; } catch (CryptographicException ex) { const string message = "服务器解析传输数据时发生异常。"; Logger.Error(message, ex); return CreateResponseTask(request, HttpStatusCode.BadRequest, message, ex); } catch (Exception ex) { Logger.Error(Resources.Http_Security_Host_DecryptRequest_Failt, ex); return CreateResponseTask(request, HttpStatusCode.BadRequest, Resources.Http_Security_Host_DecryptRequest_Failt, ex); } }