微信token验证

                    //1.微信对该URL有效性验证(GET)
                    string token = ConfigurationManager.AppSettings["WeixinToken"];//从配置文件获取Token
                    string echostr = context.Request.QueryString["echostr"];
                    string signature = context.Request.QueryString["signature"];
                    string timestamp = context.Request.QueryString["timestamp"];
                    string nonce = context.Request.QueryString["nonce"];
                    List<string> tmpArr = new List<string>();
                    tmpArr.Add(token);
                    tmpArr.Add(timestamp);
                    tmpArr.Add(nonce);
                    tmpArr.Sort();
                    string tmpstr = string.Join("", tmpArr.ToArray());
                    SHA1 tmpsha = new SHA1CryptoServiceProvider();
                    byte[] data = System.Text.Encoding.ASCII.GetBytes(tmpstr);
                    byte[] tmpBytes = tmpsha.ComputeHash(data);
                    StringBuilder sb = new StringBuilder();
                    foreach (byte b in tmpBytes)
                    {
                        sb.Append(b.ToString("x2"));
                    }
                    if (signature == sb.ToString())
                    {
                        if (!string.IsNullOrEmpty(echostr))
                        {
                            context.Response.Write(echostr);
                            context.Response.End();
                        }
                    }

 

posted @ 2017-02-14 08:59  WangJunZzz  阅读(456)  评论(0编辑  收藏  举报