微信公众号开发者申请,Token验证


    /// <summary>
    /// Post 的摘要说明
    /// </summary>
    public class Post : IHttpHandler
    {
        WXCore m_wxCore = new WXCore();

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/xml;charset=utf-8";
            //context.Response.Write("Hello World"); 打印出这一句,会造成配置不成功
            if (context.Request.HttpMethod.ToLower() == "post")
            {

            }
            else
            {
                m_wxCore.Auth();
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
public void Auth()
        {
            string echoStr = HttpContext.Current.Request.QueryString["echostr"];
            if (CheckSignature()) //校验签名是否正确
            {
                if (!string.IsNullOrEmpty(echoStr))
                {
                    HttpContext.Current.Response.Write(echoStr); //返回原值表示校验成功
                    HttpContext.Current.Response.End();
                }
            }
        }

        /// <summary>
        /// 验证微信签名
        /// * 将token、timestamp、nonce三个参数进行字典序排序
        /// * 将三个参数字符串拼接成一个字符串进行sha1加密
        /// * 开发者获得加密后的字符串可与signature对比,标识该请求来源于微信。
        /// </summary>
        /// <returns></returns>
        private bool CheckSignature()
        {
            string signature = HttpContext.Current.Request.QueryString["signature"];
            string timestamp = HttpContext.Current.Request.QueryString["timestamp"];
            string nonce = HttpContext.Current.Request.QueryString["nonce"];
            //加密/校验流程:
            //1. 将token、timestamp、nonce三个参数进行字典序排序
            string[] ArrTmp = { ConfigHelper.Token, timestamp, nonce };
            Array.Sort(ArrTmp);//字典排序
            //2.将三个参数字符串拼接成一个字符串进行sha1加密
            string tmpStr = string.Join("", ArrTmp);
            tmpStr = FormsAuthentication.HashPasswordForStoringInConfigFile(tmpStr, "SHA1");
            tmpStr = tmpStr.ToLower();
            //3.开发者获得加密后的字符串可与signature对比,标识该请求来源于微信。
            if (tmpStr == signature)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值