Esempio n. 1
0
        /// <summary>
        /// 核心执行 过程的  验签和解密
        /// </summary>
        /// <param name="recXml">消息内容</param>
        /// <param name="signature">微信加密签名</param>
        /// <param name="timestamp">时间戳</param>
        /// <param name="nonce">随机数</param>
        /// <returns>验证结果及相应的消息内容体 (如果加密模式,返回的是解密后的明文)</returns>
        private ResultMo <string> PrepareExecute(string recXml, string signature,
                                                 string timestamp, string nonce)
        {
            if (string.IsNullOrEmpty(recXml))
            {
                return(new ResultMo <string>(ResultTypes.ObjectNull, "接收的消息体为空!"));
            }

            var resCheck = WxMsgHelper.CheckSignature(ApiConfig.Token, signature, timestamp, nonce);

            if (!resCheck.IsSuccess())
            {
                return(resCheck.ConvertToResult <string>());
            }

            if (ApiConfig.SecurityType == WxSecurityType.None)
            {
                return(new ResultMo <string>(recXml));
            }

            var dirs = WxMsgHelper.ChangXmlToDir(recXml, out XmlDocument xmlDoc);

            if (dirs == null || !dirs.TryGetValue("Encrypt", out var encryStr) ||
                string.IsNullOrEmpty(encryStr))
            {
                return(new ResultMo <string>(ResultTypes.ObjectNull, "加密消息为空"));
            }

            var recMsgXml = Cryptography.WxAesDecrypt(encryStr, ApiConfig.EncodingAesKey);

            return(new ResultMo <string>(recMsgXml));
        }
Esempio n. 2
0
        /// <summary>
        /// 消息处理入口
        /// </summary>
        /// <param name="contentXml">内容信息</param>
        /// <param name="signature">签名信息</param>
        /// <param name="timestamp">时间戳</param>
        /// <param name="nonce">随机字符创</param>
        /// <param name="echostr">验证服务器参数,如果存在则只进行签名验证,并将在结果data中返回</param>
        /// <returns>消息结果,Data为响应微信数据,如果出错Message为错误信息</returns>
        public ResultMo <string> Process(string contentXml, string signature, string timestamp, string nonce,
                                         string echostr)
        {
            // 一.  检查是否是服务器验证
            if (!string.IsNullOrEmpty(echostr))
            {
                return(CheckServerValid(signature, timestamp, nonce, echostr));
            }

            // 二.  正常消息处理
            var checkRes = PrepareExecute(contentXml, signature, timestamp, nonce);

            if (!checkRes.IsSuccess())
            {
                return(checkRes.ConvertToResult <string>());
            }

            var contextRes = Execute(checkRes.data);

            if (!contextRes.IsSuccess())
            {
                return(contextRes.ConvertToResult <string>());
            }

            var resultString = contextRes.data.ReplyMsg.ToReplyXml();

            if (ApiConfig.SecurityType != WxSecurityType.None &&
                !string.IsNullOrEmpty(contextRes.data.ReplyMsg.MsgType))
            {
                return(WxMsgHelper.EncryptMsg(resultString, ApiConfig));
            }
            return(new ResultMo <string>(resultString));
        }
Esempio n. 3
0
        /// <summary>
        /// 核心执行方法 过程中的 委托方代码执行
        /// </summary>
        /// <param name="recMsgXml">传入消息的xml</param>
        /// <returns></returns>
        protected virtual ResultMo <WxMsgContext> Execute(string recMsgXml)
        {
            var recMsgDirs = WxMsgHelper.ChangXmlToDir(recMsgXml, out XmlDocument xmlDoc);

            recMsgDirs.TryGetValue("MsgType", out var msgType);// recMsgDirs["MsgType"].ToLower();
            string eventName = null;

            if (msgType == "event")
            {
                if (!recMsgDirs.TryGetValue("Event", out eventName))
                {
                    return(new ResultMo <WxMsgContext>(ResultTypes.ParaError, "事件消息数据中未发现 事件类型(Event)字段!"));
                }
            }

            var processor = GetBasicMsgProcessor(msgType, eventName);

            if (!(processor?.CanExecute).HasValue)
            {
                processor = GetCustomProcessor(msgType, eventName, recMsgDirs);
                if (!(processor?.CanExecute).HasValue)
                {
                    processor = GetRegProcessor(msgType, eventName);
                }
            }

            var context = processor != null && processor.CanExecute
                ? ExecuteProcessor(xmlDoc, recMsgDirs, processor.CreateNewInstance(), processor.Execute)
                : ExecuteProcessor(xmlDoc, recMsgDirs, new WxBaseRecMsg(), null);

            ExecuteEnd(context);

            return(new ResultMo <WxMsgContext>(context));
        }
Esempio n. 4
0
        /// <summary>
        /// 核心执行方法   ==  执行方法
        /// </summary>
        /// <param name="recMsgXml">传入消息的xml</param>
        protected ResultMo <MsgContext> ProcessExecute(string recMsgXml)
        {
            XmlDocument xmlDoc     = null;
            var         recMsgDirs = WxMsgHelper.ChangXmlToDir(recMsgXml, ref xmlDoc);

            if (!recMsgDirs.ContainsKey("MsgType"))
            {
                return(new ResultMo <MsgContext>(ResultTypes.ParaError, "消息数据中未发现 消息类型(MsgType)字段!"));
            }

            string msgType = recMsgDirs["MsgType"].ToLower();

            if (msgType == "event")
            {
                if (!recMsgDirs.ContainsKey("Event"))
                {
                    return(new ResultMo <MsgContext>(ResultTypes.ParaError, "事件消息数据中未发现 事件类型(Event)字段!"));
                }
            }

            var context = ProcessExecute_BasicMsg(xmlDoc, msgType, recMsgDirs)
                          ?? ProcessExecute_AdvancedMsg(xmlDoc, msgType, recMsgDirs)
                          ?? ExecuteBasicMsgHandler(xmlDoc, recMsgDirs, UnknowHandler);

            return(new ResultMo <MsgContext>(context));
        }
Esempio n. 5
0
        /// <summary>
        /// 核心执行方法    ==    验证签名和消息体信息解密处理部分
        /// </summary>
        /// <param name="recXml">消息内容</param>
        /// <param name="signature">微信加密签名</param>
        /// <param name="timestamp">时间戳</param>
        /// <param name="nonce">随机数</param>
        /// <returns>验证结果及相应的消息内容体 (如果加密模式,返回的是解密后的明文)</returns>
        protected ResultMo <string> ProcessBegin(string recXml, string signature,
                                                 string timestamp, string nonce)
        {
            if (string.IsNullOrEmpty(recXml))
            {
                return(new ResultMo <string>(ResultTypes.ObjectNull, "接收的消息体为空!"));
            }

            var resCheck = WxMsgHelper.CheckSignature(m_Config.Token, signature, timestamp, nonce);

            if (resCheck.IsSuccess())
            {
                if (m_Config.SecurityType != WxSecurityType.None)
                {
                    XmlDocument xmlDoc = null;
                    var         dirs   = WxMsgHelper.ChangXmlToDir(recXml, ref xmlDoc);
                    if (dirs == null || !dirs.ContainsKey("Encrypt"))
                    {
                        return(new ResultMo <string>(ResultTypes.ObjectNull, "加密消息为空"));
                    }

                    var recMsgXml = Cryptography.WxAesDecrypt(dirs["Encrypt"], m_Config.EncodingAesKey);
                    return(new ResultMo <string>(recMsgXml));
                }
                return(new ResultMo <string>(recXml));
            }
            return(resCheck.ConvertToResultOnly <string>());
        }
Esempio n. 6
0
        /// <summary>
        ///  服务器验证
        /// </summary>
        /// <param name="signature"></param>
        /// <param name="timestamp"></param>
        /// <param name="nonce"></param>
        /// <param name="echostr"></param>
        /// <returns></returns>
        public ResultMo <string> CheckServerValid(string signature, string timestamp, string nonce, string echostr)
        {
            var checkSignRes = WxMsgHelper.CheckSignature(m_Config.Token, signature, timestamp, nonce);
            var resultRes    = checkSignRes.ConvertToResultOnly <string>();

            resultRes.data = resultRes.IsSuccess() ? echostr : string.Empty;
            return(resultRes);
        }
Esempio n. 7
0
        /// <summary>
        ///  根据具体的消息类型执行相关的消息委托方法(基础消息)
        /// </summary>
        /// <typeparam name="TRecMsg"></typeparam>
        /// <param name="recMsgXml"></param>
        /// <param name="recMsgDirs"></param>
        /// <param name="func"></param>
        /// <returns></returns>
        private static MsgContext ExecuteBasicMsgHandler <TRecMsg>(XmlDocument recMsgXml,
                                                                   IDictionary <string, string> recMsgDirs, Func <TRecMsg, BaseReplyMsg> func)
            where TRecMsg : BaseRecMsg, new()
        {
            var msgContext = new MsgContext();

            var recMsg = WxMsgHelper.GetMsg <TRecMsg>(recMsgDirs);

            recMsg.RecMsgXml = recMsgXml;

            msgContext.ReplyMsg = ExecuteHandler(recMsg, func);
            msgContext.RecMsg   = recMsg;

            return(msgContext);
        }