Beispiel #1
0
        /// <summary>
        /// 消息处理过程
        /// </summary>
        /// <param name="context"></param>
        private void WeChatMessagerHandle(System.Web.HttpContext context)
        {
            var inputStream = context.Request.InputStream;
            var inputBuffer = new Byte[inputStream.Length];

            inputStream.Read(inputBuffer, 0, inputBuffer.Length);
            var requetContent = Encoding.UTF8.GetString(inputBuffer);

            //if (OnReceiveRequest != null)
            //    OnReceiveRequest(requetContent);

            Utilities.MessageCrypt cryptTool = null;
            string signature    = context.Request["signature"],
                   msgSignature = context.Request["msg_signature"],
                   timestamp    = context.Request["timestamp"],
                   nonce        = context.Request["nonce"],
            //echostr = context.Request["echostr"],
                   encryptType = context.Request["encrypt_type"]
            ;

            bool isEncrypt = WeChatConfigManager.IsCorperation || (!string.IsNullOrEmpty(encryptType) && encryptType == "aes");

            #region 加密模式解密处理
            if (isEncrypt)
            {
                cryptTool     = new Nlab.WeChatApi.Utilities.MessageCrypt(WeChatToken, EncodingAESKey, WechatAppId);
                requetContent = cryptTool.DecryptMsg(msgSignature, timestamp, nonce, requetContent);
            }
            #endregion
            if (OnReceiveRequest != null)
            {
                OnReceiveRequest(requetContent);
            }

            var msgBag          = XmlToDict(requetContent);
            var responseContent = ProcessMessageReceive(msgBag);
            if (!string.IsNullOrEmpty(responseContent))
            {
                if (OnCompleteResponse != null)
                {
                    OnCompleteResponse(requetContent, responseContent);
                }
                if (cryptTool != null) //加密模式,进行数据加密
                {
                    responseContent = cryptTool.EncryptMsg(responseContent, timestamp, nonce);
                }
                context.Response.Write(responseContent);
            }
        }
Beispiel #2
0
        /// <summary>
        /// 服务认证过程
        /// </summary>
        /// <param name="context"></param>
        private void WeChatAuthenticationHandle(System.Web.HttpContext context)
        {
            string signature   = context.Request["signature"],
                   timestamp   = context.Request["timestamp"],
                   nonce       = context.Request["nonce"],
                   echostr     = context.Request["echostr"],
                   encryptType = context.Request["encrypt_type"]
            ;

            if (string.IsNullOrEmpty(signature)) //企业号
            {
                signature = context.Request["msg_signature"];
                if (!string.IsNullOrEmpty(signature))
                {
                    var    cryptTool = new Nlab.WeChatApi.Utilities.MessageCrypt(WeChatToken, EncodingAESKey, WechatAppId);
                    string decryptEcho;
                    var    rst = cryptTool.VerifyURL(signature, timestamp, nonce, echostr, ref echostr);
                    if (rst != 0)
                    {
                        throw new WechatApiException(rst, "VerifyURL failed");
                    }
                    context.Response.Write(echostr);
                    return;
                }
            }
            //context.Request.Path
            else //
            if (!string.IsNullOrEmpty(signature) && !string.IsNullOrEmpty(timestamp) && !string.IsNullOrEmpty(nonce) && !string.IsNullOrEmpty(echostr))
            {
                if (OnAuthen != null)
                {
                    OnAuthen(signature, timestamp, nonce, echostr);
                }
                var authArray = new string[] { WeChatToken, timestamp, nonce };
                Array.Sort(authArray);
                var authStr     = string.Join("", authArray);
                var authStrSha1 = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(authStr, "sha1").ToLower();
                if (signature == authStrSha1)
                {
                    context.Response.Write(echostr);
                    return;
                }
                else
                {
                    context.Response.Write("Invalid Token.");
                }
            }
            context.Response.Write("WeChatApi Create by Laiguoxin([email protected],http://www.9499.net) 2013-7 reference:https://github.com/laicom/WechatApi.git");
        }