/// <summary> /// 授权事件接收URL /// </summary> /// <returns></returns> public ActionResult Notice(PostModel postModel) { var logPath = Server.MapPath(string.Format("~/App_Data/Open/{0}/", DateTime.Now.ToString("yyyy-MM-dd"))); if (!Directory.Exists(logPath)) { Directory.CreateDirectory(logPath); } try { var messageHandler = new CustomThirdPartyMessageHandler(Request.InputStream, postModel);//初始化 //记录RequestMessage日志(可选) messageHandler.RequestDocument.Save(Path.Combine(logPath, string.Format("{0}_Request_{1}.txt", DateTime.Now.Ticks, messageHandler.RequestMessage.AppId))); messageHandler.Execute();//执行 //记录ResponseMessage日志(可选) using (TextWriter tw = new StreamWriter(Path.Combine(logPath, string.Format("{0}_Response_{1}.txt", DateTime.Now.Ticks, messageHandler.RequestMessage.AppId)))) { tw.WriteLine(messageHandler.ResponseMessageText); tw.Flush(); tw.Close(); } return Content(messageHandler.ResponseMessageText); } catch (Exception ex) { return Content("error:" + ex.Message); } }
public ThirdPartyMessageHandler(XDocument ecryptRequestDocument, PostModel postModel = null) { _postModel = postModel; EcryptRequestDocument = ecryptRequestDocument;//原始加密XML转成XDocument Init(); }
//<?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; RequestInfoType infoType; try { infoType = InfoTypeHelper.GetRequestInfoType(doc); switch (infoType) { case RequestInfoType.component_verify_ticket: requestMessage = new RequestMessageComponentVerifyTicket(); break; case RequestInfoType.unauthorized: requestMessage = new RequestMessageUnauthorized(); break; default: throw new UnknownRequestMsgTypeException(string.Format("InfoType:{0} 在RequestMessageFactory中没有对应的处理程序!", infoType), new ArgumentOutOfRangeException());//为了能够对类型变动最大程度容错(如微信目前还可以对公众账号suscribe等未知类型,但API没有开放),建议在使用的时候catch这个异常 } EntityHelper.FillEntityWithXml(requestMessage, doc); } catch (ArgumentException ex) { throw new WeixinException(string.Format("RequestMessage转换出错!可能是InfoType不存在!,XML:{0}", doc.ToString()), ex); } return requestMessage; }
public ThirdPartyMessageHandler(Stream inputStream, PostModel postModel = null) { _postModel = postModel; EcryptRequestDocument = XmlUtility.XmlUtility.Convert(inputStream);//原始加密XML转成XDocument Init(); }
public void TicketMessageHandlerTest() { string xml = @"<xml> <AppId><![CDATA[wxbbd3f07e2945cf2a]]></AppId> <Encrypt><![CDATA[/RWeOUN4J839ynYH960Duj2k0WwvSfwq2dBFQ1NG78v9CZKbiCk5F8Fq/RGV0oFALoy+2L7KDe+1EbitZK7T4KtHMxntcEeQDxhWGdbeyTYJStcl7pudKA/ltfPP2nDbMrvmdl6JAX/XY4El0XEaYMrxegU3B1aUMZJ1GDjz9pIFv5+DPWj2mf7mhwIKcJSfeMctifS5UyJHKmIM+gnPQjtcKsVdnjaLckEScUsKKTygeG9IOtWFfE720W6g5UfOg3yxPFCUrqjWKRTue7g0yT0vQyb0L5cODKh1bCjIRcgikJsRVo3tAX1QO/CrHzKEEDqaD3Rx1hRkvRl/2KQlFd/DkhOzWp1LmpsCZQiNj0Fdc41aVleaFrwUQ8svg9Wt2iJLDhdqlz1Us2Pb6Ayx3dJLhXtb4ynJGsPFq0N8RrCuLgU391BpAiga5JBcjzS50pfvBBEEPQDyAE7znPphrA==]]></Encrypt> </xml>"; var postModel = new PostModel() { AppId = sAppID, Msg_Signature = "066523677e42a15c64a725ba0058cd7651174624", Signature = "60d1ddec039f51cc8c7ee4ea9ae9a479dd7c7d01", Timestamp = "1436881222", Nonce = "296318614", Token = sToken, EncodingAESKey = sEncodingAESKey }; var messageHandler = new CustomMessageHandler(XDocument.Parse(xml), postModel); messageHandler.Execute(); //TestMessageHandlers中没有处理坐标信息的重写方法,将返回默认消息 Assert.IsInstanceOfType(messageHandler.ResponseMessageText, typeof(String)); Assert.AreEqual("success", messageHandler.ResponseMessageText); Console.WriteLine(messageHandler.RequestDocument.ToString()); Assert.IsInstanceOfType(messageHandler.RequestMessage, typeof(RequestMessageComponentVerifyTicket)); }
//<?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; RequestInfoType infoType; try { infoType = InfoTypeHelper.GetRequestInfoType(doc); switch (infoType) { case RequestInfoType.component_verify_ticket: requestMessage = new RequestMessageComponentVerifyTicket(); break; case RequestInfoType.unauthorized: /* * <xml> <AppId>第三方平台appid</AppId> <CreateTime>1413192760</CreateTime> <InfoType>unauthorized</InfoType> <AuthorizerAppid>公众号appid</AuthorizerAppid> </xml> */ requestMessage = new RequestMessageUnauthorized(); break; case RequestInfoType.authorized: /* <xml> <AppId>第三方平台appid</AppId> <CreateTime>1413192760</CreateTime> <InfoType>authorized</InfoType> <AuthorizerAppid>公众号appid</AuthorizerAppid> <AuthorizationCode>授权码(code)</AuthorizationCode> <AuthorizationCodeExpiredTime>过期时间</AuthorizationCodeExpiredTime> </xml> */ requestMessage = new RequestMessageAuthorized(); break; case RequestInfoType.updateauthorized: requestMessage = new RequestMessageUpdateAuthorized(); break; default: throw new UnknownRequestMsgTypeException(string.Format("InfoType:{0} 在RequestMessageFactory中没有对应的处理程序!", infoType), new ArgumentOutOfRangeException());//为了能够对类型变动最大程度容错(如微信目前还可以对公众账号suscribe等未知类型,但API没有开放),建议在使用的时候catch这个异常 } EntityHelper.FillEntityWithXml(requestMessage, doc); } catch (ArgumentException ex) { throw new WeixinException(string.Format("RequestMessage转换出错!可能是InfoType不存在!,XML:{0}", doc.ToString()), ex); } return requestMessage; }
public ActionResult Notice(PostModel postModel) { var logPath = Server.MapPath(string.Format("~/App_Data/Open/{0}/", DateTime.Now.ToString("yyyy-MM-dd"))); if (!Directory.Exists(logPath)) { Directory.CreateDirectory(logPath); } //using (TextWriter tw = new StreamWriter(Path.Combine(logPath, string.Format("{0}_RequestStream.txt", DateTime.Now.Ticks)))) //{ // using (var sr = new StreamReader(Request.InputStream)) // { // tw.WriteLine(sr.ReadToEnd()); // tw.Flush(); // } //} //Request.InputStream.Seek(0, SeekOrigin.Begin); try { postModel.Token = component_Token; postModel.EncodingAESKey = component_EncodingAESKey;//根据自己后台的设置保持一致 postModel.AppId = component_AppId;//根据自己后台的设置保持一致 var messageHandler = new CustomThirdPartyMessageHandler(Request.InputStream, postModel);//初始化 //注意:再进行“全网发布”时使用上面的CustomThirdPartyMessageHandler,发布完成之后使用正常的自定义的MessageHandler,例如下面一行。 //var messageHandler = new CommonService.CustomMessageHandler.CustomMessageHandler(Request.InputStream, // postModel, 10); //记录RequestMessage日志(可选) //messageHandler.EcryptRequestDocument.Save(Path.Combine(logPath, string.Format("{0}_Request.txt", DateTime.Now.Ticks))); messageHandler.RequestDocument.Save(Path.Combine(logPath, string.Format("{0}_Request_{1}.txt", DateTime.Now.Ticks, messageHandler.RequestMessage.AppId))); messageHandler.Execute();//执行 //记录ResponseMessage日志(可选) using (TextWriter tw = new StreamWriter(Path.Combine(logPath, string.Format("{0}_Response_{1}.txt", DateTime.Now.Ticks, messageHandler.RequestMessage.AppId)))) { tw.WriteLine(messageHandler.ResponseMessageText); tw.Flush(); tw.Close(); } return Content(messageHandler.ResponseMessageText); } catch (Exception ex) { throw; return Content("error:" + ex.Message); } }
public void CustomerMessageHandlerTest() { //string sReqData = "<xml><ToUserName><![CDATA[wx5823bf96d3bd56c7]]></ToUserName><Encrypt><![CDATA[RypEvHKD8QQKFhvQ6QleEB4J58tiPdvo+rtK1I9qca6aM/wvqnLSV5zEPeusUiX5L5X/0lWfrf0QADHHhGd3QczcdCUpj911L3vg3W/sYYvuJTs3TUUkSUXxaccAS0qhxchrRYt66wiSpGLYL42aM6A8dTT+6k4aSknmPj48kzJs8qLjvd4Xgpue06DOdnLxAUHzM6+kDZ+HMZfJYuR+LtwGc2hgf5gsijff0ekUNXZiqATP7PF5mZxZ3Izoun1s4zG4LUMnvw2r+KqCKIw+3IQH03v+BCA9nMELNqbSf6tiWSrXJB3LAVGUcallcrw8V2t9EL4EhzJWrQUax5wLVMNS0+rUPA3k22Ncx4XXZS9o0MBH27Bo6BpNelZpS+/uh9KsNlY6bHCmJU9p8g7m3fVKn28H3KDYA5Pl/T8Z1ptDAVe0lXdQ2YoyyH2uyPIGHBZZIs2pDBS8R07+qN+E7Q==]]></Encrypt></xml>"; var postModel = new PostModel() { AppId = sAppID, Msg_Signature = sReqMsgSig, //Signature = sReqMsgSig, Timestamp = sReqTimeStamp, Nonce = sReqNonce, Token = sToken, EncodingAESKey = sEncodingAESKey }; var messageHandler = new CustomMessageHandler(XDocument.Parse(requestXML), postModel); messageHandler.Execute(); //TestMessageHandlers中没有处理坐标信息的重写方法,将返回默认消息 Assert.IsInstanceOfType(messageHandler.ResponseMessageText, typeof(String)); Assert.AreEqual("success。", messageHandler.ResponseMessageText); }
public void UnAuthMessageHandlerTest() { var postModel = new PostModel() { AppId = sAppID, Msg_Signature = sReqMsgSig, Signature = sReqSig, Timestamp = sReqTimeStamp, Nonce = sReqNonce, Token = sToken, EncodingAESKey = sEncodingAESKey }; var messageHandler = new CustomMessageHandler(XDocument.Parse(requestXML), postModel); messageHandler.Execute(); //TestMessageHandlers中没有处理坐标信息的重写方法,将返回默认消息 Assert.IsInstanceOfType(messageHandler.ResponseMessageText, typeof(String)); Assert.IsInstanceOfType(messageHandler.RequestMessage, typeof(RequestMessageUnauthorized)); Assert.AreEqual("success", messageHandler.ResponseMessageText); Console.WriteLine(messageHandler.RequestDocument.ToString()); }
public CustomThirdPartyMessageHandler(Stream inputStream, PostModel encryptPostModel) : base(inputStream, encryptPostModel) { }
public CustomMessageHandler(XDocument ecryptRequestDocument, PostModel postModel = null) : base(ecryptRequestDocument, postModel) { }