public ActionResult Get(Senparc.Weixin.WxOpen.Entities.Request.PostModel postModel, string echostr) { if (CheckSignature.Check(postModel.Signature, postModel.Timestamp, postModel.Nonce, Token)) { return(Content(echostr)); //返回随机字符串则表示验证通过 } else { return(Content("failed:" + postModel.Signature + "," + Senparc.Weixin.MP.CheckSignature.GetSignature(postModel.Timestamp, postModel.Nonce, Token) + "。" + "如果你在浏览器中看到这句话,说明此地址可以被作为微信小程序后台的Url,请注意保持Token一致。")); } }
public ActionResult Post(Senparc.Weixin.WxOpen.Entities.Request.PostModel postModel) { if (!CheckSignature.Check(postModel.Signature, postModel.Timestamp, postModel.Nonce, Token)) { return(Content("参数错误!")); } postModel.Token = Token; //根据自己后台的设置保持一致 postModel.EncodingAESKey = EncodingAesKey; //根据自己后台的设置保持一致 postModel.AppId = WxOpenAppId; //根据自己后台的设置保持一致(必须提供) //v4.2.2之后的版本,可以设置每个人上下文消息储存的最大数量,防止内存占用过多,如果该参数小于等于0,则不限制 var maxRecordCount = 10; var logPath = ServerUtility.ContentRootMapPath(string.Format("~/logs/WxOpen/{0}/", SystemTime.Now.ToString("yyyy-MM-dd"))); if (!Directory.Exists(logPath)) { Directory.CreateDirectory(logPath); } //自定义MessageHandler,对微信请求的详细判断操作都在这里面。 var messageHandler = new CustomWxOpenMessageHandler(Request.GetRequestMemoryStream(), postModel, maxRecordCount); try { /* 如果需要添加消息去重功能,只需打开OmitRepeatedMessage功能,SDK会自动处理。 * 收到重复消息通常是因为微信服务器没有及时收到响应,会持续发送2-5条不等的相同内容的RequestMessage*/ messageHandler.OmitRepeatedMessage = true; //测试时可开启此记录,帮助跟踪数据,使用前请确保App_Data文件夹存在,且有读写权限。 messageHandler.SaveRequestMessageLog();//记录 Request 日志(可选) //messageHandler.Execute();//执行微信处理过程(关键) messageHandler.Execute(); //执行微信处理过程(关键) messageHandler.SaveResponseMessageLog(); //记录 Response 日志(可选) //return Content(messageHandler.ResponseDocument.ToString());//v0.7- //return new Senparc.Weixin.MP.MvcExtension.FixWeixinBugWeixinResult(messageHandler);//为了解决官方微信5.0软件换行bug暂时添加的方法,平时用下面一个方法即可 return(new WeixinResult(messageHandler));//v0.8+ } catch (Exception ex) { using (TextWriter tw = new StreamWriter(ServerUtility.ContentRootMapPath("~/logs/Error_WxOpen_" + _getRandomFileName() + ".txt"))) { tw.WriteLine("ExecptionMessage:" + ex.Message); tw.WriteLine(ex.Source); tw.WriteLine(ex.StackTrace); //tw.WriteLine("InnerExecptionMessage:" + ex.InnerException.Message); if (messageHandler.ResponseDocument != null) { tw.WriteLine(messageHandler.ResponseDocument.ToString()); } if (ex.InnerException != null) { tw.WriteLine("========= InnerException ========="); tw.WriteLine(ex.InnerException.Message); tw.WriteLine(ex.InnerException.Source); tw.WriteLine(ex.InnerException.StackTrace); } tw.Flush(); tw.Close(); } return(Content("")); } }