示例#1
0
        /// <summary>
        /// 获取Token
        /// </summary>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        protected virtual string GetToken()
        {
            string accessToken;
            var    tokenKey = string.Format(TokenKey, this._resource, this._clientId, this._userName);
            var    bytes    = _distributedCache.Get(tokenKey);

            if (bytes != null)
            {
                accessToken = Encoding.UTF8.GetString(bytes);

                if (!string.IsNullOrWhiteSpace(accessToken))
                {
                    return(accessToken);
                }
            }



            // 保证缓存Token不会过期
            var now = DateTimeOffset.Now;

            lock (LockObj)
            {
                bytes = _distributedCache.Get(tokenKey);
                if (bytes != null)
                {
                    accessToken = Encoding.UTF8.GetString(bytes);

                    if (!string.IsNullOrWhiteSpace(accessToken))
                    {
                        return(accessToken);
                    }
                }

                var authResult =
                    Dynamics365Auth.AcquireToken(_adfsUri, _resource, _clientId, _redirectUri, _userName, _password);
                if (authResult == null)
                {
                    throw new Exception("Auth get failed");
                }

                var expiresIn = authResult.expires_in;
                accessToken = authResult.access_token;
                if (string.IsNullOrWhiteSpace(accessToken))
                {
                    throw new Exception("Token get failed");
                }


                _distributedCache.Set(tokenKey, Encoding.UTF8.GetBytes(accessToken), options: new DistributedCacheEntryOptions()
                {
                    AbsoluteExpiration = now + TimeSpan.FromSeconds(expiresIn)
                });
                return(accessToken);
            }
        }
示例#2
0
        /// <summary>
        /// 获取Token
        /// </summary>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        protected virtual string GetToken()
        {
            string accessToken;
            var    tokenKey = string.Format(TokenKey, this._resource, this._clientId, this._userName);

            if (_cacheManager.IsSet(tokenKey))
            {
                accessToken = _cacheManager.Get <string>(tokenKey);
                if (!string.IsNullOrWhiteSpace(accessToken))
                {
                    return(accessToken);
                }
            }

            // 保证缓存Token不会过期
            var now = DateTimeOffset.Now;

            lock (LockObj)
            {
                if (_cacheManager.IsSet(tokenKey))
                {
                    accessToken = _cacheManager.Get <string>(tokenKey);
                    if (!string.IsNullOrWhiteSpace(accessToken))
                    {
                        return(accessToken);
                    }
                }

                var authResult =
                    Dynamics365Auth.AcquireToken(_adfsUri, _resource, _clientId, _redirectUri, _userName, _password);
                if (authResult == null)
                {
                    throw new Exception("Auth get failed");
                }

                var expiresIn = authResult.expires_in;
                accessToken = authResult.access_token;
                if (string.IsNullOrWhiteSpace(accessToken))
                {
                    throw new Exception("Token get failed");
                }

                var cacheTime = now + TimeSpan.FromSeconds(expiresIn);

                _cacheManager.Set <string>(tokenKey, cacheTime, accessToken);
                return(accessToken);
            }
        }