2018-04-17 微信带参数的二维码

原始代码记录

 public ActionResult Index()
        {
            if (Request.HttpMethod.ToLower() == "post") {
                bb();
            }
            else
            {
                string echoStr = Request.QueryString["echoStr"];
                string token = Request.QueryString["token"];

                if (CheckSignature())
                {
                    if (!string.IsNullOrEmpty(echoStr))
                    {
                        Response.Write(echoStr);
                        Response.End();
                    }
                }
            }
            return View();
        }
public void bb() {
  
            //接收数据
            System.IO.StreamReader reader = new System.IO.StreamReader(Request.InputStream);
            String xmlData = reader.ReadToEnd();
            XElement xdoc = XElement.Parse(xmlData);

            //解析XML
            var toUserName = xdoc.Element("ToUserName").Value;
            var fromUserName = xdoc.Element("FromUserName").Value;
            var createTime = xdoc.Element("CreateTime").Value;
            string key= xdoc.Element("EventKey").Value;
            var content = xdoc.Element("Ticket").Value;
            DateTime datatime = DateTime.Now;
            if (!string.IsNullOrEmpty(key)) {
                
                AccessToken Token = wxApi.GetToken("wx0b024f521540d37e", "0bfcdde311c42067a1ece9eb2aa7ef48");
                userin User = wxApi.GetNickname(Token.Accesstoken, fromUserName);
                string a = User.Nickname;


                string createSql = "insert into TgCount values('" + key + "','" + createTime + "','" + a + "','" + toUserName + "','" + datatime + "')";
                using (DbOperator dboperator = ConnectionManager.CreateReadConnection())
                {
                    dboperator.ExecuteNonQuery(createSql, 30000);
                }

            }

            //int datetime = 1460541339;

            ////回复内容
            //StringBuilder resxml = new StringBuilder(
            //string.Format("{2}", fromUserName, toUserName, datetime));
            //resxml.AppendFormat("0", content);
            //string msg = resxml.ToString();


        }

  public bool CheckSignature()
        {
            string WeChat_Token = "test123";
            //  string WeChat_Key = Request.QueryString["key"];

            //  DataTable dtWeChat = wechatdal.GetList("wechat_key='" + WeChat_Key + "'").Tables[0];

            //if (dtWeChat.Rows.Count > 0)
            //{
            //    WeChat_Token = dtWeChat.Rows[0]["wechat_token"].ToString();
            //}

            //从微信服务器接收传递过来的数据
            string signature = Request.QueryString["signature"]; //微信加密签名
            string timestamp = Request.QueryString["timestamp"]; //时间戳
            string nonce = Request.QueryString["nonce"];//随机数
            string[] ArrTmp = { WeChat_Token, timestamp, nonce };
            Array.Sort(ArrTmp);     //字典排序
            string tmpStr = string.Join("", ArrTmp);//将三个字符串组成一个字符串
            tmpStr = FormsAuthentication.HashPasswordForStoringInConfigFile(tmpStr, "SHA1");//进行sha1加密
            tmpStr = tmpStr.ToLower();
            //加过密的字符串与微信发送的signature进行比较,一样则通过微信验证,否则失败。
            if (tmpStr == signature)
            {
                return true;
            }
            else
            {
                return false;
            }

        }

你可能感兴趣的:(2018-04-17 微信带参数的二维码)