/// <summary>
 /// Default constructor to initializes user account and subscription
 /// </summary>
 public AzureUserAccountSubscriptionContext(AzureSubscriptionIdentifier subscription, ServiceClientCredentials credentials)
 {
     CommonUtil.CheckForNull(subscription, nameof(subscription));
     CommonUtil.CheckForNull(credentials, nameof(credentials));
     Subscription = subscription;
     Credentials  = credentials;
 }
        /// <summary>
        /// Creates a subscription context for connecting with a known access token. This creates a <see cref="TokenCredentials"/> object for use
        /// in a request
        /// </summary>
        public static AzureUserAccountSubscriptionContext CreateStringTokenContext(AzureSubscriptionIdentifier subscription, string accessToken)
        {
            CommonUtil.CheckForNull(subscription, nameof(subscription));
            CommonUtil.CheckStringForNullOrEmpty(accessToken, nameof(accessToken));
            TokenCredentials credentials = new TokenCredentials(accessToken);

            return(new AzureUserAccountSubscriptionContext(subscription, credentials));
        }
Beispiel #3
0
        private async Task <ServiceResponse <IAzureUserAccountSubscriptionContext> > GetSubscriptionsForTentantAsync(
            IAzureUserAccount userAccount, IAzureTenant tenant, string lookupKey,
            CancellationToken cancellationToken, CancellationToken internalCancellationToken)
        {
            AzureTenant azureTenant = tenant as AzureTenant;

            if (azureTenant != null)
            {
                ServiceClientCredentials credentials = CreateCredentials(azureTenant);
                using (SubscriptionClient client = new SubscriptionClient(_resourceManagementUri, credentials))
                {
                    IEnumerable <Subscription> subs = await GetSubscriptionsAsync(client);

                    return(new ServiceResponse <IAzureUserAccountSubscriptionContext>(subs.Select(sub =>
                    {
                        AzureSubscriptionIdentifier subId = new AzureSubscriptionIdentifier(userAccount, azureTenant.TenantId, sub.SubscriptionId, _resourceManagementUri);
                        AzureUserAccountSubscriptionContext context = new AzureUserAccountSubscriptionContext(subId, credentials);
                        return context;
                    })));
                }
            }
            return(new ServiceResponse <IAzureUserAccountSubscriptionContext>());
        }
Beispiel #4
0
        private async Task <ServiceResponse <IAzureUserAccountSubscriptionContext> > GetSubscriptionsForTentantAsync(
            IAzureUserAccount userAccount, IAzureTenant tenant, string lookupKey,
            CancellationToken cancellationToken, CancellationToken internalCancellationToken)
        {
            AzureTenant azureTenant = tenant as AzureTenant;

            if (azureTenant != null)
            {
                ServiceClientCredentials credentials = CreateCredentials(azureTenant);
                string armEndpoint = userAccount.UnderlyingAccount.Properties.ProviderSettings?.Settings?.ArmResource?.Endpoint;
                Uri    armUri      = null;
                if (armEndpoint != null)
                {
                    try
                    {
                        armUri = new Uri(armEndpoint);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine($"Exception while parsing URI: {e.Message}");
                    }
                }
                using (SubscriptionClient client = new SubscriptionClient(armUri ?? _resourceManagementUri, credentials))
                {
                    IEnumerable <Subscription> subs = await GetSubscriptionsAsync(client);

                    return(new ServiceResponse <IAzureUserAccountSubscriptionContext>(subs.Select(sub =>
                    {
                        AzureSubscriptionIdentifier subId = new AzureSubscriptionIdentifier(userAccount, azureTenant.TenantId, sub.SubscriptionId, armUri ?? _resourceManagementUri);
                        AzureUserAccountSubscriptionContext context = new AzureUserAccountSubscriptionContext(subId, credentials);
                        return context;
                    })));
                }
            }
            return(new ServiceResponse <IAzureUserAccountSubscriptionContext>());
        }