Exemplo n.º 1
0
        /// <summary>
        /// 获取jsapi_ticket
        /// </summary>
        /// <returns></returns>
        public static string GetJsapiTicket()
        {
            string accessToken = GetAccessToken();
            string key         = "jsapi_ticket";

            if (CacheExtend.HasCache(key))
            {
                return(CacheExtend.GetCache <JsapiTicket>(key).ticket);
            }

            lock (_lock)
            {
                if (CacheExtend.HasCache(key))
                {
                    return(CacheExtend.GetCache <JsapiTicket>(key).ticket);
                }
                JsapiTicket ticket = null;
                try
                {
                    LogHelper.WriteLog("GetJsapiTicket-accessToken:" + accessToken);
                    HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(string.Format("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token={0}&type=jsapi", accessToken));
                    req.Method = "GET";
                    using (WebResponse wr = req.GetResponse())
                    {
                        HttpWebResponse myResponse = (HttpWebResponse)req.GetResponse();
                        StreamReader    reader     = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
                        string          content    = reader.ReadToEnd();
                        LogHelper.WriteLog("content:" + content);
                        if (!string.IsNullOrEmpty(content))
                        {
                            ticket = JsonHelper.JsonToObject <JsapiTicket>(content);
                            if (ticket != null)
                            {
                                LogHelper.WriteLog("ticket:" + ticket.ticket);
                                //LogHelper.WriteLog("jsapi_ticket插入缓存,jsapi_ticket有效期为:" + (ticket.expires_in - 1800).ToString());
                                CacheExtend.CacheInsert(key, ticket, DateTime.Now.AddSeconds(ticket.expires_in - 1800));
                            }
                            else
                            {
                                LogHelper.WriteLog(ErrorManage.GetErrorInfo(ticket.errcode ?? -1));
                            }
                        }
                        else
                        {
                            LogHelper.WriteLog("jsapi_ticket 返回的值为NULL");
                        }
                    }
                }
                catch (Exception ex)
                {
                    LogHelper.WriteLog(string.Format("{0}\n{1}\n{2}\n", ex.Message, ex.Source, ex.StackTrace));
                }
                return(ticket == null ? "" : ticket.ticket);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 获取AccessToken
        /// </summary>
        /// <returns></returns>
        public static string GetAccessToken()
        {
            string key = "AccessToken";

            AccessToken token = null;

            try
            {
                HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(string.Format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}", appid, secret));
                req.Method = "GET";
                using (WebResponse wr = req.GetResponse())
                {
                    HttpWebResponse myResponse = (HttpWebResponse)req.GetResponse();
                    StreamReader    reader     = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
                    string          content    = reader.ReadToEnd();

                    if (!string.IsNullOrEmpty(content))
                    {
                        token = JsonHelper.JsonToObject <AccessToken>(content);
                        if (token != null)
                        {
                            //LogHelper.WriteLog("Token插入缓存,Token有效期为:" + (token.expires_in - 1800).ToString());
                            CacheExtend.CacheInsert(key, token, DateTime.Now.AddSeconds(token.expires_in - 1800));
                        }
                        else
                        {
                            LogHelper.WriteLog(ErrorManage.GetErrorInfo(token.errcode ?? -1));
                        }
                    }
                    else
                    {
                        LogHelper.WriteLog("Token 返回的值为NULL");
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(string.Format("{0}\n{1}\n{2}\n", ex.Message, ex.Source, ex.StackTrace));
            }
            return(token == null ? "" : token.access_token);
        }
Exemplo n.º 3
0
        private static string GetMsg(string remsg, ResultCryptography ccg)
        {
            if (ccg.HasCrypto())
            {
                string sEncryptMsg = "";

                WXBizMsgCrypt wxcpt = new WXBizMsgCrypt(ccg.sToken, ccg.sEncodingAESKey, ccg.sAppID);
                int           ret   = 0;
                ret = wxcpt.EncryptMsg(remsg, ccg.timestamp, ccg.nonce, ref sEncryptMsg);

                if (ret != 0)
                {
                    LogHelper.WriteLog("ERR: Encrypt fail:" + ErrorManage.GetErrorInfo(ret));
                    return("");
                }

                return(sEncryptMsg);
            }

            return(remsg);
        }
Exemplo n.º 4
0
        /// <summary>
        /// 微信回调入口
        /// </summary>
        /// <param name="Request"></param>
        /// <param name="Response"></param>
        public static void ReceviceMain(HttpRequestBase Request, HttpResponseBase Response)
        {
            try
            {
                if (Request.HttpMethod.ToLower() != "post")
                {
                    Response.End();
                }
                else
                {
                    Stream s = System.Web.HttpContext.Current.Request.InputStream;
                    byte[] b = new byte[s.Length];
                    s.Read(b, 0, (int)s.Length);
                    var    postStr = Encoding.UTF8.GetString(b);
                    string sMsg    = "";
                    //LogHelper.WriteLog("post结果:" + postStr);
                    var checkgrp = new ResultCryptography()
                    {
                        encrypttype     = "raw",
                        timestamp       = "",
                        nonce           = "",
                        sToken          = token,
                        sEncodingAESKey = encodingAESKey,
                        sAppID          = appid
                    };
                    if (Request.QueryString["encrypt_type"] != null && Request.QueryString["encrypt_type"].ToLower() == "aes")
                    {
                        //解密
                        string sReqMsgSig    = Request.QueryString["msg_signature"].ToString();
                        string sReqTimeStamp = Request.QueryString["timestamp"].ToString();
                        string sReqNonce     = Request.QueryString["nonce"].ToString();

                        checkgrp.encrypttype = "aes";
                        checkgrp.timestamp   = sReqTimeStamp;
                        checkgrp.nonce       = sReqNonce;

                        WXBizMsgCrypt wxcpt = new WXBizMsgCrypt(token, encodingAESKey, appid);

                        int ret = 0;
                        ret = wxcpt.DecryptMsg(sReqMsgSig, sReqTimeStamp, sReqNonce, postStr, ref sMsg);

                        if (ret != 0)
                        {
                            LogHelper.WriteLog("ERR: Decrypt fail:" + ErrorManage.GetErrorInfo(ret));
                            return;
                        }
                    }
                    else
                    {
                        sMsg = postStr;
                    }
                    var msgModel = XmlHelpler.GetMsg(sMsg);

                    ReceiveMsg.SwitchCase(Response, msgModel, checkgrp);
                }
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog("ReceviceMain-" + ex.Message + "," + ex.Source + "," + ex.StackTrace);
            }
        }