Exemplo n.º 1
0
        /// <summary>
        /// 开发者接管
        /// </summary>
        /// <param name="signature"></param>
        /// <param name="timestamp"></param>
        /// <param name="nonce"></param>
        /// <param name="echostr"></param>
        /// <param name="encrypt_type"></param>
        /// <param name="msg_signature"></param>
        public async void WeChat(string signature, string timestamp, string nonce, string echostr, string encrypt_type, string msg_signature)
        {
            string result = string.Empty;

            //微信后台验证地址(使用Get),微信后台的“接口配置信息”的Url
            if (Request.Method.ToLower() == "get")
            {
                var Token = GlobalTo.GetValue("ApiKey:WeChatMP:Token");

                if (Netnr.WeChat.Helpers.Util.CheckSignature(signature, timestamp, nonce, Token))
                {
                    //返回随机字符串则表示验证通过
                    result = echostr;
                }
                else
                {
                    result = "参数错误!";
                }
            }
            //处理请求
            else
            {
                WeChatMessage message  = null;
                var           safeMode = encrypt_type == "aes";

                var Token          = string.Empty;
                var EncodingAESKey = string.Empty;
                var AppID          = string.Empty;

                if (safeMode)
                {
                    Token          = GlobalTo.GetValue("ApiKey:WeChatMP:Token");
                    EncodingAESKey = GlobalTo.GetValue("ApiKey:WeChatMP:EncodingAESKey");
                    AppID          = GlobalTo.GetValue("ApiKey:WeChatMP:AppID");
                }

                using (var ms = new MemoryStream())
                {
                    await Request.Body.CopyToAsync(ms);

                    var myByteArray = ms.ToArray();

                    var    decryptMsg = string.Empty;
                    string postStr    = System.Text.Encoding.UTF8.GetString(myByteArray);

                    #region 解密
                    if (safeMode)
                    {
                        var wxBizMsgCrypt = new WeChat.Helpers.Crypto.WXBizMsgCrypt(Token, EncodingAESKey, AppID);
                        var ret           = wxBizMsgCrypt.DecryptMsg(msg_signature, timestamp, nonce, postStr, ref decryptMsg);
                        //解密失败
                        if (ret != 0)
                        {
                            FilterConfigs.WriteLog(HttpContext, new Exception("微信解密失败"));
                        }
                    }
                    else
                    {
                        decryptMsg = postStr;
                    }
                    #endregion

                    message = WeChatMessage.Parse(decryptMsg);
                }
                var response = new WeChatExecutor().Execute(message);

                #region 加密
                if (safeMode)
                {
                    var wxBizMsgCrypt = new WeChat.Helpers.Crypto.WXBizMsgCrypt(Token, EncodingAESKey, AppID);
                    var ret           = wxBizMsgCrypt.EncryptMsg(response, timestamp, nonce, ref result);
                    if (ret != 0)//加密失败
                    {
                        FilterConfigs.WriteLog(HttpContext, new Exception("微信加密失败"));
                    }
                }
                else
                {
                    result = response;
                }
                #endregion
            }

            //输出
            byte[] buffer = System.Text.Encoding.UTF8.GetBytes(result);
            await Response.Body.WriteAsync(buffer, 0, buffer.Length);

            await Response.Body.FlushAsync();
        }
Exemplo n.º 2
0
        /// <summary>
        /// 开发者接管
        /// </summary>
        public void Index(string signature, string timestamp, string nonce, string echostr, string encrypt_type, string msg_signature)
        {
            string result = string.Empty;

            //微信后台验证地址(使用Get),微信后台的“接口配置信息”的Url
            if (Request.Method.ToLower() != "post")
            {
                var Token = Configuration.GetValue <string>("WeChat:MP:Token");
                if (Helpers.Util.CheckSignature(signature, timestamp, nonce, Token))
                {
                    //返回随机字符串则表示验证通过
                    result = echostr;
                }
                else
                {
                    result = "参数错误!";
                }
            }
            //处理请求
            else
            {
                WeChatMessage message  = null;
                var           safeMode = encrypt_type == "aes";

                var Token          = string.Empty;
                var EncodingAESKey = string.Empty;
                var AppID          = string.Empty;

                if (safeMode)
                {
                    Token          = Configuration.GetValue <string>("WeChat:MP:Token");
                    EncodingAESKey = Configuration.GetValue <string>("WeChat:MP:EncodingAESKey");
                    AppID          = Configuration.GetValue <string>("WeChat:MP:AppID");
                }

                using (var ms = new MemoryStream())
                {
                    Request.Body.CopyTo(ms);
                    var myByteArray = ms.ToArray();

                    var    decryptMsg = string.Empty;
                    string postStr    = System.Text.Encoding.UTF8.GetString(myByteArray);

                    #region 解密
                    if (safeMode)
                    {
                        var wxBizMsgCrypt = new Helpers.Crypto.WXBizMsgCrypt(Token, EncodingAESKey, AppID);
                        var ret           = wxBizMsgCrypt.DecryptMsg(msg_signature, timestamp, nonce, postStr, ref decryptMsg);
                        //解密失败
                        if (ret != 0)
                        {
                            //TODO:开发者解密失败的业务处理逻辑
                        }
                    }
                    else
                    {
                        decryptMsg = postStr;
                    }
                    #endregion

                    message = WeChatMessage.Parse(decryptMsg);
                }
                var response   = new WeChatExecutor().Execute(message);
                var encryptMsg = string.Empty;

                #region 加密
                if (safeMode)
                {
                    var wxBizMsgCrypt = new Helpers.Crypto.WXBizMsgCrypt(Token, EncodingAESKey, AppID);
                    var ret           = wxBizMsgCrypt.EncryptMsg(response, timestamp, nonce, ref encryptMsg);
                    if (ret != 0)//加密失败
                    {
                        //TODO:开发者加密失败的业务处理逻辑
                    }
                }
                else
                {
                    encryptMsg = response;
                }
                #endregion
            }

            //输出
            byte[] buffer = System.Text.Encoding.UTF8.GetBytes(result);
            Response.Body.Write(buffer, 0, buffer.Length);
            Response.Body.Flush();
        }