/// <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);

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

                if (Logging.IsEnabled)
                {
                    Logging.GenerateToken(this, ExpiresOn);
                }

                return(_token);
            }
            finally
            {
                _lock.Release();
            }
        }
Exemplo n.º 2
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 iotHub)
        {
            if (!IsExpiring)
            {
                return(_token);
            }

            await _lock.WaitAsync();

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

                _token = await SafeCreateNewToken(iotHub, _suggestedTimeToLiveSeconds);

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

                return(_token);
            }
            finally
            {
                _lock.Release();
            }
        }
Exemplo n.º 3
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 iotHub)
        {
            if (_isDisposed)
            {
                if (Logging.IsEnabled)
                {
                    Logging.Error(
                        this,
                        $"Encountered {nameof(ObjectDisposedException)} - The authentication method instance has already been disposed, so this client is no longer usable.",
                        nameof(GetTokenAsync));
                }

                throw new ObjectDisposedException(GetType().Name, "The authentication method instance has already been disposed, so this client is no longer usable. " +
                                                  "Please close and dispose your current client instance. To continue carrying out operations from your device/ module, " +
                                                  "create a new authentication method instance and use it for reinitializing your client.");
            }

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

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

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

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

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

                if (Logging.IsEnabled)
                {
                    Logging.GenerateToken(this, ExpiresOn);
                }

                return(_token);
            }
            finally
            {
                _lock.Release();
            }
        }
Exemplo n.º 4
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);
        }