首页 > 代码库 > 微信验证代码

微信验证代码

public partial class Weixin : System.Web.UI.Page    {        private string Token = "weixin";        //与那边填写的token一致        protected void Page_Load(object sender, EventArgs e)        {            if (Request.HttpMethod.ToLower() == "post")            {                ResMsg();            }            else            {                Valid();            }        }        /// <summary>        /// 验证微信签名        /// </summary>        /// * 将token、timestamp、nonce三个参数进行字典序排序        /// * 将三个参数字符串拼接成一个字符串进行sha1加密        /// * 开发者获得加密后的字符串可与signature对比,标识该请求来源于微信。        /// <returns></returns>        private bool CheckSignature()        {            string signature = Request.QueryString["signature"];            string timestamp = Request.QueryString["timestamp"];            string nonce = Request.QueryString["nonce"];            string[] ArrTmp = { Token, timestamp, nonce };            Array.Sort(ArrTmp);     //字典排序            string tmpStr = string.Join("", ArrTmp);            tmpStr = FormsAuthentication.HashPasswordForStoringInConfigFile(tmpStr, "SHA1");            tmpStr = tmpStr.ToLower();            if (tmpStr == signature)            {                return true;            }            else            {                return false;            }        }        private void Valid()        {            string echoStr = Request.QueryString["echoStr"];            if (CheckSignature())            {                if (!string.IsNullOrEmpty(echoStr))                {                    Response.Write(echoStr);                    Response.End();                }            }        }           }

微信验证代码