private IEnumerable <AzureSubscription> ListAllSubscriptionsForTenant( string tenantId) { IAzureAccount account = _profile.DefaultContext.Account; IAzureEnvironment environment = _profile.DefaultContext.Environment; SecureString password = null; string promptBehavior = ShowDialog.Never; IAccessToken accessToken = null; try { accessToken = AcquireAccessToken(account, environment, tenantId, password, promptBehavior, null); } catch { // Unable to acquire token for tenant return(new List <AzureSubscription>()); } SubscriptionClient subscriptionClient = null; subscriptionClient = AzureSession.Instance.ClientFactory.CreateCustomArmClient <SubscriptionClient>( environment.GetEndpointAsUri(AzureEnvironment.Endpoint.ResourceManager), new TokenCredentials(accessToken.AccessToken) as ServiceClientCredentials, AzureSession.Instance.ClientFactory.GetCustomHandlers()); AzureContext context = new AzureContext(_profile.DefaultContext.Subscription, account, environment, CreateTenantFromString(tenantId, accessToken.TenantId)); return(subscriptionClient.ListAllSubscriptions().Select(s => s.ToAzureSubscription(context.Account, context.Environment, accessToken.TenantId))); }
private static IEnumerable <AzureSubscription> ListAllSubscriptionsForTenant( IAzureContext defaultContext, string tenantId) { IAzureAccount account = defaultContext.Account; IAzureEnvironment environment = defaultContext.Environment; IAccessToken accessToken = null; try { accessToken = AcquireAccessToken(account, environment, tenantId); } catch (Exception e) { throw new AadAuthenticationFailedException("Could not find subscriptions", e); } SubscriptionClient subscriptionClient = null; subscriptionClient = AzureSession.Instance.ClientFactory.CreateCustomArmClient <SubscriptionClient>( environment.GetEndpointAsUri(AzureEnvironment.Endpoint.ResourceManager), new TokenCredentials(accessToken.AccessToken) as ServiceClientCredentials, AzureSession.Instance.ClientFactory.GetCustomHandlers()); AzureContext context = new AzureContext(defaultContext.Subscription, account, environment, CreateTenantFromString(tenantId, accessToken.TenantId)); return(subscriptionClient.ListAllSubscriptions().Select(s => ToAzureSubscription(s, context))); }
public List <IAzureSubscription> GetSubscriptionsFromTenantToken(IAccount account, IAzureEnvironment environment, IAccessToken token, Action <string> promptAction) { TracingAdapter.Information(string.Format("[AuthenticationClientFactory] Attempting to acquire subscriptions in tenant '{0}' for account '{1}'.", token.TenantId, account.Username)); List <IAzureSubscription> result = new List <IAzureSubscription>(); var azureAccount = new AzureAccount() { Id = account.Username, Type = AzureAccount.AccountType.User }; using (SubscriptionClient subscriptionClient = GetSubscriptionClient(token, environment)) { var subscriptions = (subscriptionClient.ListAllSubscriptions().ToList() ?? new List <Subscription>()) .Where(s => "enabled".Equals(s.State.ToString(), StringComparison.OrdinalIgnoreCase) || "warned".Equals(s.State.ToString(), StringComparison.OrdinalIgnoreCase)); foreach (var subscription in subscriptions) { var azureSubscription = new AzureSubscription(); azureSubscription.SetAccount(azureAccount.Id); azureSubscription.SetEnvironment(environment.Name); azureSubscription.Id = subscription?.SubscriptionId; azureSubscription.Name = subscription?.DisplayName; azureSubscription.State = subscription?.State.ToString(); azureSubscription.SetProperty(AzureSubscription.Property.Tenants, token.TenantId); result.Add(azureSubscription); } } return(result); }