Ejemplo n.º 1
0
        public static DwManagementClient Create(string resourceId, ExecutionContext context)
        {
            var config = new ConfigurationBuilder()
                         .SetBasePath(context.FunctionAppDirectory)
                         .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
                         .AddEnvironmentVariables()
                         .Build();

            SubscriptionId = config["SubscriptionId"];
            TenantId       = config["TenantId"];
            ClientId       = config["ClientId"];
            ClientKey      = config["ClientKey"];

            var httpClient            = new HttpClient();
            var authenticationContext = new AuthenticationContext(ActiveDirectoryEndpoint + TenantId);


            var credential = new ClientCredential(clientId: ClientId, clientSecret: ClientKey);
            var result     = authenticationContext.AcquireTokenAsync(resource: WindowsManagementUri, clientCredential: credential).Result;

            if (result == null)
            {
                throw new InvalidOperationException("Failed to obtain the token!");
            }

            var token = result.AccessToken;

            var aadTokenCredentials = new TokenCloudCredentials(SubscriptionId, token);

            var client = new DwManagementClient(aadTokenCredentials, resourceId);

            return(client);
        }
Ejemplo n.º 2
0
        public static async Task <DwManagementClient> Create(string resourceId, ExecutionContext context)
        {
            var config = new ConfigurationBuilder()
                         .SetBasePath(context.FunctionAppDirectory)
                         .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
                         .AddEnvironmentVariables()
                         .Build();

            var azureServiceTokenProvider = new AzureServiceTokenProvider();

            var kvClient = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(azureServiceTokenProvider.KeyVaultTokenCallback));
            var kvURL    = config["KeyVaultURL"];

            try
            {
                var SubscriptionIdSecret = config["SubscriptionId"];
                SubscriptionId = (await kvClient.GetSecretAsync(kvURL, SubscriptionIdSecret)).Value;

                var TenantIdSecret = config["TenantId"];
                TenantId = (await kvClient.GetSecretAsync(kvURL, TenantIdSecret)).Value;
                ClientId = config["ClientId"];
                var ClientKeySecret = config["ClientKey"];
                var ClientKey       = (await kvClient.GetSecretAsync(kvURL, ClientKeySecret)).Value;

                var httpClient            = new HttpClient();
                var authenticationContext = new AuthenticationContext(ActiveDirectoryEndpoint + TenantId);


                var credential = new ClientCredential(clientId: ClientId, clientSecret: ClientKey);
                var result     = authenticationContext.AcquireTokenAsync(resource: WindowsManagementUri, clientCredential: credential).Result;

                if (result == null)
                {
                    throw new InvalidOperationException("Failed to obtain the token!");
                }

                var token = result.AccessToken;

                var aadTokenCredentials = new TokenCloudCredentials(SubscriptionId, token);

                var client = new DwManagementClient(aadTokenCredentials, resourceId);
                return(client);
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException("Failed to obtain the token!");
            }
        }