public AddAMSAccount2Browse(TokenCredentials credentials, IPage <Subscription> subscriptions, AzureEnvironmentV3 environment, myTenant[] myTenants, IPlatformParameters parameters) { InitializeComponent(); this.Icon = Bitmaps.Azure_Explorer_ico; this.credentials = credentials; this.subscriptions = subscriptions; this.environment = environment; _myTenants = myTenants; _parameters = parameters; }
private async void buttonPickupAccount_Click(object sender, EventArgs e) { var addaccount1 = new AddAMSAccount1(); if (addaccount1.ShowDialog() == DialogResult.OK) { if (addaccount1.SelectedMode == AddAccountMode.BrowseSubscriptions) { environment = addaccount1.GetEnvironment(); var authContext = new AuthenticationContext( // authority: environment.Authority, authority: environment.AADSettings.AuthenticationEndpoint.ToString() + "common", validateAuthority: true ); AuthenticationResult accessToken; try { accessToken = await authContext.AcquireTokenAsync( resource: environment.AADSettings.TokenAudience.ToString(), clientId: environment.ClientApplicationId, redirectUri: new Uri("urn:ietf:wg:oauth:2.0:oob"), parameters: new PlatformParameters(addaccount1.SelectUser ? PromptBehavior.SelectAccount : PromptBehavior.Auto) ); } catch (Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } var credentials = new TokenCredentials(accessToken.AccessToken, "Bearer"); var subscriptionClient = new SubscriptionClient(environment.ArmEndpoint, credentials); var subscriptions = subscriptionClient.Subscriptions.List(); // tenants browsing var tenants = new myTenants(); string URL = environment.ArmEndpoint + "tenants?api-version=2017-08-01"; HttpClient client = new HttpClient(); client.DefaultRequestHeaders.Remove("Authorization"); client.DefaultRequestHeaders.Add("Authorization", "Bearer " + accessToken.AccessToken); HttpResponseMessage response = await client.GetAsync(URL); if (response.IsSuccessStatusCode) { var str = await response.Content.ReadAsStringAsync(); tenants = (myTenants)JsonConvert.DeserializeObject(str, typeof(myTenants)); } var addaccount2 = new AddAMSAccount2Browse(credentials, subscriptions, environment, tenants.value, new PlatformParameters(addaccount1.SelectUser ? PromptBehavior.SelectAccount : PromptBehavior.Auto)); if (addaccount2.ShowDialog() == DialogResult.OK) { // Getting Media Services accounts... var mediaServicesClient = new AzureMediaServicesClient(environment.ArmEndpoint, credentials); var entry = new CredentialsEntryV3(addaccount2.selectedAccount, environment, addaccount1.SelectUser ? PromptBehavior.SelectAccount : PromptBehavior.Auto, false, addaccount2.selectedTenantId, false ); CredentialList.MediaServicesAccounts.Add(entry); AddItemToListviewAccounts(entry); SaveCredentialsToSettings(); } else { return; } } // Get info from the Azure CLI JSON else if (addaccount1.SelectedMode == AddAccountMode.FromAzureCliJson) { string example = @"{ ""AadClientId"": ""00000000-0000-0000-0000-000000000000"", ""AadEndpoint"": ""https://login.microsoftonline.com"", ""AadSecret"": ""00000000-0000-0000-0000-000000000000"", ""AadTenantId"": ""00000000-0000-0000-0000-000000000000"", ""AccountName"": ""amsaccount"", ""ArmAadAudience"": ""https://management.core.windows.net/"", ""ArmEndpoint"": ""https://management.azure.com/"", ""Region"": ""West Europe"", ""ResourceGroup"": ""amsResourceGroup"", ""SubscriptionId"": ""00000000-0000-0000-0000-000000000000"" }"; var form = new EditorXMLJSON("Enter the JSON output of Azure Cli Service Principal creation (az ams account sp create)", example, true, false, true, "The Service Principal secret is stored encrypted in the application settings."); if (form.ShowDialog() == DialogResult.OK) { JsonFromAzureCli json = null; try { json = (JsonFromAzureCli)JsonConvert.DeserializeObject(form.TextData, typeof(JsonFromAzureCli)); } catch (Exception ex) { MessageBox.Show(ex.Message, "Error reading the json", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } string resourceId = string.Format("/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.Media/mediaservices/{2}", json.SubscriptionId, json.ResourceGroup, json.AccountName); string AADtenantId = json.AadTenantId; var aadSettings = new ActiveDirectoryServiceSettings() { AuthenticationEndpoint = json.AadEndpoint, TokenAudience = json.ArmAadAudience, ValidateAuthority = true }; var env = new AzureEnvironmentV3(AzureEnvType.Custom) { AADSettings = aadSettings, ArmEndpoint = json.ArmEndpoint }; var entry = new CredentialsEntryV3( new SubscriptionMediaService(resourceId, json.AccountName, null, null, json.Region), env, PromptBehavior.Auto, true, AADtenantId, false ) { ADSPClientId = json.AadClientId, ClearADSPClientSecret = json.AadSecret }; CredentialList.MediaServicesAccounts.Add(entry); AddItemToListviewAccounts(entry); SaveCredentialsToSettings(); } else { return; } } else if (addaccount1.SelectedMode == AddAccountMode.ManualEntry) { var form = new AddAMSAccount2Manual(); if (form.ShowDialog() == DialogResult.OK) { string accountnamecc = form.textBoxAMSResourceId.Text.Split('/').Last(); var entry = new CredentialsEntryV3( new SubscriptionMediaService(form.textBoxAMSResourceId.Text, accountnamecc, null, null, form.textBoxLocation.Text), addaccount1.GetEnvironment(), PromptBehavior.Auto, form.radioButtonAADServicePrincipal.Checked, form.textBoxAADtenantId.Text, true ); CredentialList.MediaServicesAccounts.Add(entry); AddItemToListviewAccounts(entry); SaveCredentialsToSettings(); } else return; } } }