/// <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); }
/// <summary> /// 从请求头信息中获取加密请求的消息信息 /// </summary> /// <param name="context"></param> /// <returns></returns> private ResponseModel GetEnRequestHeaderData(HttpContext context) { //请求头信息验证未通过 var resultobj = new ResponseModel(ResponseCode.BadRequest, "头信息验证失败"); //实例化接口请求头信息 var requestHeader = new ApiRequestHeader(context); if (requestHeader == null) { return(resultobj); } var msg = ""; //判断必填参数 var required_result = RequiredAttribute.ValidateRequired(requestHeader, ref msg); //必填验证通过 if (!required_result) { resultobj.msg = msg + "当前验证无法通过"; return(resultobj); } //获取平台所有的应用信息 var userinfo_key = "applyitems"; var applyItems = CacheManager.Create().Get <List <AppLyInfo> >(userinfo_key); if (applyItems == null || !applyItems.Any()) { //数据库获取应用信息 applyItems = this.Query <AppLyInfo>() .Where("数据标识", 1) .GetModelList(); if (applyItems != null && applyItems.Any()) { var obj = CacheManager.Create().Set(userinfo_key, applyItems); } } if (applyItems == null || !applyItems.Any()) { resultobj.msg = msg + "没有找到平台的应用信息"; return(resultobj); } //获取请求的平台应用信息 var applyInfo = applyItems.FirstOrDefault(t => t.APPID == requestHeader.appid); if (applyInfo == null) { resultobj.msg = requestHeader.appid + "非法!"; return(resultobj); } string decrypt_timestamp = ""; try { var decryptData = GetRequestRsaData(applyInfo, requestHeader.sign); //解密失败 if (decryptData.code != ResponseCode.Success.ToInt32()) { resultobj = decryptData; return(resultobj); } decrypt_timestamp = decryptData.data.ToString(); } catch (Exception ex) { Logger.Instance.Error("RSA解密发生异常", ex); resultobj.msg = "请求参数签名不合法"; return(resultobj); } //签名参数校验不通过 if (decrypt_timestamp != requestHeader.timestamp) { resultobj.msg = "请求参数签名不合法"; return(resultobj); } //请求验签成功 resultobj.code = ResponseCode.Success.ToInt32(); if (!requestHeader.biz_content.IsNullOrEmpty()) { try { //json参数需要解密 string bodyData = HttpUtility.UrlDecode(requestHeader.biz_content, System.Text.Encoding.UTF8); //AES解密请求的参数信息,秘钥长度必须32位,"以0左补齐" string bodystring = AESEncryptWeb.DecryptByAES(bodyData, requestHeader.timestamp); string content = bodystring; //UrlHelper.ParameterToJsonString(bodystring); resultobj.data = content; Logger.Instance.Info("接口请求信息" + content); } catch (Exception ex) { Logger.Instance.Error("参数异常", ex); resultobj.code = ResponseCode.Error.ToInt32(); resultobj.msg = ex.Message; } } return(resultobj); }