public async Task GetToken(AzureSubscription azureSubscription) { _LogProvider.WriteLog("GetToken", "Start token request"); if (azureSubscription == null) { _LogProvider.WriteLog("GetToken", "Azure Subscription cannot be null."); throw new ArgumentNullException("Azure Subscription cannot be null."); } _LogProvider.WriteLog("GetToken", "Azure Subscription: " + azureSubscription.ToString()); string authenticationUrl = AzureServiceUrls.GetAzureLoginUrl(azureSubscription.AzureEnvironment) + azureSubscription.AzureAdTenantId.ToString(); _LogProvider.WriteLog("GetToken", "Authentication Url: " + authenticationUrl); _AuthenticationResult = null; AuthenticationContext context = new AuthenticationContext(authenticationUrl); PlatformParameters platformParams = new PlatformParameters(PromptBehavior.Auto, null); _AuthenticationResult = await context.AcquireTokenAsync(AzureServiceUrls.GetASMServiceManagementUrl(azureSubscription.AzureEnvironment), strClientId, new Uri(strReturnUrl), platformParams); _LogProvider.WriteLog("GetToken", "End token request"); }
/// <summary> /// Get Azure Subscriptions within the provided Azure Tenant /// </summary> /// <param name="azureTenant">Azure Tenant for which Azure Subscriptions should be retrieved</param> /// <param name="allowRestCacheUse">False in production use so that Azure Token Content is Tenant specific. True in Unit Tests to allow offline (no actual URL querying).</param> /// <returns></returns> public async Task <List <AzureSubscription> > GetAzureARMSubscriptions(AzureContext azureContext, bool allowRestCacheUse = false) { azureContext.LogProvider.WriteLog("GetAzureARMSubscriptions", "Start - azureTenant: " + this.ToString()); azureContext.StatusProvider.UpdateStatus("BUSY: Getting Auth Token to Query Subscriptions"); String subscriptionsUrl = azureContext.AzureEnvironment.ResourceManagerEndpoint + "subscriptions?api-version=2015-01-01"; AuthenticationResult authenticationResult = await azureContext.TokenProvider.GetToken(azureContext.AzureEnvironment.ResourceManagerEndpoint, this.TenantId); azureContext.StatusProvider.UpdateStatus("BUSY: Querying Subscriptions"); AzureRestRequest azureRestRequest = new AzureRestRequest(subscriptionsUrl, authenticationResult, "GET", allowRestCacheUse); AzureRestResponse azureRestResponse = await azureContext.AzureRetriever.GetAzureRestResponse(azureRestRequest); JObject subscriptionsJson = JObject.Parse(azureRestResponse.Response); var subscriptions = from subscription in subscriptionsJson["value"] select subscription; azureContext.StatusProvider.UpdateStatus("BUSY: Instantiating Subscriptions"); List <AzureSubscription> azureSubscriptions = new List <AzureSubscription>(); foreach (JObject azureSubscriptionJson in subscriptions) { AzureSubscription azureSubscription = new AzureSubscription(azureSubscriptionJson, this, azureContext.AzureEnvironment, azureContext.GetARMServiceManagementUrl(), azureContext.GetARMTokenResourceUrl()); azureSubscriptions.Add(azureSubscription); azureContext.StatusProvider.UpdateStatus("BUSY: Loaded Subscription " + azureSubscription.ToString()); } azureContext.StatusProvider.UpdateStatus("BUSY: Getting Subscriptions Completed"); return(azureSubscriptions); }