Esempio n. 1
0
        private TokenData GetNewTokenData()
        {
            TokenData tokenData = null;
            IDictionary <string, object> tokenParams = new Dictionary <string, object>
            {
                { "client_id", _ClientId },
                { "client_secret", _ClientSecret },
                { "authorize_type", "silent" },
                { "grant_id", _GrantId }
            };
            DefaultHttpClient defaultHttpClient = new DefaultHttpClient();
            string            result            = defaultHttpClient.Send(this.GetTokenUrl(), tokenParams, null, null);
            OauthToken        oAuthToken        = JsonConvert.DeserializeObject <OauthToken>(result);

            if (oAuthToken.Data == null)
            {
                tokenData = new TokenData
                {
                    Message = result
                };
                return(tokenData);
            }
            string data = oAuthToken.Data.ToString();

            tokenData = JsonConvert.DeserializeObject <TokenData>(data);

            // Token添加缓存
            if (cache.Contains(this._ClientId + "_" + this._GrantId))
            {
                cache.Remove(this._ClientId + "_" + this._GrantId);
            }
            cache.Add(this._ClientId + "_" + this._GrantId, tokenData, tokenData.ExpiresTime.AddMinutes(-5));

            return(tokenData);
        }
Esempio n. 2
0
        internal TokenData GetNewTokenData()
        {
            TokenData tokenData = null;
            IDictionary <string, object> tokenParams = new ConcurrentDictionary <string, object>();

            tokenParams.Add("client_id", _ClientId);
            tokenParams.Add("client_secret", _ClientSecret);
            tokenParams.Add("authorize_type", "silent");
            tokenParams.Add("grant_id", _GrantId);
            DefaultHttpClient defaultHttpClient = new DefaultHttpClient();
            string            result            = defaultHttpClient.Send(this.GetTokenUrl(), tokenParams, null, null);
            OauthToken        oAuthToken        = JsonConvert.DeserializeObject <OauthToken>(result);

            if (oAuthToken.Data == null)
            {
                tokenData = new TokenData
                {
                    Message = result
                };
                return(tokenData);
            }
            string data = oAuthToken.Data.ToString();

            tokenData = JsonConvert.DeserializeObject <TokenData>(data);

            // Token添加缓存
            if (!YouZanConfig.SaveAccessTokenToDB)
            {
                if (cache.Contains(this._CacheKey))
                {
                    cache.Remove(this._CacheKey);
                }
                cache.Add(this._CacheKey, tokenData, tokenData.ExpiresTime.AddMinutes(-5));
            }

            return(tokenData);
        }