예제 #1
0
        public void Index()
        {
            string echoString = HttpContext.Current.Request.QueryString["echoStr"];
            string signature  = HttpContext.Current.Request.QueryString["signature"];
            string timestamp  = HttpContext.Current.Request.QueryString["timestamp"];
            string nonce      = HttpContext.Current.Request.QueryString["nonce"];

            //WriteLog("echoString:" + echoString + "---signature:" + signature + "---timestamp:" + timestamp + "---nonce:" + nonce);
            if (string.IsNullOrEmpty(echoString))
            {
                if (WeChatHelper.CheckSignature(signature, timestamp, nonce))
                {
                    string postString = string.Empty;
                    if (HttpContext.Current.Request.HttpMethod.ToUpper() == "POST")
                    {
                        //读取流消息
                        using (Stream stream = HttpContext.Current.Request.InputStream)
                        {
                            Byte[] postBytes = new Byte[stream.Length];
                            stream.Read(postBytes, 0, (Int32)stream.Length);
                            postString = Encoding.UTF8.GetString(postBytes);
                            MessageHelp help = new MessageHelp();
                            help.ToKen = GetToken();
                            string responseContent = string.Empty;
                            //判断是否加密
                            bool IsAes = HttpContext.Current.Request.QueryString["encrypt_type"] == "aes" ? true : false;
                            if (IsAes)
                            {
                                string        msg_signature = HttpContext.Current.Request.QueryString["msg_signature"];
                                WXBizMsgCrypt wmc           = new WXBizMsgCrypt(WeChatConfig.Token, WeChatConfig.EncodingAESKey, WeChatConfig.AppId);
                                string        decmsg        = string.Empty; //解密后
                                int           decnum        = wmc.DecryptMsg(msg_signature, timestamp, nonce, postString, ref postString);
                                if (decnum == 0)
                                {
                                    wmc.EncryptMsg(help.ReturnMessage(postString), timestamp, nonce, ref responseContent);
                                }
                            }
                            else
                            {
                                responseContent = help.ReturnMessage(postString);
                            }
                            HttpContext.Current.Response.ContentEncoding = Encoding.UTF8;
                            HttpContext.Current.Response.Write(responseContent);
                        }
                    }
                }
            }
            else
            {
                if (WeChatHelper.CheckSignature(signature, timestamp, nonce))
                {
                    Write(echoString);
                }
            }
        }