Exemplo n.º 1
0
        public void ProcessRequest(HttpContext context)
        {
            con = context;
            con.Response.ContentType = "text/plain";

            signature = context.Request.QueryString["signature"] ?? "";
            timestamp = context.Request.QueryString["timestamp"] ?? "";
            nonce     = context.Request.QueryString["nonce"] ?? "";
            echoStr   = context.Request.QueryString["echoStr"];


            #region 认证判断
            string[] tempArr = new[] { timestamp, nonce, Common.WeChatAppInfo.Token };
            tempArr = tempArr.OrderBy(item => item).ToArray();
            string tempStr     = string.Join("", tempArr);
            string tempSha1Str = WeChatHelper.GetSHA1EnryptStr(tempStr);

            if (tempSha1Str != signature)
            {
                con.Response.Write("无验证请求");
                con.Response.End();
                return;
            }
            if (!string.IsNullOrEmpty(echoStr))
            {
                context.Response.Write(echoStr);
                context.Response.End();
                return;
            }
            #endregion


            //  WeChatCrypt = Tencent.WXBizMsgCrypt(WeChatAppInfo.Token, WeChatAppInfo.sEncodingAESKey, WeChatAppInfo.AppID);

            string requestContent = System.Text.Encoding.UTF8.GetString(context.Request.BinaryRead(context.Request.TotalBytes));

            int    ret  = 0;
            string sMsg = "";  //解析之后的明文
            ret = Tencent.WXBizMsgCrypt.DecryptMsg(signature, timestamp, nonce, requestContent, ref sMsg);
            if (ret != 0)
            {
                context.Response.Write("无效请求");
                context.Response.End();
                return;
            }

            XmlDocument doc = new XmlDocument();

            doc.LoadXml(sMsg);

            if (doc.FirstChild["MsgId"] != null)
            {
                if (!IsUniqueMsg(doc.FirstChild["MsgId"].InnerText))
                {
                    context.Response.End();
                    return;
                }
            }
            ReceiveData(doc);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 生成微信配置
        /// </summary>
        private void InitWxConfig()
        {
            timestamp    = DateTime.Now.ToWeChatSecondFromDateTime().ToString();
            noncestr     = WeChatHelper.GetRandomString(16);
            url          = Request.Url.AbsoluteUri;
            jsapi_ticket = WeChatAppInfo.ticket;

            List <Dictionary <string, string> > sortList = new List <Dictionary <string, string> >()
            {
                new Dictionary <string, string>()
                {
                    { "key", "jsapi_ticket" }, { "value", jsapi_ticket }
                },
                new Dictionary <string, string>()
                {
                    { "key", "noncestr" }, { "value", noncestr }
                },
                new Dictionary <string, string>()
                {
                    { "key", "timestamp" }, { "value", timestamp }
                },
                new Dictionary <string, string>()
                {
                    { "key", "url" }, { "value", url }
                }
            };


            StringBuilder keyValueSB = new StringBuilder();

            foreach (Dictionary <string, string> item in sortList)
            {
                keyValueSB.AppendFormat("{0}={1}&", item["key"], item["value"]);
            }
            keyValueSB.Remove(keyValueSB.Length - 1, 1);
            string str = keyValueSB.ToString();

            hash = WeChatHelper.GetSHA1EnryptStr(str);

            System.Web.UI.WebControls.Literal wxConfig = new System.Web.UI.WebControls.Literal();
            wxConfig.Text = string.Format(@"
    <script src='/Resource/js/jquery-1.8.2.js'></script>
    <script src='/Resource/js/jweixin-1.0.0.js'></script>
<script defer='defer'>
wx.config({{
    debug: {0}, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
    appId: '{1}', // 必填,公众号的唯一标识
    timestamp: '{2}', // 必填,生成签名的时间戳
    nonceStr: '{3}', // 必填,生成签名的随机串
    signature: '{4}',// 必填,签名,见附录1
    jsApiList: [{5}] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
}});
</script>
", WeChatAppInfo.SDKDebugger, Common.WeChatAppInfo.AppID, timestamp, noncestr, hash, WeChatAppInfo.SDKjsApiList);

            Page.Header.Controls.AddAt(0, wxConfig);
        }