WXValidController.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using Microsoft.AspNetCore.Mvc;
  6. using Microsoft.Extensions.Logging;
  7. using ZcPeng.weixin.PublicAccount;
  8. using System.IO;
  9. using System.Text;
  10. using Common.Wechat;
  11. using Microsoft.AspNetCore.Http;
  12. namespace SupplierWeb.Controllers
  13. {
  14. [Route("api/WXValid")]
  15. public class WXValidController : Controller
  16. {
  17. private readonly ILogger<WXValidController> _logger;
  18. public WXValidController(ILoggerFactory loggerFactory)
  19. {
  20. _logger = loggerFactory.CreateLogger<WXValidController>();
  21. }
  22. public string Token = "Token";
  23. /// <summary>
  24. /// 验证接口
  25. /// </summary>
  26. /// <param name="signature">签名</param>
  27. /// <param name="timestamp">时间戳</param>
  28. /// <param name="nonce"></param>
  29. /// <param name="echostr"></param>
  30. /// <returns></returns>
  31. [HttpGet]
  32. //[AllowAnonymous]
  33. public ActionResult Index(string echostr, string signature, string timestamp,string nonce)
  34. {
  35. _logger.LogInformation($"echostr:{echostr}, signature:{signature}, timestamp:{timestamp}, nonce:{nonce}");
  36. if (CheckSignature(signature, timestamp, nonce, Token))
  37. {
  38. return Content(echostr);
  39. }
  40. return Content("error");
  41. }
  42. /// <summary>
  43. /// 接收消息并处理和返回相应结果
  44. /// </summary>
  45. /// <param name="msg_signature">当加密模式时才会有该变量(消息签名)</param>
  46. /// <param name="signature">签名</param>
  47. /// <param name="timestamp">时间戳</param>
  48. /// <param name="nonce"></param>
  49. /// <returns></returns>
  50. [HttpPost]
  51. //[AllowAnonymous]
  52. public ActionResult MessagePost(string msg_signature, string signature, string timestamp, string nonce)
  53. {
  54. try
  55. {
  56. //if (!new SecurityHelper().CheckSignature(signature, timestamp, nonce, _settings.Value.Token))
  57. //{
  58. // return Content(null);
  59. //}
  60. using (Stream stream = HttpContext.Request.Body)
  61. {
  62. byte[] buffer = new byte[HttpContext.Request.ContentLength.Value];
  63. stream.Read(buffer, 0, buffer.Length);
  64. string content = Encoding.UTF8.GetString(buffer);
  65. /*if (!string.IsNullOrWhiteSpace(msg_signature)) // 消息加密模式
  66. {
  67. string decryptMsg = string.Empty;
  68. var wxBizMsgCrypt = new WXBizMsgCrypt(_settings.Value.Token, _settings.Value.EncodingAESKey, _settings.Value.AppId);
  69. int decryptResult = wxBizMsgCrypt.DecryptMsg(msg_signature, timestamp, nonce, content, ref decryptMsg);
  70. if (decryptResult == 0 && !string.IsNullOrWhiteSpace(decryptMsg))
  71. {
  72. string resultMsg = new WechatMessageHelper().MessageResult(decryptMsg);
  73. string sEncryptMsg = string.Empty;
  74. if (!string.IsNullOrWhiteSpace(resultMsg))
  75. {
  76. int encryptResult = wxBizMsgCrypt.EncryptMsg(resultMsg, timestamp, nonce, ref sEncryptMsg);
  77. if (encryptResult == 0 && !string.IsNullOrWhiteSpace(sEncryptMsg))
  78. {
  79. return Content(sEncryptMsg);
  80. }
  81. }
  82. }
  83. }
  84. else // 消息未加密码处理
  85. {*/
  86. string resultMsg = string.Empty;
  87. RequestMessageHelper helper = new RequestMessageHelper(content, ref resultMsg);
  88. return Content(resultMsg);
  89. /*}
  90. return Content(null);*/
  91. }
  92. }
  93. catch (Exception ex)
  94. {
  95. _logger.LogError("接收消息并处理和返回相应结果异常:", ex);
  96. return Content(null);
  97. }
  98. }
  99. //////////////////////////////////////////
  100. /// <summary>
  101. /// 要与基本配置中的Token一致
  102. /// </summary>
  103. //PRotected void Page_Load(string echostr, string signature, string timestamp, string nonce)
  104. //{
  105. // //Log.Debug("Token", "测试输出: echoStr = " + echoStr);
  106. // if (CheckSignature() && !string.IsNullOrEmpty(echoStr))
  107. // {
  108. // Response.Write(echoStr);
  109. // Response.End();
  110. // }
  111. //}
  112. /// <summary>
  113. /// 验证微信签名
  114. /// </summary>
  115. /// * 将token、timestamp、nonce三个参数进行字典序排序
  116. /// * 将三个参数字符串拼接成一个字符串进行sha1加密
  117. /// * 开发者获得加密后的字符串可与signature对比,标识该请求来源于微信。
  118. /// <returns></returns>
  119. private bool CheckSignature(string msg_signature, string signature, string timestamp, string nonce)
  120. {
  121. //Log.Debug("Token", "测试输出: signature = " + signature);
  122. //Log.Debug("Token", "测试输出: timestamp = " + timestamp);
  123. //Log.Debug("Token", "测试输出: nonce = " + nonce);
  124. string[] arrTmp = { Token, timestamp, nonce };
  125. Array.Sort(arrTmp);
  126. string tmpStr = string.Join("", arrTmp);
  127. //tmpStr = System.Web.Security.FormsAuthentication.HashPassWordForStoringInConfigFile(tmpStr, "SHA1");
  128. //if (tmpStr != null)
  129. //{
  130. // tmpStr = tmpStr.ToLower();
  131. // return tmpStr == signature;
  132. //}
  133. return true;
  134. //return false;
  135. }
  136. }
  137. }