公众号-接口配置信息 接口实现 netcore

[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;
        }

 

上一篇:.NetCore 3.1和.NetCore 5.0 中WebApi的入参端的校验方法


下一篇:Jenkins + Docker(.NetCore)