[Routing(EHttpMethod.HttpGet, "wx/api")] [MessageFormat(EMessageType.Original)] public string WechatValidation(string echostr, string signature, string timestamp, string nonce) { string[] ArrTmp = { "1234.com", timestamp, nonce }; //字典排序 Array.Sort(ArrTmp); string tmpStr = string.Join("", ArrTmp); //字符加密 var sha1 = HmacSha1Sign(tmpStr); if (sha1.Equals(signature)) { return echostr; } else { return null; } } /// <summary> /// HMAC-SHA1加密算法 /// </summary> /// <param name="str">加密字符串</param> /// <returns></returns> public string HmacSha1Sign(string str) { var sha1 = System.Security.Cryptography.SHA1.Create(); var hash = sha1.ComputeHash(Encoding.Default.GetBytes(str)); string byte2String = null; for (int i = 0; i < hash.Length; i++) { byte2String += hash[i].ToString("x2"); } return byte2String; }