示例#1
0
        /// 获取jsapi_ticket
        /// <summary>
        /// 获取jsapi_ticket
        /// </summary>
        /// <param name="access"></param>
        /// <returns></returns>
        private static JSTicket getJSTicketTicket(string access)
        {
            string   Str = GetJson("https://api.weixin.qq.com/cgi-bin/ticket/getticket?AccessToken_token=" + access + "&type=JSTicket");
            JSTicket jt  = JsonConvert.DeserializeObject <JSTicket>(Str);

            return(jt);
        }
示例#2
0
        /// 获取jsapi_ticket,如果存在且没过期,则直接取jsapi_ticket
        /// <summary>
        /// 获取jsapi_ticket,如果存在且没过期,则直接取jsapi_ticket
        /// </summary>
        /// <returns></returns>
        public static string GetExistJSTicket()
        {
            // 读取XML文件中的数据
            string       filepath = System.Web.HttpContext.Current.Server.MapPath("/XMLToken.xml");
            FileStream   fs       = new FileStream(filepath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            StreamReader str      = new StreamReader(fs, System.Text.Encoding.UTF8);
            XmlDocument  xml      = new XmlDocument();

            xml.Load(str);
            str.Close();
            str.Dispose();
            fs.Close();
            fs.Dispose();
            string   ticket             = xml.SelectSingleNode("xml").SelectSingleNode("JSTicket").InnerText;
            DateTime AccessTokenExpires = Convert.ToDateTime(xml.SelectSingleNode("xml").SelectSingleNode("JSAccessExpires").InnerText);

            //如果jsapi_ticket过期,则重新获取token
            if (DateTime.Now >= AccessTokenExpires)
            {
                string   Token = xml.SelectSingleNode("xml").SelectSingleNode("AccessToken").InnerText;
                JSTicket mode  = getJSTicketTicket(Token);
                if (mode.ticket != null && mode.expires_in != null)
                {
                    //将jsapi_ticket存到xml文件中,全局缓存
                    xml.SelectSingleNode("xml").SelectSingleNode("JSTicket").InnerText = mode.ticket;
                    DateTime _AccessTokenExpires = DateTime.Now.AddSeconds(int.Parse(mode.expires_in));
                    xml.SelectSingleNode("xml").SelectSingleNode("JSAccessExpires").InnerText = _AccessTokenExpires.ToString();
                    xml.Save(filepath);
                    ticket = mode.ticket;
                }
                else
                {
                    ticket = "";
                }
            }
            return(ticket);
        }