/// <summary> /// 发生错误 处理方式 /// </summary> /// <param name="actionExecutedContext"></param> public override void OnException(HttpActionExecutedContext actionExecutedContext) { LogQueueModel logQueueModel = new LogQueueModel() { FileName = ApiFileDirectoryPara.ApiErrorDir, Msg = actionExecutedContext.Exception.Message }; LogHelp.AddLogQueue(logQueueModel); var returnModel = ApiReturnModel.ReturnError(actionExecutedContext.Exception.Message); actionExecutedContext.Response = new HttpResponseMessage(); actionExecutedContext.Response.Content = new StringContent(JsonConvert.SerializeObject(returnModel)); return; }
/// <summary> /// 微信回调 处理方法 包含签名验证 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="requestContent">回调内容</param> /// <param name="deserializeType">内容反序列化 类型id 默认为0 0-xml反序列化 1-json反序列化</param> /// <returns></returns> public override T Callback <T>(string requestContent, int deserializeType = 0) { T request = new T(); var pros = request.GetType().GetProperties(); XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(requestContent); XmlNode xmlNode = xmlDoc.FirstChild;//获取到根节点<xml> XmlNodeList nodes = xmlNode.ChildNodes; string sign = string.Empty; foreach (XmlNode xn in nodes) { foreach (var pro in pros) { if (xn.Name == pro.Name) { if (pro.PropertyType == typeof(int)) { pro.SetValue(request, Convert.ToInt32(xn.InnerText)); } else { pro.SetValue(request, xn.InnerText); } if (pro.Name == SignProName) { sign = xn.InnerText; } break; } } } if (CheckSign(sign, request)) { LogQueueModel logQueueModel = new LogQueueModel() { FileName = ApiFileDirectoryPara.WeiXinBusinessLog, Msg = $"收到微信合法回调 请求数据为 : \r\n {requestContent}" }; LogHelp.AddLogQueue(logQueueModel); return(request); } else { //说明有人伪造 请求 记录日志 LogQueueModel logQueueModel = new LogQueueModel() { FileName = ApiFileDirectoryPara.WeiXinBusinessLog, Msg = $"收到微信非法回调(签名错误) 请求数据为 : \r\n {requestContent}" }; LogHelp.AddLogQueue(logQueueModel); return(default(T)); } }