Example #1
0
        /// <summary>
        /// Gets a snapshot of the security token associated with the device. This call is thread-safe.
        /// </summary>
        public async Task <string> GetTokenAsync(string iotHubName)
        {
            if (!IsExpiring)
            {
                return(_token);
            }

            await _lock.WaitAsync();

            if (!IsExpiring)
            {
                return(_token);
            }

            _token = await SafeCreateNewToken(iotHubName);

            SharedAccessSignature sas = SharedAccessSignature.Parse(iotHubName, _token);

            _expiryTime = sas.ExpiresOn;

            _lock.Release();

            return(_token);
        }
        /// <summary>
        /// Gets a snapshot of the security token associated with the device. This call is thread-safe.
        /// </summary>
        public async Task <string> GetTokenAsync(string iotHub)
        {
            if (!IsExpiring)
            {
                return(_token);
            }

            await _lock.WaitAsync().ConfigureAwait(false);

            try
            {
                if (!IsExpiring)
                {
                    return(_token);
                }

                _token = await SafeCreateNewToken(iotHub, _suggestedTimeToLiveSeconds).ConfigureAwait(false);

                SharedAccessSignature sas = SharedAccessSignature.Parse(".", _token);
                _expiryTime = sas.ExpiresOn;
                UpdateTimeBufferSeconds((int)(_expiryTime - DateTime.UtcNow).TotalSeconds);

#if !NET451
                if (Logging.IsEnabled)
                {
                    Logging.GenerateToken(this, _expiryTime);
                }
#endif

                return(_token);
            }
            finally
            {
                _lock.Release();
            }
        }