/// <summary> /// /// </summary> /// <param name="inputStream"></param> /// <param name="maxRecordCount"></param> /// <param name="postModel">需要传入到Init的参数</param> public MessageHandler(Stream inputStream, IEncryptPostModel postModel, int maxRecordCount = 0) { var postDataDocument = XmlUtility.Convert(inputStream); //PostModel = postModel;//PostModel 在当前类初始化过程中必须赋值 CommonInitialize(postDataDocument, maxRecordCount, postModel); }
public ThirdPartyMessageHandler(Stream inputStream, PostModel postModel = null) { _postModel = postModel; EcryptRequestDocument = XmlUtility.Convert(inputStream);//原始加密XML转成XDocument Init(); }
public ActionResult Post(PostModel postModel) { if (!CheckSignature.Check(postModel.Signature, postModel.Timestamp, postModel.Nonce, weixinoption.Token)) { return(Content("参数错误!")); } postModel.Token = weixinoption.Token; postModel.EncodingAESKey = weixinoption.WeixinAESKey; //根据自己后台的设置保持一致 postModel.AppId = weixinoption.AppId; //根据自己后台的设置保持一致 //转化steam 为memory stream using (MemoryStream memory = new MemoryStream()) { this.Request.Body.CopyTo(memory); memory.Seek(0, SeekOrigin.Begin); var postDataDocument = XmlUtility.Convert(memory); var messageHandler = new WeixinMessageHandler(postDataDocument, postModel, ossOption, baiduapiOption, _baiyecontext, _logger); //接收消息(第一步) messageHandler.Execute(); //执行微信处理过程(第二步) return(new FixWeixinBugWeixinResult(messageHandler)); //返回(第三步) } }
public ActionResult Callback2(PostModel postModel) { if (postModel == null) { log4net.LogHelper.WriteInfo(this.GetType(), "微信回调信息postModel为null"); return(Content("success")); } try { postModel.Token = WxRequest._component_Token; postModel.EncodingAESKey = WxRequest._component_EncodingAESKey; //根据自己后台的设置保持一致 postModel.AppId = WxRequest._component_AppId; //根据自己后台的设置保持一致 var stream = Request.InputStream; var tempdoc = XmlUtility.Convert(stream); if (tempdoc == null) { return(Content("success")); } var postDataDocument = XDocument.Parse(tempdoc.ToString()); postDataDocument = WXRequestCommandBLL.Init(postModel, postDataDocument); if (postDataDocument != null) { string returntype = postDataDocument?.Root?.Element("Event")?.Value; switch (returntype) { case "weapp_audit_success": case "weapp_audit_fail": //小程序代码审核成功回调 WXRequestCommandBLL.CommandXCXPublish(returntype, postDataDocument); break; default: break; } } else { log4net.LogHelper.WriteInfo(this.GetType(), "微信回调信息错误"); } } catch (Exception ex) { log4net.LogHelper.WriteError(this.GetType(), ex); } return(Content("success")); }
public void ConvertTest() { using (var ms = new MemoryStream())//模拟已有stream { using (var sw = new StreamWriter(ms)) { sw.Write(xml); sw.Flush(); ms.Position = 0; var xdoc = XmlUtility.Convert(ms); Console.WriteLine(xdoc.ToString()); Assert.AreEqual("666", xdoc.Root.Element("Id").Value); Assert.AreEqual("Jeffrey", xdoc.Root.Element("Name").Value); } } }
public static XDocument Init(Stream inputStream, PostModel postModel) { XDocument edocument = XmlUtility.Convert(inputStream); if (postModel != null && edocument != null && edocument.Root.Element("Encrypt") != null && !string.IsNullOrEmpty(edocument.Root.Element("Encrypt").Value)) { //解密XML信息 string postDataStr = edocument.ToString(); WXBizMsgCrypt msgCrype = new WXBizMsgCrypt(postModel.Token, postModel.EncodingAESKey, postModel.AppId); string msgXml = null; int result = msgCrype.DecryptMsg(postModel.Msg_Signature, postModel.Timestamp, postModel.Nonce, postDataStr, ref msgXml); //判断result类型 if (result != 0) { return null; } return XDocument.Parse(msgXml);//完成解密 } return edocument; }
/// <summary> /// /// </summary> /// <param name="inputStream"></param> /// <param name="maxRecordCount"></param> /// <param name="postData">需要传入到Init的参数</param> public MessageHandler(Stream inputStream, int maxRecordCount = 0, object postData = null) { var postDataDocument = XmlUtility.Convert(inputStream); CommonInitialize(postDataDocument, maxRecordCount, postData); }
protected void Page_Load(object sender, EventArgs e) { string sToken = "zsyy94123qwe"; string sEncodingAESKey = "ugezVmmKMqlIV0DHK7DwxYUu0SRx56jxsHASe6qQowk"; string sCorpID = "wxe1afc2b45b386c79"; WXBizMsgCrypt wxcpt = new WXBizMsgCrypt(sToken, sEncodingAESKey, sCorpID); string sVerifyMsgSig = Request.QueryString["msg_signature"]; string sVerifyTimeStamp = Request.QueryString["timestamp"]; string sVerifyNonce = Request.QueryString["nonce"]; string sVerifyEchoStr = Request.QueryString["echostr"]; string replyEchoStr = ""; LogHelper log = new LogHelper(); if (Request.HttpMethod == "GET") { //get method - 仅在微信后台填写URL验证时触发 log.WriteLog("1"); replyEchoStr = Signature.VerifyURL(sToken, sEncodingAESKey, sCorpID, sVerifyMsgSig, sVerifyTimeStamp, sVerifyNonce, sVerifyEchoStr); if (replyEchoStr != null || replyEchoStr != "") { WriteContent(replyEchoStr); //返回字符串则表示验证通过 log.WriteLog("2"); } else { WriteContent("failed:" + replyEchoStr + "。" + "如果你在浏览器中看到这句话,说明此地址可以被作为微信企业号账号后台的Url,请注意保持Token一致。"); } Response.End(); } else { log.WriteLog("3:"); //post method - 当有用户想公众账号发送消息时触发 //本地测试的时候注释掉 ----start ----- //replyEchoStr = Signature.VerifyURL(sToken, sEncodingAESKey, sCorpID, sVerifyMsgSig, sVerifyTimeStamp, sVerifyNonce, sVerifyEchoStr); //if (replyEchoStr == null || replyEchoStr == "") //{ // WriteContent("参数错误!"); // return; //} //本地测试的时候注释掉 ----end ----- var maxRecordCount = 10; log.WriteLog("4:"); //自定义MessageHandler,对微信请求的详细判断操作都在这里面。 try { XDocument postData = XmlUtility.Convert(Request.InputStream); EncryptPostData PostData = Senparc.Weixin.QY.RequestMessageFactory.GetEncryptPostData(postData.ToString()); //获取签名 string sig = Signature.GenarateSinature(sToken, sVerifyTimeStamp, sVerifyNonce, PostData.Encrypt); //解密的明文信息 string sMsg = ""; int r = wxcpt.DecryptMsg(sig, sVerifyTimeStamp, sVerifyNonce, postData.ToString(), ref sMsg); log.WriteLog("r=:" + r); log.WriteLog("sMsg=:" + sMsg); IRequestMessageBase RequestMessageBase = RequestMessageFactory.GetRequestEntity(sMsg); log.WriteLog("MsgType=:" + RequestMessageBase.MsgType); PostModel postmodel = new PostModel(); postmodel.CorpId = sCorpID; postmodel.EncodingAESKey = sEncodingAESKey; postmodel.Msg_Signature = sVerifyMsgSig; postmodel.Nonce = sVerifyNonce; postmodel.Signature = sig; postmodel.Timestamp = sVerifyTimeStamp; postmodel.Token = sToken; CustomQYMessageHandler myMes = new CustomQYMessageHandler(postData, postmodel); myMes.Execute(); log.WriteLog("myMes.RequestDocument=:" + myMes.RequestDocument.ToString()); log.WriteLog("myMes.RequestMessage.FromUserName=:" + myMes.RequestMessage.FromUserName); myMes.DefaultResponseMessage(RequestMessageBase); //企业回复 //测试时可开启此记录,帮助跟踪数据,使用前请确保App_Data文件夹存在,且有读写权限。 //messageHandler.RequestDocument.Save( // Server.MapPath("~/App_Data/" + DateTime.Now.Ticks + "_Request_" + // messageHandler.RequestMessage.FromUserName + ".txt")); //执行微信处理过程 //messageHandler.Execute(); //测试时可开启,帮助跟踪数据 //messageHandler.ResponseDocument.Save( // Server.MapPath("~/App_Data/" + DateTime.Now.Ticks + "_Response_" + // messageHandler.ResponseMessage.ToUserName + ".txt")); //WriteContent(messageHandler.ResponseDocument.ToString()); return; } catch (Exception ex) { log.WriteLog(ex.ToString()); //using (TextWriter tw = new StreamWriter(Server.MapPath("~/App_Data/Error_" + DateTime.Now.Ticks + ".txt"))) //{ // tw.WriteLine(ex.Message); // tw.WriteLine(ex.InnerException.Message); // //if (messageHandler.ResponseDocument != null) // //{ // // tw.WriteLine(messageHandler.ResponseDocument.ToString()); // //} // tw.Flush(); // tw.Close(); //} WriteContent(""); } finally { Response.End(); } } }