Exemplo n.º 1
0
        public static string GetAccessTokenAndRefresh()
        {
            lock (locker)
            {
                RedisHelper       redis = new RedisHelper();
                WechatAccessToken token = redis.StringGet <WechatAccessToken>("WechatAccessToken");
                if (token == null)
                {
                    token = new WechatAccessToken();
                    AccessTokenResponseMessage message = GetAccessTokenFromWechat();

                    token.AccessToken = message.AccessToken;
                    token.ExpiresIn   = message.ExpiresIn;
                    token.UpdateTime  = DateTime.Now;

                    redis.StringSet <WechatAccessToken>("WechatAccessToken", token);
                }
                else
                {
                    TimeSpan ts = DateTime.Now - token.UpdateTime;
                    if (ts.TotalMinutes > 90)
                    {
                        AccessTokenResponseMessage message = GetAccessTokenFromWechat();

                        token.AccessToken = message.AccessToken;
                        token.ExpiresIn   = message.ExpiresIn;
                        token.UpdateTime  = DateTime.Now;

                        redis.StringSet <WechatAccessToken>("WechatAccessToken", token);
                    }
                }

                return(token.AccessToken);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 刷新票据凭证
        /// </summary>
        /// <param name="accessToken"></param>
        /// <returns></returns>
        public static string RefreshAccessToken(string accessToken)
        {
            lock ( locker )
            {
                if (WechatApi.AccessToken != accessToken)
                {
                    return(WechatApi.AccessToken);
                }

                AccessTokenResponseMessage response = WechatApi.GetAccessTokenFromWechat();

                WechatAccessToken token = new WechatAccessToken();

                token.AppId       = ConfigurationManager.AppSettings["AppId"];
                token.AccessToken = response.AccessToken;
                token.ExpiresIn   = response.ExpiresIn;
                token.UpdateTime  = DateTime.Now;

                RedisHelper redis = new RedisHelper();
                redis.StringSet <WechatAccessToken>(token.AppId, token);

                WechatApi.AccessToken = token.AccessToken;

                return(WechatApi.AccessToken);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// 获取票据凭证
        /// </summary>
        /// <returns></returns>
        public static string GetAccessToken()
        {
            lock ( locker )
            {
                if (string.IsNullOrEmpty(WechatApi.AccessToken))
                {
                    WechatAccessToken token = WechatApi.GetAccessTokenFromDb();

                    if (token == null)
                    {
                        AccessTokenResponseMessage response = WechatApi.GetAccessTokenFromWechat();

                        token = new WechatAccessToken();

                        token.AppId       = ConfigurationManager.AppSettings["AppId"];
                        token.AccessToken = response.AccessToken;
                        token.ExpiresIn   = response.ExpiresIn;
                        token.UpdateTime  = DateTime.Now;

                        RedisHelper redis = new RedisHelper();
                        redis.StringSet <WechatAccessToken>(token.AppId, token);

                        WechatApi.AccessToken = token.AccessToken;
                    }
                    else
                    {
                        WechatApi.AccessToken = token.AccessToken;
                    }
                }

                return(WechatApi.AccessToken);
            }
        }