微博公众平台(二)-- Token验证代码

本文详细介绍了微信公众号接入时的签名验证机制,包括如何生成签名、验证签名以及相关参数的理解,帮助开发者确保安全地完成公众号的对接过程。

Token,验证逻辑:
1、将Token、timestamp、nonce放入数组

2、将数组从小到大排列

3、将数组按顺序拼装成一个字符串

4、对生成的字符串进行SHA1加密

5、将密文转换为小写

6、将signature与最后生成的小写密文进行对比,如匹配则返回echostr

 

protected void Page_Load(object sender, EventArgs e)
        {
            string signature = Request["signature"];
            string timestamp = Request["timestamp"];

            string nonce = Request["nonce"];
            string echostr = Request["echostr"];

            string strSignature = GetSignature("Token", timestamp, nonce);

            if (strSignature == signature)
            {
                Response.Clear();
                Response.Write(echostr);
                Response.End();
            }
            else
            {
                Response.Clear();
                Response.Write("Error");
                Response.End();
            }
        }

        string GetSignature(string token, string timestamp, string nonce)
        {
            string[] strs = new string[] { token, timestamp, nonce };

            Array.Sort(strs);

            return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(String.Join("", strs), "SHA1").ToLower();
        }

 

转载于:https://www.cnblogs.com/comsokey/p/WeiXin2.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值