public ActionResult Get(PostModel postModel, string echostr) { if (CheckSignature.Check(postModel.Signature, postModel.Timestamp, postModel.Nonce, WeiXininfo.Token)) { return Content(echostr); //返回随机字符串则表示验证通过 } else { return Content("failed:" + postModel.Signature + "," + WeiXin.MP.CheckSignature.GetSignature(postModel.Timestamp, postModel.Nonce, WeiXininfo.Token) + "。" + "如果你在浏览器中看到这句话,说明此地址可以被作为微信公众账号后台的Url,请注意保持Token一致。"); } }
public ActionResult MiniPost(PostModel postModel) { if (!CheckSignature.Check(postModel.Signature, postModel.Timestamp, postModel.Nonce, this.WeiXininfo.Token)) { //return Content("参数错误!");//v0.7- return new WeiXinResult("参数错误!");//v0.8+ } postModel.Token = WeiXininfo.Token; postModel.EncodingAESKey = WeiXininfo.EncodingAESKey;//根据自己后台的设置保持一致 postModel.AppId = WeiXininfo.appID;//根据自己后台的设置保持一致 var messageHandler = new CustomMessageHandler(Request.InputStream, postModel, 10); messageHandler.Execute();//执行微信处理过程 return new FixWeiXinBugWeiXinResult(messageHandler);//v0.8+ }
//<?xml version="1.0" encoding="utf-8"?> //<xml> // <ToUserName><![CDATA[gh_a96a4a619366]]></ToUserName> // <FromUserName><![CDATA[olPjZjsXuQPJoV0HlruZkNzKc91E]]></FromUserName> // <CreateTime>1357986928</CreateTime> // <MsgType><![CDATA[text]]></MsgType> // <Content><![CDATA[中文]]></Content> // <MsgId>5832509444155992350</MsgId> //</xml> /// <summary> /// 获取XDocument转换后的IRequestMessageBase实例。 /// 如果MsgType不存在,抛出UnknownRequestMsgTypeException异常 /// </summary> /// <returns></returns> public static IRequestMessageBase GetRequestEntity(XDocument doc, PostModel postModel = null) { RequestMessageBase requestMessage = null; RequestMsgType msgType; try { msgType = MsgTypeHelper.GetRequestMsgType(doc); switch (msgType) { case RequestMsgType.Text: requestMessage = new RequestMessageText(); break; case RequestMsgType.Location: requestMessage = new RequestMessageLocation(); break; case RequestMsgType.Image: requestMessage = new RequestMessageImage(); break; case RequestMsgType.Voice: requestMessage = new RequestMessageVoice(); break; case RequestMsgType.Video: requestMessage = new RequestMessageVideo(); break; case RequestMsgType.Link: requestMessage = new RequestMessageLink(); break; case RequestMsgType.ShortVideo: requestMessage = new RequestMessageShortVideo(); break; case RequestMsgType.Event: //判断Event类型 switch (doc.Root.Element("Event").Value.ToUpper()) { case "ENTER"://进入会话 requestMessage = new RequestMessageEvent_Enter(); break; case "LOCATION"://地理位置 requestMessage = new RequestMessageEvent_Location(); break; case "SUBSCRIBE"://订阅(关注) requestMessage = new RequestMessageEvent_Subscribe(); break; case "UNSUBSCRIBE"://取消订阅(关注) requestMessage = new RequestMessageEvent_Unsubscribe(); break; case "CLICK"://菜单点击 requestMessage = new RequestMessageEvent_Click(); break; case "SCAN"://二维码扫描 requestMessage = new RequestMessageEvent_Scan(); break; case "VIEW"://URL跳转 requestMessage = new RequestMessageEvent_View(); break; case "MASSSENDJOBFINISH": requestMessage = new RequestMessageEvent_MassSendJobFinish(); break; case "TEMPLATESENDJOBFINISH"://模板信息 requestMessage = new RequestMessageEvent_TemplateSendJobFinish(); break; case "SCANCODE_PUSH"://扫码推事件(scancode_push) requestMessage = new RequestMessageEvent_Scancode_Push(); break; case "SCANCODE_WAITMSG"://扫码推事件且弹出“消息接收中”提示框(scancode_waitmsg) requestMessage = new RequestMessageEvent_Scancode_Waitmsg(); break; case "PIC_SYSPHOTO"://弹出系统拍照发图(pic_sysphoto) requestMessage = new RequestMessageEvent_Pic_Sysphoto(); break; case "PIC_PHOTO_OR_ALBUM"://弹出拍照或者相册发图(pic_photo_or_album) requestMessage = new RequestMessageEvent_Pic_Photo_Or_Album(); break; case "PIC_WeiXin"://弹出微信相册发图器(pic_WeiXin) requestMessage = new RequestMessageEvent_Pic_WeiXin(); break; case "LOCATION_SELECT"://弹出地理位置选择器(location_select) requestMessage = new RequestMessageEvent_Location_Select(); break; case "CARD_PASS_CHECK"://卡券通过审核 requestMessage = new RequestMessageEvent_Card_Pass_Check(); break; case "CARD_NOT_PASS_CHECK"://卡券未通过审核 requestMessage = new RequestMessageEvent_Card_Not_Pass_Check(); break; case "USER_GET_CARD"://领取卡券 requestMessage = new RequestMessageEvent_User_Get_Card(); break; case "USER_DEL_CARD"://删除卡券 requestMessage = new RequestMessageEvent_User_Del_Card(); break; case "KF_CREATE_SESSION"://多客服接入会话 requestMessage = new RequestMessageEvent_Kf_Create_Session(); break; case "KF_CLOSE_SESSION"://多客服关闭会话 requestMessage = new RequestMessageEvent_Kf_Close_Session(); break; case "KF_SWITCH_SESSION"://多客服转接会话 requestMessage = new RequestMessageEvent_Kf_Switch_Session(); break; case "POI_CHECK_NOTIFY"://审核结果事件推送 requestMessage = new RequestMessageEvent_Poi_Check_Notify(); break; case "WIFICONNECTED"://Wi-Fi连网成功事件 requestMessage = new RequestMessageEvent_WifiConnected(); break; case "USER_CONSUME_CARD"://卡券核销 requestMessage = new RequestMessageEvent_User_Consume_Card(); break; case "USER_ENTER_SESSION_FROM_CARD"://从卡券进入公众号会话 requestMessage = new RequestMessageEvent_User_Enter_Session_From_Card(); break; case "USER_VIEW_CARD"://进入会员卡 requestMessage = new RequestMessageEvent_User_View_Card(); break; default://其他意外类型(也可以选择抛出异常) requestMessage = new RequestMessageEventBase(); break; } break; default: throw new UnknownRequestMsgTypeException(string.Format("MsgType:{0} 在RequestMessageFactory中没有对应的处理程序!", msgType), new ArgumentOutOfRangeException());//为了能够对类型变动最大程度容错(如微信目前还可以对公众账号suscribe等未知类型,但API没有开放),建议在使用的时候catch这个异常 } EntityHelper.FillEntityWithXml(requestMessage, doc); } catch (ArgumentException ex) { throw new WeiXinException(string.Format("RequestMessage转换出错!可能是MsgType不存在!,XML:{0}", doc.ToString()), ex); } return requestMessage; }
public ActionResult Post(PostModel postModel) { if (!CheckSignature.Check(postModel.Signature, postModel.Timestamp, postModel.Nonce, this.WeiXininfo.Token)) { return Content("参数错误!"); } postModel.Token = WeiXininfo.Token; postModel.EncodingAESKey = WeiXininfo.EncodingAESKey;//根据自己后台的设置保持一致 postModel.AppId = WeiXininfo.appID;//根据自己后台的设置保持一致 //每个人上下文消息储存的最大数量,防止内存占用过多,如果该参数小于等于0,则不限制 var maxRecordCount = 10; var logPath = Server.MapPath(string.Format("~/App_Data/MP/{0}/", DateTime.Now.ToString("yyyy-MM-dd"))); if (!Directory.Exists(logPath)) { Directory.CreateDirectory(logPath); } //自定义MessageHandler,对微信请求的详细判断操作都在这里面。 var messageHandler = new CustomMessageHandler(Request.InputStream, postModel, maxRecordCount); try { //测试时可开启此记录,帮助跟踪数据,使用前请确保App_Data文件夹存在,且有读写权限。 messageHandler.RequestDocument.Save(Path.Combine(logPath, string.Format("{0}_Request_{1}.txt", DateTime.Now.Ticks, messageHandler.RequestMessage.FromUserName))); if (messageHandler.UsingEcryptMessage) { messageHandler.EcryptRequestDocument.Save(Path.Combine(logPath, string.Format("{0}_Request_Ecrypt_{1}.txt", DateTime.Now.Ticks, messageHandler.RequestMessage.FromUserName))); } messageHandler.OmitRepeatedMessage = true; //消息去重功能 //执行微信处理过程 messageHandler.Execute(); //测试时可开启,帮助跟踪数据 //if (messageHandler.ResponseDocument == null) //{ // throw new Exception(messageHandler.RequestDocument.ToString()); //} if (messageHandler.ResponseDocument != null) { messageHandler.ResponseDocument.Save(Path.Combine(logPath, string.Format("{0}_Response_{1}.txt", DateTime.Now.Ticks, messageHandler.RequestMessage.FromUserName))); } if (messageHandler.UsingEcryptMessage) { //记录加密后的响应信息 messageHandler.FinalResponseDocument.Save(Path.Combine(logPath, string.Format("{0}_Response_Final_{1}.txt", DateTime.Now.Ticks, messageHandler.RequestMessage.FromUserName))); } return new FixWeiXinBugWeiXinResult(messageHandler);//为了解决官方微信5.0软件换行bug暂时添加的方法 } catch (Exception ex) { using (TextWriter tw = new StreamWriter(Server.MapPath("~/App_Data/Error_" + DateTime.Now.Ticks + ".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(""); } }