微信token验证

来源:互联网 发布:非洲男士防晒霜 知乎 编辑:程序博客网 时间:2024/05/18 01:37
 protected void Page_Load(object sender, EventArgs e)
        {


            if (!string.IsNullOrEmpty(Request["echoStr"]))
            {
                var echostr = Request["echoStr"];
                if (checkSignature() && !string.IsNullOrEmpty(echostr))
                {
                    Response.Write(echostr);//推送


                    Response.End();
                }
            }
        }
        public bool checkSignature()
        {
            var signature = Request["signature"];
            var timestamp = Request["timestamp"];
            var nonce = Request["nonce"];
            var token = "你的Token值";
            string[] ArrTmp = { token, timestamp, nonce };
            Array.Sort(ArrTmp);     //字典排序
            string tmpStr = string.Join("", ArrTmp);
            tmpStr = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(tmpStr, "SHA1");
            tmpStr = tmpStr.ToLower();
            if (tmpStr == signature)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
原创粉丝点击