private IEnumerable<AzureSubscription> ListServiceManagementSubscriptions(AzureAccount account, AzureEnvironment environment, SecureString password, ShowDialog promptBehavior, string[] tenants) { List<AzureSubscription> result = new List<AzureSubscription>(); if (!environment.IsEndpointSet(AzureEnvironment.Endpoint.ServiceManagement)) { return result; } foreach (var tenant in tenants) { try { var tenantAccount = new AzureAccount(); CopyAccount(account, tenantAccount); var tenantToken = AzureSession.AuthenticationFactory.Authenticate(tenantAccount, environment, tenant, password, ShowDialog.Never); if (string.Equals(tenantAccount.Id, account.Id, StringComparison.InvariantCultureIgnoreCase)) { tenantAccount = account; } tenantAccount.SetOrAppendProperty(AzureAccount.Property.Tenants, new string[] { tenant }); using (var subscriptionClient = AzureSession.ClientFactory.CreateCustomClient<SubscriptionClient>( new TokenCloudCredentials(tenantToken.AccessToken), environment.GetEndpointAsUri(AzureEnvironment.Endpoint.ServiceManagement))) { var subscriptionListResult = subscriptionClient.Subscriptions.List(); foreach (var subscription in subscriptionListResult.Subscriptions) { // only add the subscription if it's actually in this tenant if (subscription.ActiveDirectoryTenantId == tenant) { AzureSubscription psSubscription = new AzureSubscription { Id = new Guid(subscription.SubscriptionId), Name = subscription.SubscriptionName, Environment = environment.Name }; psSubscription.SetProperty(AzureSubscription.Property.Tenants, subscription.ActiveDirectoryTenantId); psSubscription.Account = tenantAccount.Id; tenantAccount.SetOrAppendProperty(AzureAccount.Property.Subscriptions, new string[] { psSubscription.Id.ToString() }); result.Add(psSubscription); } } } AddOrSetAccount(tenantAccount); } catch (CloudException cEx) { WriteOrThrowAadExceptionMessage(cEx); } catch (AadAuthenticationException aadEx) { WriteOrThrowAadExceptionMessage(aadEx); } } return result; }
/// <summary> /// Initializes a new Azure environment from the given internal representation. /// </summary> /// <param name="environment">The internal representation of the environment.</param> public PSAzureEnvironment(AzureEnvironment environment) { Name = environment.Name; EnableAdfsAuthentication = environment.OnPremise; if (environment.IsEndpointSet(AzureEnvironment.Endpoint.ActiveDirectoryServiceEndpointResourceId)) { ActiveDirectoryServiceEndpointResourceId = environment.Endpoints[AzureEnvironment.Endpoint.ActiveDirectoryServiceEndpointResourceId]; } if (environment.IsEndpointSet(AzureEnvironment.Endpoint.AdTenant)) { AdTenant = environment.Endpoints[AzureEnvironment.Endpoint.AdTenant]; } if (environment.IsEndpointSet(AzureEnvironment.Endpoint.Gallery)) { GalleryUrl = environment.Endpoints[AzureEnvironment.Endpoint.Gallery]; } if (environment.IsEndpointSet(AzureEnvironment.Endpoint.ManagementPortalUrl)) { ManagementPortalUrl = environment.Endpoints[AzureEnvironment.Endpoint.ManagementPortalUrl]; } if (environment.IsEndpointSet(AzureEnvironment.Endpoint.ServiceManagement)) { ServiceManagementUrl = environment.Endpoints[AzureEnvironment.Endpoint.ServiceManagement]; } if (environment.IsEndpointSet(AzureEnvironment.Endpoint.PublishSettingsFileUrl)) { PublishSettingsFileUrl = environment.Endpoints[AzureEnvironment.Endpoint.PublishSettingsFileUrl]; } if (environment.IsEndpointSet(AzureEnvironment.Endpoint.ResourceManager)) { ResourceManagerUrl = environment.Endpoints[AzureEnvironment.Endpoint.ResourceManager]; } if (environment.IsEndpointSet(AzureEnvironment.Endpoint.SqlDatabaseDnsSuffix)) { SqlDatabaseDnsSuffix = environment.Endpoints[AzureEnvironment.Endpoint.SqlDatabaseDnsSuffix]; } if (environment.IsEndpointSet(AzureEnvironment.Endpoint.StorageEndpointSuffix)) { StorageEndpointSuffix = environment.Endpoints[AzureEnvironment.Endpoint.StorageEndpointSuffix]; } if (environment.IsEndpointSet(AzureEnvironment.Endpoint.ActiveDirectory)) { ActiveDirectoryAuthority = environment.Endpoints[AzureEnvironment.Endpoint.ActiveDirectory]; } if (environment.IsEndpointSet(AzureEnvironment.Endpoint.Graph)) { GraphUrl = environment.Endpoints[AzureEnvironment.Endpoint.Graph]; } if (environment.IsEndpointSet(AzureEnvironment.Endpoint.TrafficManagerDnsSuffix)) { TrafficManagerDnsSuffix = environment.Endpoints[AzureEnvironment.Endpoint.TrafficManagerDnsSuffix]; } if (environment.IsEndpointSet(AzureEnvironment.Endpoint.AzureKeyVaultDnsSuffix)) { AzureKeyVaultDnsSuffix = environment.Endpoints[AzureEnvironment.Endpoint.AzureKeyVaultDnsSuffix]; } if (environment.IsEndpointSet(AzureEnvironment.Endpoint.AzureDataLakeStoreFileSystemEndpointSuffix)) { AzureDataLakeStoreFileSystemEndpointSuffix = environment.Endpoints[AzureEnvironment.Endpoint.AzureDataLakeStoreFileSystemEndpointSuffix]; } if (environment.IsEndpointSet(AzureEnvironment.Endpoint.AzureDataLakeAnalyticsCatalogAndJobEndpointSuffix)) { AzureDataLakeAnalyticsCatalogAndJobEndpointSuffix = environment.Endpoints[AzureEnvironment.Endpoint.AzureDataLakeAnalyticsCatalogAndJobEndpointSuffix]; } if (environment.IsEndpointSet(AzureEnvironment.Endpoint.AzureKeyVaultServiceEndpointResourceId)) { AzureKeyVaultServiceEndpointResourceId = environment.Endpoints[AzureEnvironment.Endpoint.AzureKeyVaultServiceEndpointResourceId]; } }
private string[] LoadAccountTenants(AzureAccount account, AzureEnvironment environment, SecureString password, ShowDialog promptBehavior) { var commonTenantToken = AzureSession.AuthenticationFactory.Authenticate(account, environment, AuthenticationFactory.CommonAdTenant, password, promptBehavior); if (environment.IsEndpointSet(AzureEnvironment.Endpoint.ResourceManager)) { using (CSMSubscriptionClient csmSubscriptionClient = AzureSession.ClientFactory .CreateCustomClient<CSMSubscriptionClient>( new TokenCloudCredentials(commonTenantToken.AccessToken), environment.GetEndpointAsUri(AzureEnvironment.Endpoint.ResourceManager))) { return csmSubscriptionClient.Tenants.List().TenantIds.Select(ti => ti.TenantId).ToArray(); } } else { using (RDFESubscriptionClient rdfeSubscriptionClient = AzureSession.ClientFactory .CreateCustomClient<RDFESubscriptionClient>( new TokenCloudCredentials(commonTenantToken.AccessToken), environment.GetEndpointAsUri(AzureEnvironment.Endpoint.ServiceManagement))) { var subscriptionListResult = rdfeSubscriptionClient.Subscriptions.List(); return subscriptionListResult.Subscriptions.Select(s => s.ActiveDirectoryTenantId).Distinct().ToArray(); } } }