Exemplo n.º 1
0
        public void UsesDefaultCredentialWhenNoneExists()
        {
            var configuration = GetConfiguration();

            var serviceCollection = new ServiceCollection();

            serviceCollection.AddAzureClients(builder => builder.UseCredential(new EnvironmentCredential()));

            ServiceProvider       provider   = serviceCollection.BuildServiceProvider();
            AzureComponentFactory factory    = provider.GetService <AzureComponentFactory>();
            TokenCredential       credential = factory.CreateCredential(configuration);

            Assert.IsInstanceOf <EnvironmentCredential>(credential);
        }
Exemplo n.º 2
0
        public void CanCreateCredential()
        {
            var configuration = GetConfiguration(
                new KeyValuePair <string, string>("TestClient:clientId", "ConfigurationClientId"),
                new KeyValuePair <string, string>("TestClient:clientSecret", "ConfigurationClientSecret"),
                new KeyValuePair <string, string>("TestClient:tenantId", "ConfigurationTenantId"));

            var serviceCollection = new ServiceCollection();

            serviceCollection.AddAzureClientsCore();

            ServiceProvider       provider   = serviceCollection.BuildServiceProvider();
            AzureComponentFactory factory    = provider.GetService <AzureComponentFactory>();
            TokenCredential       credential = factory.CreateCredential(configuration.GetSection("TestClient"));

            Assert.IsInstanceOf <ClientSecretCredential>(credential);
            var clientSecretCredential = (ClientSecretCredential)credential;

            Assert.AreEqual("ConfigurationClientId", clientSecretCredential.ClientId);
            Assert.AreEqual("ConfigurationClientSecret", clientSecretCredential.ClientSecret);
            Assert.AreEqual("ConfigurationTenantId", clientSecretCredential.TenantId);
        }
        /// <summary>
        /// TODO.
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public virtual StorageAccount Get(string name)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                name = ConnectionStringNames.Storage; // default
            }

            // $$$ Where does validation happen?
            IConfigurationSection connectionSection = _configuration.GetWebJobsConnectionStringSection(name);

            if (!connectionSection.Exists())
            {
                // Not found
                throw new InvalidOperationException($"Storage account connection string '{IConfigurationExtensions.GetPrefixedConnectionStringName(name)}' does not exist. Make sure that it is a defined App Setting.");
            }

            if (!string.IsNullOrWhiteSpace(connectionSection.Value))
            {
                return(new StorageAccount(
                           new BlobServiceClient(connectionSection.Value, CreateBlobClientOptions(null)),
                           new QueueServiceClient(connectionSection.Value, CreateQueueClientOptions(null))));
            }

            var endpoint = connectionSection["endpoint"];

            if (string.IsNullOrWhiteSpace(endpoint))
            {
                // Not found
                throw new InvalidOperationException($"Connection should have an 'endpoint' property or be a string representing a connection string.");
            }

            var credential  = _componentFactory.CreateCredential(connectionSection);
            var endpointUri = new Uri(endpoint);

            return(new StorageAccount(
                       new BlobServiceClient(endpointUri, credential, CreateBlobClientOptions(connectionSection)),
                       new QueueServiceClient(endpointUri, credential, CreateQueueClientOptions(connectionSection))));
        }
Exemplo n.º 4
0
            public Task <object> GetValueAsync()
            {
                Type clientOptionType = null;

                foreach (var constructor in _clientType.GetConstructors(BindingFlags.Public | BindingFlags.Instance))
                {
                    var lastParameter = constructor.GetParameters().LastOrDefault();
                    if (lastParameter != null && typeof(ClientOptions).IsAssignableFrom(lastParameter.ParameterType))
                    {
                        clientOptionType = lastParameter.ParameterType;
                        break;
                    }
                }

                if (clientOptionType == null)
                {
                    throw new InvalidOperationException("Unable to detect the client option type");
                }

                var credential = _componentFactory.CreateCredential(_configuration);
                var options    = _componentFactory.CreateClientOptions(clientOptionType, null, _configuration);

                return(Task.FromResult(_componentFactory.CreateClient(_clientType, _configuration, credential, options)));
            }