/// <summary> /// 微信验证 /// </summary> /// <param name="echostr"></param> /// <param name="signature"></param> /// <param name="timestamp"></param> /// <param name="nonce"></param> /// <returns></returns> public IActionResult WeChatSign(string echostr,string signature,string timestamp,string nonce) { string _token = "HanLiKongGu"; if (CheckSignAture(_token, timestamp, nonce, signature)) { if (!string.IsNullOrEmpty(echostr)) { return Content(echostr); } } return Content("Error"); } /// <summary> /// 验证签名是否一致 /// </summary> /// <param name="token">微信平台设置的口令</param> /// <param name="timestamp">时间戳</param> /// <param name="nonce">随机数</param> /// <param name="signature">微信加密签名</param> /// <returns></returns> public bool CheckSignAture(string token, string timestamp, string nonce, string signature) { string[] strs = new string[] { token, timestamp, nonce };//把参数放到数组 Array.Sort(strs);//加密/校验流程1、数组排序 string sign = string.Join("", strs); sign = GetSHA1Str(sign); if (sign == signature) { return true; } else { return false; } } /// <summary> /// SHA1加密方法 /// </summary> /// <param name="str">需要加密的字符串</param> /// <returns></returns> public string GetSHA1Str(string str) { byte[] _byte = Encoding.Default.GetBytes(str); HashAlgorithm ha = new SHA1CryptoServiceProvider(); _byte = ha.ComputeHash(_byte); StringBuilder sha1Str = new StringBuilder(); foreach (byte b in _byte) { sha1Str.AppendFormat("{0:x2}", b); } return sha1Str.ToString(); }
推荐文章:https://www.cnblogs.com/HelloMyWorld/p/6380081.html
http://blog.csdn.net/inforstack/article/details/47780275