首页 新闻 会员 周边

问下网页授权 refreshtoken存储

0
悬赏园豆:20 [待解决问题]

各位大虾,有人网页授权的refreshtoken吗?在微信端应该怎样存储呢?

lf_DoBest的主页 lf_DoBest | 初学一级 | 园豆:169
提问于:2015-04-13 16:26
< >
分享
所有回答(2)
0

什么叫怎么存储?

问天何必 | 园豆:3311 (老鸟四级) | 2015-04-13 16:37

存到服务器的refreshtoken然后怎么存到客户端供下一次使用呢?

支持(0) 反对(0) lf_DoBest | 园豆:169 (初学一级) | 2015-04-13 16:40
0

缓存啊

带缓存的 获取token的方法

static Dictionary<string, List<string>> _AppDict = new Dictionary<string, List<string>>();

        /// <summary>
        /// 获得Token
        /// 1.通过字典获得一个cachekeylist
        /// 2.遍历cachekeylist 获获得相应的Token
        /// 3.比对Token 如果已经用过这个Token就换一个
        /// </summary>
        /// <param name="appID"></param>
        /// <param name="appSecret"></param>
        /// <param name="exceptTokenList"></param>
        /// <returns></returns>
        public static string GetToken(string appID, string appSecret, List<string> exceptTokenList)
        {
            string token = null;
            // 1.通过字典获得一个cachekeylist
            List<string> cachekeylist = null;
            if (_AppDict.ContainsKey(appID + appSecret))
            {
                cachekeylist = _AppDict[appID + appSecret];
            }
            else
            {
                cachekeylist = new List<string>();
                _AppDict.Add(appID + appSecret, cachekeylist);
            }
            List<string> removeList = new List<string>();
            // 2.遍历cachekeylist 获获得相应的Token
            foreach (string item in cachekeylist)
            {
                if (exceptTokenList == null || exceptTokenList.Exists(p => p != item))
                {
                    //HttpRuntime.Cache
                    if (HttpRuntime.Cache[item] != null)
                    {
                        token = HttpRuntime.Cache[item].ToString();
                        break;
                    }
                    else
                    {
                        removeList.Add(item);
                    }
                }
            }
            // 3.比对Token 如果已经用过这个Token就换一个
            cachekeylist.RemoveAll(p => removeList.Exists(q => q == p));

            if (token == null)
            {
                int expires_in = 0;

                WXToken wXToken = WXHelper.GetToken(appID, appSecret);
                if (wXToken != null)
                {
                    token = wXToken.access_token;
                    expires_in = wXToken.expires_in;
                }
                cachekeylist.Add(token);
                HttpRuntime.Cache.Insert(token, token, null, DateTime.Now.AddSeconds(expires_in - 10), TimeSpan.Zero);

            }
            return token;
        }

 基础方法

        /// <summary>
        /// 获得token
        /// </summary>
        /// <param name="appid"></param>
        /// <param name="secret"></param>
        /// <returns></returns>
        public static WXToken GetToken(string appid, string secret)
        {
            System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace(0, true);

            string sRet = "";

            string sUrl = "";
            string sPam = "";
            sUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid + "&secret=" + secret;
            sRet = RequestHelper.HttpPost(sUrl, sPam);
            LogHeper.Instant.log.Info(sUrl);
            LogHeper.Instant.log.Info(sRet);

            JavaScriptSerializer javaScriptSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
            WXToken wXToken = javaScriptSerializer.Deserialize<WXToken>(sRet);

            return wXToken;
        }

 

小眼睛老鼠 | 园豆:2731 (老鸟四级) | 2015-04-14 11:23
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册