예제 #1
0
        public static ApplicationAccessToken GetApplicationToken()
        {
            string       token = string.Empty;
            DateTime     time;
            string       path = HttpContext.Current.Server.MapPath("~/AccessToken.xml");
            StreamReader sr   = new StreamReader(path, System.Text.Encoding.UTF8);
            XmlDocument  xd   = new XmlDocument();

            xd.Load(sr);
            sr.Close();
            sr.Dispose();
            token = xd.SelectSingleNode("xml").SelectSingleNode("Access_Token").InnerText;
            time  = Convert.ToDateTime(xd.SelectSingleNode("xml").SelectSingleNode("Access_Time").InnerText);
            ApplicationAccessToken appToken = new ApplicationAccessToken();

            appToken.access_token = token;
            return(appToken);
        }
예제 #2
0
        /// <summary>
        /// 全局token保存到xml文件里
        /// </summary>
        /// <returns></returns>
        public static Boolean SaveXMLByApplactionToken()
        {
            string       token = string.Empty;
            DateTime     time;
            string       path = HttpContext.Current.Server.MapPath("~/AccessToken.xml");
            StreamReader sr   = new StreamReader(path, System.Text.Encoding.UTF8);
            XmlDocument  xd   = new XmlDocument();

            xd.Load(sr);
            sr.Close();
            sr.Dispose();
            token = xd.SelectSingleNode("xml").SelectSingleNode("Access_Token").InnerText;
            time  = Convert.ToDateTime(xd.SelectSingleNode("xml").SelectSingleNode("Access_Time").InnerText);
            if (DateTime.Now > time)
            {
                string accessToken = WxLoginBLL.GetApplicationAccessToken();
                Log.WriterLog("获取全局token" + accessToken);
                ApplicationAccessToken acctoken = JsonHelper.JsonStringToObj <ApplicationAccessToken>(accessToken);
                if (acctoken.access_token != null && acctoken.access_token != "")
                {
                    DateTime _youxrq = DateTime.Now;
                    xd.SelectSingleNode("xml").SelectSingleNode("Access_Token").InnerText = acctoken.access_token;
                    _youxrq = _youxrq.AddSeconds(int.Parse(acctoken.expires_in));
                    xd.SelectSingleNode("xml").SelectSingleNode("Access_Time").InnerText = _youxrq.ToString();
                    try
                    {
                        xd.Save(path);
                    }catch (Exception e)
                    {
                        Log.WriterLog(e.Source, e.Message);
                        return(false);
                    }
                }
                else
                {
                    return(false);
                }
            }
            return(true);
        }