public string Process([FromQuery] string signature, string timestamp, string nonce, string echostr)
        {
            logHelper.Debug("Process:" + Request.Host.Value);
            // 验签
            int ret = WXBizMsgCrypt.VerifySignature(PubInterface.Conf.AppToken, timestamp, nonce, "", signature);

            // 判断是否验签成功
            if (ret != 0)
            {
                var msg = "Process:微信验证服务器失败:错误代码:" + ret;
                logHelper.Debug(msg);
                return(msg);
            }

            // 验证签名成功返回结果
            return(echostr);
        }
示例#2
0
 public void ProcessRequest(HttpContext context)
 {
     try
     {
         DateTime start = DateTime.Now;
         context.Response.Buffer          = true;
         context.Response.ExpiresAbsolute = DateTime.Now.AddDays(-1.0);
         context.Response.AddHeader("pragma", "no-cache");
         context.Response.AddHeader("cache-control", "");
         context.Response.CacheControl = "no-cache";
         context.Response.ContentType  = "text/plain";
         this.Request  = context.Request;
         this.Response = context.Response;
         this.Server   = context.Server;
         this.Context  = context;
         this.Log(this.GetAllParameter());
         string signature     = this.Request.QueryString["signature"];
         string timestamp     = this.Request.QueryString["timestamp"];
         string nonce         = this.Request.QueryString["nonce"];
         string encrypt_type  = "";
         string msg_signature = "";
         if (this.Request.QueryString["encrypt_type"] != null)
         {
             encrypt_type = this.Request.QueryString["encrypt_type"];
         }
         if (this.Request.QueryString["msg_signature"] != null)
         {
             msg_signature = this.Request.QueryString["msg_signature"];
         }
         string token          = PubFunction.curParameter.strWeiXinToken;
         string encodingAESKey = PubFunction.curParameter.strWeiXinEncodingAESKey;
         string appid          = PubFunction.curParameter.strWeiXinAppID;
         string appSecret      = PubFunction.curParameter.strWeiXinAppSecret;
         if (this.Request.HttpMethod.ToLower() == "get")
         {
             string echostr = this.Request.QueryString["echostr"].ToString();
             int    temp    = WXBizMsgCrypt.VerifySignature(token, timestamp, nonce, signature);
             if (temp == 0)
             {
                 this.Response.Write(echostr);
             }
             else
             {
                 this.Log(string.Concat(new object[]
                 {
                     "验证消息真实性错误;代码:",
                     temp,
                     ",信息:",
                     Enum.GetName(typeof(WXBizMsgCrypt.WXBizMsgCryptErrorCode), temp)
                 }));
                 this.Response.Write("");
             }
         }
         else
         {
             Stream s = HttpContext.Current.Request.InputStream;
             byte[] b = new byte[s.Length];
             s.Read(b, 0, (int)s.Length);
             string postData = Encoding.UTF8.GetString(b);
             this.Log("接收消息:" + postData);
             if (PubFunction.curParameter.strDoMain.IndexOf("localhost") >= 0 || PubFunction.curParameter.strDoMain.IndexOf("192.168.0.") >= 0)
             {
                 token          = "QDG6eK";
                 encodingAESKey = "jWmYm7qr5nMoAUwZRjGtBxmz3KA1tkAj3ykkR6q2B2C";
                 appid          = "wx5823bf96d3bd56c7";
             }
             WXBizMsgCrypt wxbmc       = new WXBizMsgCrypt(token, encodingAESKey, appid);
             string        requestData = "";
             if (encrypt_type == "aes")
             {
                 int ret = wxbmc.DecryptMsg(msg_signature, timestamp, nonce, postData, ref requestData);
                 if (ret != 0)
                 {
                     this.Log(string.Concat(new object[]
                     {
                         "解密微信消息错误;代码:",
                         ret,
                         ",信息:",
                         Enum.GetName(typeof(WXBizMsgCrypt.WXBizMsgCryptErrorCode), ret)
                     }));
                     this.Response.Write("");
                     return;
                 }
             }
             else
             {
                 requestData = postData;
             }
             this.Log("解密消息:" + requestData);
             IRequest wxRequest    = ReqeustManager.GetRequest(requestData);
             string   responseData = wxRequest.Response();
             this.Log("回复消息:" + responseData);
             string encryptResponseData = "";
             if (encrypt_type == "aes")
             {
                 int ret = wxbmc.EncryptMsg(responseData, timestamp, nonce, ref encryptResponseData);
                 if (ret != 0)
                 {
                     this.Log(string.Concat(new object[]
                     {
                         "加密微信消息错误;代码:",
                         ret,
                         ",信息:",
                         Enum.GetName(typeof(WXBizMsgCrypt.WXBizMsgCryptErrorCode), ret)
                     }));
                     this.Response.Write("");
                     return;
                 }
             }
             else
             {
                 encryptResponseData = responseData;
             }
             string time = (DateTime.Now - start).TotalMilliseconds + "毫秒";
             this.Log("加密消息:" + encryptResponseData + "\r\n执行时间:" + time);
             this.Response.Write(encryptResponseData);
         }
     }
     catch (Exception ex)
     {
         this.Log(ex.ToString());
     }
 }
        public async Task <dynamic> Process([FromQuery] string signature, string timestamp, string nonce, string openid, string encrypt_type, string msg_signature)
        {
            logHelper.Debug("Process:请求Url:" + Request.Host.Value);
            // 响应
            var response = HttpContext.Response;

            // post过来的数据
            Stream stream = HttpContext.Request.Body;

            byte[] bytes = new byte[stream.Length];
            stream.Read(bytes, 0, (int)stream.Length);
            string postStr = System.Text.Encoding.UTF8.GetString(bytes);

            logHelper.Debug("Process:推送数据:" + postStr);

            // 验证数据签名
            string decryptMsg = string.Empty;
            int    ret        = -1;

            if (string.IsNullOrEmpty(PubInterface.Conf.EncodingAesKey))
            {
                // 明文验签
                ret        = WXBizMsgCrypt.VerifySignature(PubInterface.Conf.AppToken, timestamp, nonce, "", signature);
                decryptMsg = postStr;
            }
            else
            {
                // 密文验签
                ret = QyCrypt.DecryptMsg(msg_signature, timestamp, nonce, postStr, ref decryptMsg);
            }

            if (ret != 0)
            {
                // 验签、解密失败
                logHelper.Error("Process:微信通知消息验证失败:错误代码:" + ret);
                // 返回Sucess防止微信后续5次的推送(未收到Success之前都会一直推送)
                return(wxAutoComResponse.ResponseOK());;
            }

            logHelper.Debug("Process:解密后内容:" + decryptMsg);
            // 序列化接收到的消息
            PubReceiveMsgCData recMsg = ComHelper.XmlDeserialize <PubReceiveMsgCData>(decryptMsg);

            logHelper.Debug("ReceiveMsgCData:" + recMsg.JsonSerialize());

            // 保存日志到数据库
            await wxDbLogHelper.SaveWxOptLog(recMsg);

            // 保存用户信息
            await wxDbLogHelper.SaveWxUser(recMsg);

            // 判断是否接入客服
            var kfRes = wxKfTransferHelper.ChatWithKf(recMsg);

            if (kfRes.IsSucc)
            {
                return(await wxAutoComResponse.AutoMsgResponse(kfRes.Data));
            }

            // 判断消息类型
            switch (recMsg.MsgType)
            {
            case PubMsgType.text:
                logHelper.Debug("text");
                return(textHandler.DealQyText(recMsg));

            case PubMsgType.image:
                logHelper.Debug("image");
                break;

            case PubMsgType.voice:
                logHelper.Debug("voice");
                break;

            case PubMsgType.video:
                logHelper.Debug("video");
                break;

            case PubMsgType.shortvideo:
                logHelper.Debug("shortvideo");
                break;

            case PubMsgType.location:
                logHelper.Debug("location");
                break;

            case PubMsgType.link:
                logHelper.Debug("link");
                break;

            case PubMsgType.@event:
                logHelper.Debug("event");
                return(eventHandler.DealQyEvent(recMsg));

            //case PubMsgType.wxcard:
            //    logHelper.Debug("wxcard");
            //    break;
            //case PubMsgType.mpnews:
            //    logHelper.Debug("mpnews");
            //    break;
            //case PubMsgType.news:
            //    logHelper.Debug("news");
            //    break;
            //case PubMsgType.music:
            //    logHelper.Debug("music");
            //    break;
            default:
                break;
            }

            return(wxAutoComResponse.ResponseOK());;
        }