public ManagedServiceAccessToken(IAzureAccount account, IAzureEnvironment environment, string resourceId, string tenant = "Common")
        {
            if (account == null || string.IsNullOrEmpty(account.Id) || !account.IsPropertySet(AzureAccount.Property.MSILoginUri))
            {
                throw new ArgumentNullException(nameof(account));
            }

            if (string.IsNullOrWhiteSpace(tenant))
            {
                throw new ArgumentNullException(nameof(tenant));
            }

            if (environment == null)
            {
                throw new ArgumentNullException(nameof(environment));
            }

            _account    = account;
            _resourceId = GetResource(resourceId, environment);
            var idType = GetIdentityType(account);

            foreach (var uri in BuildTokenUri(_account.GetProperty(AzureAccount.Property.MSILoginUri), account, idType, _resourceId))
            {
                RequestUris.Enqueue(uri);
            }

            if (account.IsPropertySet(AzureAccount.Property.MSILoginUriBackup))
            {
                foreach (var uri in BuildTokenUri(_account.GetProperty(AzureAccount.Property.MSILoginUriBackup), account, idType, _resourceId))
                {
                    RequestUris.Enqueue(uri);
                }
            }

            _tenant = tenant;
            IHttpOperationsFactory factory;

            if (!AzureSession.Instance.TryGetComponent(HttpClientOperationsFactory.Name, out factory))
            {
                factory = HttpClientOperationsFactory.Create();
            }

            _tokenGetter = factory.GetHttpOperations <ManagedServiceTokenInfo>(true).WithHeader("Metadata", new[] { "true" });
            if (account.IsPropertySet(AzureAccount.Property.MSILoginSecret))
            {
                _tokenGetter = _tokenGetter.WithHeader("Secret", new[] { account.GetProperty(AzureAccount.Property.MSILoginSecret) });
            }
        }
Exemplo n.º 2
0
        void GetOrRenewAuthentication()
        {
            if (_expiration - DateTime.UtcNow < ManagedServiceTokenInfo.TimeoutThreshold)
            {
                ManagedServiceTokenInfo info = null;
                while (info == null && RequestUris.Count > 0)
                {
                    var currentRequestUri = RequestUris.Dequeue();
                    try
                    {
                        info = _tokenGetter.GetAsync(currentRequestUri, CancellationToken.None).ConfigureAwait(false).GetAwaiter().GetResult();
                        // if a request was succesful, we should not check any other Uris
                        RequestUris.Clear();
                        RequestUris.Enqueue(currentRequestUri);
                    }
                    catch (Exception e) when((e is CloudException || e is HttpRequestException) && RequestUris.Count > 0)
                    {
                        // skip to the next uri
                    }
                }

                SetToken(info);
            }
        }