Esempio n. 1
0
        /// <summary>
        /// 调用。
        /// </summary>
        /// <param name="context">处理上下文。</param>
        /// <returns>任务。</returns>
        public override Task Invoke(IHandlerContext context)
        {
            var responseMessage = context.GetResponseMessage();

            if (responseMessage == null)
            {
                context.ResponseXml = string.Empty;
            }
            else
            {
                var requestMessage = context.GetRequestMessage();

                //基本信息初始化。
                responseMessage.CreateTime   = DateTime.Now;
                responseMessage.FromUserName = requestMessage.ToUserName;
                responseMessage.ToUserName   = requestMessage.FromUserName;

                var dependencyResolver     = context.GetDependencyResolver();
                var responseMessageFactory = dependencyResolver.GetService <IResponseMessageFactory>();
                var content = responseMessageFactory.GetXmlByReponseMessage(responseMessage);

                #region Encrypt

                if (!string.IsNullOrWhiteSpace(content))
                {
                    var request     = context.Request;
                    var encryptType = request.QueryString["encrypt_type"];

                    if (encryptType != null)
                    {
                        var nonce     = request.QueryString["nonce"];
                        var timestamp = request.QueryString["timestamp"];

                        var baseInfo       = context.GetMessageHandlerBaseInfo();
                        var appId          = baseInfo.AppId;
                        var encodingAesKey = baseInfo.EncodingAesKey;
                        var token          = baseInfo.Token;

                        var wxBizMsgCrypt = new WXBizMsgCrypt(token, encodingAesKey, appId);
                        wxBizMsgCrypt.EncryptMsg(content, timestamp, nonce, ref content);
                    }
                }

                #endregion Encrypt

                context.ResponseXml = content;
            }

            return(Next.Invoke(context));
        }
        /// <summary>
        /// 调用。
        /// </summary>
        /// <param name="context">处理上下文。</param>
        /// <returns>任务。</returns>
        public override Task Invoke(IHandlerContext context)
        {
            var responseMessage = context.GetResponseMessage();

            if (responseMessage == null)
            {
                context.ResponseXml = string.Empty;
            }
            else
            {
                var requestMessage = context.GetRequestMessage();

                //基本信息初始化。
                responseMessage.CreateTime = DateTime.Now;
                responseMessage.FromUserName = requestMessage.ToUserName;
                responseMessage.ToUserName = requestMessage.FromUserName;

                var dependencyResolver = context.GetDependencyResolver();
                var responseMessageFactory = dependencyResolver.GetService<IResponseMessageFactory>();
                var content = responseMessageFactory.GetXmlByReponseMessage(responseMessage);

                #region Encrypt

                if (!string.IsNullOrWhiteSpace(content))
                {
                    var request = context.Request;
                    var encryptType = request.QueryString["encrypt_type"];

                    if (encryptType != null)
                    {
                        var nonce = request.QueryString["nonce"];
                        var timestamp = request.QueryString["timestamp"];

                        var baseInfo = context.GetMessageHandlerBaseInfo();
                        var appId = baseInfo.AppId;
                        var encodingAesKey = baseInfo.EncodingAesKey;
                        var token = baseInfo.Token;

                        var wxBizMsgCrypt = new WXBizMsgCrypt(token, encodingAesKey, appId);
                        wxBizMsgCrypt.EncryptMsg(content, timestamp, nonce, ref content);
                    }
                }

                #endregion Encrypt

                context.ResponseXml = content;
            }

            return Next.Invoke(context);
        }