/// <summary> /// 统一进行接口返回输出 /// </summary> /// <param name="item">接口返回对象</param> /// <returns></returns> protected virtual JsonResult Json(ResponseModel item) { //获取服务器时间戳 item.timestamp = Tools.ConvertDateTimeToInt(DateTime.Now); //记录日志 ApiLog_Moni(Request.Headers, item); //判断接口请求参数是否加密 var endpoint = GetEndpoint(HttpContext); //如果URL与操作不匹配,则端点为null if (endpoint != null) { //获取方法的自定义特性 var enAttribute = endpoint.Metadata.GetMetadata <UPEncryptionAttribute>(); //默认输入输出都需要加密 if (enAttribute == null) { enAttribute = new UPEncryptionAttribute(); } //输出参数需要加密 if (enAttribute.IsOutEncryption && item.data != null) { string dataJson = Strings.ObjectToJson(item.data); //输出数据进行AES加密 item.data = HttpUtility.UrlEncode(AESEncryptWeb.EncryptByAES(dataJson, item.timestamp.ToString())); } } var jsonResult = new JsonResult(item); jsonResult.StatusCode = 200; jsonResult.ContentType = "application/json"; return(jsonResult); }
public Task Invoke(HttpContext context) { var requestpath = context.Request.Path.Value.ToLower(); //GET请求不进行校验 if (context.Request.Method.ToUpper() == "GET") { return(this._next(context)); } //富文本上传文件不进行校验 if (requestpath.IndexOf("imagefile/imageupload") >= 0) { return(this._next(context)); } //判断接口请求参数是否加密 var endpoint = GetEndpoint(context); //如果URL与操作不匹配,则端点为null if (endpoint != null) { //获取方法的自定义特性 var enAttribute = endpoint.Metadata.GetMetadata <UPEncryptionAttribute>(); //默认输入输出都需要加密 if (enAttribute == null) { enAttribute = new UPEncryptionAttribute(); } //输入参数不加密,则直接跳过解密方法 if (enAttribute != null && !enAttribute.IsInEncryption) { //获取头信息 var headerResultNo = GetNoEnRequestHeaderData(context); //header校验失败 if (headerResultNo.code != ResponseCode.Success.ToInt32()) { //返回验证失败 string responStr = QWPlatform.SystemLibrary.Utils.Strings.ObjectToJson(headerResultNo); context.Response.WriteAsync(responStr); return(Task.CompletedTask); } else { return(this._next(context)); } } } //获取头信息 var headerResult = GetEnRequestHeaderData(context); //header校验失败 if (headerResult.code != ResponseCode.Success.ToInt32()) { //返回验证失败 string responStr = QWPlatform.SystemLibrary.Utils.Strings.ObjectToJson(headerResult); context.Response.WriteAsync(responStr); return(Task.CompletedTask); } if (headerResult.data != null) { var request = context.Request; //创建一个流 var requestBodyStream = new MemoryStream(); //转化为字节 byte[] content1 = Encoding.UTF8.GetBytes(headerResult.data.ToString()); //把修改写入流中 requestBodyStream.Write(content1, 0, content1.Length); //把修改后的内容赋值给请求body request.Body = requestBodyStream; request.Body.Seek(0, SeekOrigin.Begin); request.ContentLength = content1.Length; request.ContentType = "application/json"; } return(this._next(context)); }