因為access_token的壽命只有7200秒,每日獲取access_token存在上限,所以在獲取access_token后,需要將其緩存起來.
首先建立一個模型
1 public class AccessToken 2 3 { 4 public string access_token { get; set; } 5 6 public DateTime expires_in { get; set; } 7 8 }
然后存儲獲取的access_token(獲取方法),和當前的時間
1 public static void Save() 2 { 3 var accessToken = new AccessToken(); 4 accessToken.access_token = a_token; 5 accessToken.expires_in = DateTime.Now; 6 //存儲 7 System.Web.HttpContext.Current.Application["accessToken"] = accessToken; 8 //獲取 9 //var accessToken = System.Web.HttpContext.Current.Application["accessToken"] as AccessToken; 10 }