예제 #1
0
        /// <summary>
        /// 缓存并获取调用微信公众号接口的全局唯一票据
        /// </summary>
        /// <param name="appId">设置应用的APPID</param>
        /// <param name="secret">设置应用的APPSECRET</param>
        /// <returns>如果缓存的票据有效,则返回缓存的票据字符串,否则调用接口重新获取票据信息</returns>
        public static string GetAccessToken(string appId, string secret)
        {
            HttpApplicationState apps = HttpContext.Current.Application;
            SortedDictionary<string, object> cache = null;
            string accessToken = null;

            if (apps[CACHEKEYNAME] != null)
                cache = apps[CACHEKEYNAME] as SortedDictionary<string, object>;

            if (!General.IsNullable(cache))
            {
                DateTime saveTime = Convert.ToDateTime(cache["last_save_time"]);
                int cost = (int)(DateTime.Now - saveTime).TotalSeconds;
                int expires = Convert.ToInt32(cache["expires_in"]);
                if (cost < expires)
                    accessToken = cache["access_token"].ToString();
            }

            if (General.IsNullable(accessToken))
            {
                if (!General.IsNullable(appId) && !General.IsNullable(secret))
                {
                    string url = "https://api.weixin.qq.com/cgi-bin/token";
                    WxParameters paras = new WxParameters();
                    paras.SetValue("grant_type", "client_credential");
                    paras.SetValue("appid", appId);
                    paras.SetValue("secret", secret);

                    NetClient client = new NetClient();
                    string response = client.Get(url, paras.Parameters);
                    WxParameters result = new WxParameters();

                    if (result.FromJson(response))
                    {
                        if (result.ContainsValue("access_token") && result.ContainsValue("expires_in"))
                        {
                            cache = result.Parameters;
                            cache.Add("last_save_time", DateTime.Now);
                            apps[CACHEKEYNAME] = cache;
                            accessToken = result["access_token"] as string;
                        }
                    }
                }
            }

            return accessToken;
        }
예제 #2
0
파일: JSSDK.cs 프로젝트: honj51/micro-emall
        private static string GetTicket(string appId, string secret)
        {
            HttpApplicationState apps = HttpContext.Current.Application;
            SortedDictionary<string, object> cache = null;
            string ticket = null;

            if (apps[CACHEKEYNAME] != null)
                cache = apps[CACHEKEYNAME] as SortedDictionary<string, object>;

            if (!General.IsNullable(cache))
            {
                DateTime saveTime = Convert.ToDateTime(cache["last_save_time"]);
                int cost = (int)(DateTime.Now - saveTime).TotalSeconds;
                int expires = Convert.ToInt32(cache["expires_in"]);
                if (cost < expires)
                    ticket = cache["ticket"] as string;
            }

            if (General.IsNullable(ticket))
            {
                if (!General.IsNullable(appId) && !General.IsNullable(secret))
                {
                    string accessToken = InterfaceCredentials.GetAccessToken(appId, secret);
                    string url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket";
                    WxParameters paras = new WxParameters();
                    paras.SetValue("access_token", accessToken);
                    paras.SetValue("type", "jsapi");

                    NetClient client = new NetClient();
                    string response = client.Get(url, paras.Parameters);
                    WxParameters result = new WxParameters();

                    if (result.FromJson(response))
                    {
                        if (result.ContainsValue("ticket") && result.ContainsValue("expires_in"))
                        {
                            cache = result.Parameters;
                            cache.Remove("errcode");
                            cache.Remove("errmsg");
                            cache.Add("last_save_time", DateTime.Now);
                            apps[CACHEKEYNAME] = cache;
                            ticket = result["ticket"] as string;
                        }
                    }
                }
            }

            return ticket;
        }
예제 #3
0
        /// <summary>
        /// 获取微信服务器的IP地址列表
        /// </summary>
        /// <param name="accessToken">设置全局接口调用票据字符串</param>
        /// <returns>如果票据有效,则返回微信服务器的IP地址列表,否则返回NULL</returns>
        public static List<string> GetServerIP(string accessToken)
        {
            List<string> ipList = null;

            if (!General.IsNullable(accessToken))
            {
                string url = "https://api.weixin.qq.com/cgi-bin/getcallbackip";
                NetClient client = new NetClient();
                string response = client.Get(url, new string[] { "access_token=" + accessToken });
                WxParameters result = new WxParameters();
                if (result.FromJson(response))
                {
                    if (result.ContainsValue("ip_list"))
                        ipList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<string>>(result["ip_list"].ToString());
                }
            }

            return ipList;
        }