PostInfo.aspx.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. using LYFZ.Weixin.SDK;
  2. using LYFZ.WXLibrary;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Data;
  6. using System.Linq;
  7. using System.Web;
  8. using System.Web.UI;
  9. using System.Web.UI.WebControls;
  10. namespace LYFZ.WeixinServers.WeiXinAPP
  11. {
  12. public partial class PostInfo : System.Web.UI.Page
  13. {
  14. string postinfoddomaine = "post";
  15. protected void Page_Load(object sender, EventArgs e)
  16. {
  17. /* if (!Global.LogsDongleDomainList.Contains(postinfoddomaine))
  18. {
  19. Global.LogsDongleDomainList.Add(postinfoddomaine);
  20. }*/
  21. try
  22. {
  23. string signature = Request["signature"];
  24. string timestamp = Request["timestamp"];// yyyyMMddhhmmss
  25. string nonce = Request["nonce"];
  26. if (Request.HttpMethod == "GET")
  27. {
  28. if (Request.QueryString["type"] == null)
  29. {
  30. Response.Write("如果你在浏览器中看到这句话,说明此地址可以接口的Url,请注意保持Token一致。" + LYFZ.Weixin.SDK.BasicAPI.GetSignature(timestamp, nonce, Global.lyfzToken));
  31. }
  32. }
  33. else
  34. {
  35. var ent = "";
  36. if (!CheckSignature(signature, timestamp, nonce, Global.lyfzToken, out ent))
  37. {
  38. CommonHandleClass.WriteLog("POST:验证失败,非法提交", postinfoddomaine, Global.LogsDongleDomainList);
  39. WriteContent("验证失败,非法提交!");
  40. return;
  41. }
  42. try
  43. {
  44. WriteContent(PostHandler());
  45. }
  46. catch (Exception ex)
  47. {
  48. CommonHandleClass.WriteLog("POST:出错,原因:" + ex.Message, postinfoddomaine, Global.LogsDongleDomainList);
  49. WriteContent("服务器处理出错误!原因:" + ex.Message);
  50. return;
  51. }
  52. }
  53. }
  54. catch { }
  55. finally
  56. {
  57. Response.End();
  58. }
  59. }
  60. #region 获取web客户端ip
  61. /// <summary>
  62. /// 获取web客户端ip
  63. /// </summary>
  64. /// <returns></returns>
  65. public static string GetWebClientIp()
  66. {
  67. string userIP = "0.0.0.0";
  68. try
  69. {
  70. if (System.Web.HttpContext.Current == null
  71. || System.Web.HttpContext.Current.Request == null
  72. || System.Web.HttpContext.Current.Request.ServerVariables == null)
  73. return "";
  74. string CustomerIP = "";
  75. //CDN加速后取到的IP simone 090805
  76. CustomerIP = System.Web.HttpContext.Current.Request.Headers["Cdn-Src-Ip"];
  77. if (!string.IsNullOrEmpty(CustomerIP))
  78. {
  79. return CustomerIP;
  80. }
  81. CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
  82. if (!String.IsNullOrEmpty(CustomerIP))
  83. return CustomerIP;
  84. if (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_VIA"] != null)
  85. {
  86. CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
  87. if (CustomerIP == null)
  88. CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
  89. }
  90. else
  91. {
  92. CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
  93. }
  94. if (string.Compare(CustomerIP, "unknown", true) == 0)
  95. return System.Web.HttpContext.Current.Request.UserHostAddress;
  96. return CustomerIP;
  97. }
  98. catch { }
  99. return userIP;
  100. }
  101. #endregion
  102. bool CheckSignature(string signature, string timestamp, string nonce, string token, out string ent)
  103. {
  104. // if (Convert.ToInt64(DateTime.Now.ToString("yyyyMMddhhmmss")) - Convert.ToInt64(timestamp) > 200)
  105. // {
  106. return BasicAPI.CheckSignature(signature, timestamp, nonce, token, out ent);
  107. // }
  108. // else {
  109. // ent = "签名已过期";
  110. // return false;
  111. // }
  112. }
  113. LYFZ.WeixinServiceDate.DAL.DAL_CustomerInterfaces cusDal = new WeixinServiceDate.DAL.DAL_CustomerInterfaces();
  114. string PostHandler()
  115. {
  116. string retMsg = "Set Success";
  117. if (Request.Form["newPost"] != null && Request.Form["newPost"].ToString() == "1")
  118. {
  119. try
  120. {
  121. LYFZ.WeixinServiceDate.Model.Model_CustomerInterfaces cusModel = new WeixinServiceDate.Model.Model_CustomerInterfaces();
  122. string JMGDomainName = "";
  123. string ShellDomainName = "";
  124. string IPAddress = "";
  125. int PortNumber = 0;
  126. int UseType = 0;
  127. string CompanyName = "";
  128. // string ThirdPartyPlatformURL = "";
  129. string myToken = "";
  130. string EncodingAESKey = "";
  131. string AppID = "";
  132. string AppSecret = "";
  133. try
  134. {
  135. JMGDomainName = System.Web.HttpUtility.UrlDecode(Request.Form["JMGDomainName"].ToString().Trim());
  136. ShellDomainName = System.Web.HttpUtility.UrlDecode(Request.Form["ShellDomainName"].ToString().Trim());
  137. IPAddress = Request.Form["IPAddress"].ToString().Trim();
  138. // PortNumber = Convert.ToInt32(Request.Form["PortNumber"].ToString());
  139. // UseType = 0;// Convert.ToInt32(Request.Form["UseType"].ToString());
  140. CompanyName = System.Web.HttpUtility.UrlDecode(Request.Form["CompanyName"].ToString()).Trim();
  141. // ThirdPartyPlatformURL = "";//LYFZ.WinAPI.SDKSecurity.Decode(Request.Form["ThirdPartyPlatformURL"].ToString().Trim());
  142. myToken = Request.Form["Token"].ToString().Trim();
  143. // EncodingAESKey = "";//Request.Form["EncodingAESKey"].ToString().Trim();
  144. AppID = Request.Form["AppID"].ToString().Trim();
  145. AppSecret = Request.Form["AppSecret"].ToString().Trim();
  146. }
  147. catch
  148. {
  149. CommonHandleClass.WriteLog("域名信息更新提交失败,参数不完整或格式不正确", postinfoddomaine, Global.LogsDongleDomainList);
  150. return System.Web.HttpUtility.UrlEncode("Exception:参数不正确");
  151. }
  152. if (JMGDomainName.Trim().Length > 0)
  153. {
  154. cusModel = Global.GetCustomerModel(JMGDomainName);
  155. if (cusModel != null && cusModel.ID > 0)
  156. {
  157. try
  158. {
  159. cusModel.ShellDomainName = ShellDomainName;
  160. cusModel.IPAddress = IPAddress;
  161. cusModel.PortNumber = 0;
  162. cusModel.UseType = 0;
  163. cusModel.CompanyName = CompanyName;
  164. cusModel.RefreshTime = DateTime.Now;
  165. cusModel.ThirdPartyPlatformURL = "";
  166. cusModel.Token = myToken;
  167. cusModel.AppID = AppID;
  168. cusModel.AppSecret = AppSecret;
  169. cusModel.EncodingAESKey = EncodingAESKey;
  170. cusModel.ISEnabled = 1;
  171. if (cusDal.Update(cusModel))
  172. {
  173. cusModel = (LYFZ.WeixinServiceDate.Model.Model_CustomerInterfaces)cusDal.GetModelObject("JMGDomainName", JMGDomainName);
  174. Global.UpdateCustInfo(JMGDomainName, cusModel);
  175. try
  176. {
  177. Global.UpdateAuthorizedAPPIDHandleJMGDomainName(cusModel.AppID, cusModel.JMGDomainName, cusModel.CompanyName);
  178. }
  179. catch (Exception ex)
  180. {
  181. CommonHandleClass.WriteLog("域名为:" + JMGDomainName + " 的的客户更新授权处理对象加密锁域名时出错:" + ex.Message, postinfoddomaine, Global.LogsDongleDomainList);
  182. }
  183. CommonHandleClass.WriteLog("域名为:" + JMGDomainName + " 的信息更新提交成功", postinfoddomaine, Global.LogsDongleDomainList);
  184. }
  185. else
  186. {
  187. retMsg = "Exception:Refresh Failed";
  188. CommonHandleClass.WriteLog("域名为:" + JMGDomainName + " 的信息更新提交失败,数据库更新记录时失败", postinfoddomaine, Global.LogsDongleDomainList);
  189. }
  190. }
  191. catch (Exception ex)
  192. {
  193. retMsg = "Exception:" + ex.Message;
  194. CommonHandleClass.WriteLog("域名为:" + JMGDomainName + " 的信息更新提交失败,错误信息为:" + ex.Message, postinfoddomaine, Global.LogsDongleDomainList);
  195. Global.RefreshLoadCustDaTable();
  196. }
  197. }
  198. else
  199. {
  200. try
  201. {
  202. cusModel.JMGDomainName = JMGDomainName;
  203. cusModel.ShellDomainName = ShellDomainName;
  204. cusModel.IPAddress = IPAddress;
  205. cusModel.PortNumber = PortNumber;
  206. cusModel.UseType = Convert.ToInt32(UseType);
  207. cusModel.CompanyName = CompanyName;
  208. cusModel.CallFrequency = 0;
  209. if (cusModel.ThirdPartyPlatformURL.Trim().Length <= 5)
  210. {
  211. cusModel.ThirdPartyPlatformURL = "";
  212. }
  213. cusModel.Token = myToken;
  214. cusModel.AppID = AppID;
  215. cusModel.AppSecret = AppSecret;
  216. cusModel.EncodingAESKey = EncodingAESKey;
  217. cusModel.ISEnabled = 1;
  218. if (cusDal.Add(cusModel))
  219. {
  220. cusModel = (LYFZ.WeixinServiceDate.Model.Model_CustomerInterfaces)cusDal.GetModelObject("JMGDomainName", JMGDomainName);
  221. Global.UpdateCustInfo(JMGDomainName, cusModel);
  222. // retMsg += ":" + IPAddress;
  223. CommonHandleClass.WriteLog("域名为:" + JMGDomainName + " 的信息提交成功", postinfoddomaine, Global.LogsDongleDomainList);
  224. }
  225. else
  226. {
  227. retMsg = "Exception:Add Failed";
  228. CommonHandleClass.WriteLog("域名为:" + JMGDomainName + " 的信息提交失败,数据库添加记录时失败", postinfoddomaine, Global.LogsDongleDomainList);
  229. }
  230. }
  231. catch (Exception ex)
  232. {
  233. retMsg = "Exception:" + ex.Message;
  234. CommonHandleClass.WriteLog("域名为:" + JMGDomainName + " 的信息提交失败,错误信息为:" + ex.Message, postinfoddomaine, Global.LogsDongleDomainList);
  235. Global.RefreshLoadCustDaTable();
  236. }
  237. }
  238. }
  239. else
  240. {
  241. retMsg = "Exception:JMG domain name not be null";
  242. CommonHandleClass.WriteLog("域名为:" + JMGDomainName + " 的信息提交失败,错误信息为:加密狗域名不能为空", postinfoddomaine, Global.LogsDongleDomainList);
  243. }
  244. }
  245. catch (Exception ex)
  246. {
  247. retMsg = "Exception:" + ex.Message;
  248. CommonHandleClass.WriteLog("域名信息提交失败,错误信息为:" + ex.Message, postinfoddomaine, Global.LogsDongleDomainList);
  249. }
  250. }
  251. return System.Web.HttpUtility.UrlEncode(retMsg);
  252. }
  253. private void WriteContent(string str)
  254. {
  255. Response.Output.Write(str);
  256. }
  257. }
  258. }