コード例 #1
0
        /// <summary>
        /// 微信验证成为开发者
        /// </summary>
        public void Check()
        {
            try
            {
                if (Request.HttpMethod.ToUpper() == "GET")
                {
                    string result    = string.Empty;
                    string signature = Request["signature"]; //微信加密签名
                    string timestamp = Request["timestamp"]; //时间戳
                    string nonce     = Request["nonce"];     //随机数
                    string echostr   = Request["echostr"];   //随机字符串

                    // 开发者通过检验signature对请求进行校验(下面有校验方式)。若确认此次GET请求来自微信服务器,请原样返回echostr参数内容,则接入生效,成为开发者成    功,否则接入失败。加密 / 校验流程如下:
                    //1)将token、timestamp、nonce三个参数进行字典序排序
                    //2)将三个参数字符串拼接成一个字符串进行sha1加密
                    //3)开发者获得加密后的字符串可与signature对比,标识该请求来源于微信

                    string[] array = { token, timestamp, nonce };
                    Array.Sort(array);//字典排序
                    string newSignature = SHA1(string.Join("", array));
                    if (newSignature == signature.ToUpper())
                    {
                        result = echostr;
                    }
                    else
                    {
                        result = "Valiate Fail";
                    }
                    logger.Info("验证成为开发者:" + result);
                    Response.Write(result);
                }
                else
                {                                                              //接收微信加密消息
                    string       msg_signature     = Request["msg_signature"]; //签名
                    string       timestamp         = Request["timestamp"];
                    string       nonce             = Request["nonce"];
                    StreamReader sr                = new StreamReader(Request.InputStream, Encoding.UTF8);
                    string       requestXmlMessage = sr.ReadToEnd();
                    logger.Info("接收微信消息:" + requestXmlMessage);


                    WXBizMsgCrypt wxcpt = new WXBizMsgCrypt(token, sEncodingAESKey, sAppId);
                    string        sMsg  = string.Empty;
                    wxcpt.DecryptMsg(msg_signature, timestamp, nonce, requestXmlMessage, ref sMsg);
                    logger.Info("解密后的消息:" + sMsg);
                    string sMsgType = XmlHelper.getTextByNode(sMsg, "MsgType");
                    if (sMsgType.ToUpper() != "EVENT")
                    {
                        string        sOpenId    = XmlHelper.getTextByNode(sMsg, "FromUserName");//获取openId
                        List <string> WaiterInfo = RedisHelper.GetCharValue(CacheType.Session, sOpenId).Split(',').ToList();
                        if (!string.IsNullOrEmpty(WaiterInfo[0]))
                        {
                            //获取客户账号
                            string OriginId     = XmlHelper.getTextByNode(sMsg, "ToUserName");//获取公众号原始ID
                            long   CreateTime   = Convert.ToInt64((DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalSeconds);
                            string access_token = WeChatHelper.GetAccessToken();
                            string KfAccount    = WeChatHelper.GetKfAccount(WaiterInfo[0], access_token);
                            string responeData  = string.Format(@"<xml>
                                                                    <ToUserName><![CDATA[{0}]]></ToUserName>
                                                                    <FromUserName><![CDATA[{1}]]></FromUserName>
                                                                    <CreateTime>{2}</CreateTime>
                                                                    <MsgType><![CDATA[transfer_customer_service]]></MsgType>
                                                                    <TransInfo>
                                                                    <KfAccount><![CDATA[{3}]]></KfAccount>
                                                                    </TransInfo>
                                                                </xml>", sOpenId, OriginId, CreateTime, KfAccount);
                            logger.Info("返回的消息:" + responeData);
                            //发送模板消息
                            string openId  = WaiterInfo[1];//营业员的OpenId
                            string Content = XmlHelper.getTextByNode(sMsg, "Content");
                            SendMessage(openId, Content);

                            string sEncryptMsg = "";                                          //xml格式的密文
                            wxcpt.EncryptMsg(responeData, timestamp, nonce, ref sEncryptMsg); //加密
                            logger.Info("返回的加密消息:" + sEncryptMsg);
                            Response.Write(sEncryptMsg);
                        }
                    }
                    else
                    {
                        Response.Write(string.Empty);
                    }
                }
            }
            catch (Exception e)
            {
                logger.Info("WeiXinController:" + e.Message);
                logger.Info(e.Message, e);
                Response.Write("System Error!");
            }
        }