各位大虾,有人网页授权的refreshtoken吗?在微信端应该怎样存储呢?
什么叫怎么存储?
存到服务器的refreshtoken然后怎么存到客户端供下一次使用呢?
缓存啊
带缓存的 获取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; }