CheckSignature.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #region Apache License Version 2.0
  2. /*----------------------------------------------------------------
  3. Copyright 2019 Jeffrey Su & Suzhou Senparc Network Technology Co.,Ltd.
  4. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
  5. except in compliance with the License. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software distributed under the
  8. License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
  9. either express or implied. See the License for the specific language governing permissions
  10. and limitations under the License.
  11. Detail: https://github.com/JeffreySu/WeiXinMPSDK/blob/master/license.md
  12. ----------------------------------------------------------------*/
  13. #endregion Apache License Version 2.0
  14. /*----------------------------------------------------------------
  15. Copyright (C) 2019 Senparc
  16. 文件名:CheckSignature.cs
  17. 文件功能描述:检测签名
  18. 创建标识:Senparc - 20150211
  19. 修改标识:Senparc - 20150303
  20. 修改描述:整理接口
  21. 修改标识:Senparc - 20151005
  22. 修改描述:v13.3.1 提供带PostModel参数的方法
  23. 修改标识:Senparc - 20151005
  24. 修改描述:v13.8.7 fixbug:Check(string signature, PostModel postModel)方法调用错误
  25. ----------------------------------------------------------------*/
  26. using System.Linq;
  27. using System.Security.Cryptography;
  28. using System.Text;
  29. using Senparc.Weixin.MP.Entities.Request;
  30. //using System.Web.Security;
  31. namespace Senparc.Weixin.MP
  32. {
  33. /// <summary>
  34. /// 签名验证类
  35. /// </summary>
  36. public class CheckSignature
  37. {
  38. /// <summary>
  39. /// 在网站没有提供Token(或传入为null)的情况下的默认Token,建议在网站中进行配置。
  40. /// </summary>
  41. public const string Token = "weixin";
  42. /// <summary>
  43. /// 检查签名是否正确
  44. /// </summary>
  45. /// <param name="signature"></param>
  46. /// <param name="postModel">需要提供:Timestamp、Nonce、Token</param>
  47. /// <returns></returns>
  48. public static bool Check(string signature, PostModel postModel)
  49. {
  50. return Check(signature, postModel.Timestamp, postModel.Nonce, postModel.Token);
  51. }
  52. /// <summary>
  53. /// 检查签名是否正确
  54. /// </summary>
  55. /// <param name="signature"></param>
  56. /// <param name="timestamp"></param>
  57. /// <param name="nonce"></param>
  58. /// <param name="token"></param>
  59. /// <returns></returns>
  60. public static bool Check(string signature, string timestamp, string nonce, string token = null)
  61. {
  62. return signature == GetSignature(timestamp, nonce, token);
  63. }
  64. /// <summary>
  65. /// 返回正确的签名
  66. /// </summary>
  67. /// <param name="postModel">需要提供:Timestamp、Nonce、Token</param>
  68. /// <returns></returns>
  69. public static string GetSignature(PostModel postModel)
  70. {
  71. return GetSignature(postModel.Timestamp, postModel.Nonce, postModel.Token);
  72. }
  73. /// <summary>
  74. /// 返回正确的签名
  75. /// </summary>
  76. /// <param name="timestamp"></param>
  77. /// <param name="nonce"></param>
  78. /// <param name="token"></param>
  79. /// <returns></returns>
  80. public static string GetSignature(string timestamp, string nonce, string token = null)
  81. {
  82. token = token ?? Token;
  83. var arr = new[] { token, timestamp, nonce }.OrderBy(z => z).ToArray();
  84. var arrString = string.Join("", arr);
  85. //var enText = FormsAuthentication.HashPasswordForStoringInConfigFile(arrString, "SHA1");//使用System.Web.Security程序集
  86. var sha1 = SHA1.Create();
  87. var sha1Arr = sha1.ComputeHash(Encoding.UTF8.GetBytes(arrString));
  88. StringBuilder enText = new StringBuilder();
  89. foreach (var b in sha1Arr)
  90. {
  91. enText.AppendFormat("{0:x2}", b);
  92. }
  93. return enText.ToString();
  94. }
  95. }
  96. }