private void buttonSaveToList_Click(object sender, EventArgs e)
        {
            LoginCredentials = GenerateLoginCredentials;

            // New code for JSON
            if (string.IsNullOrEmpty(LoginCredentials.ReturnAccountName()))
            {
                MessageBox.Show(AMSExplorer.Properties.Resources.AMSLogin_buttonSaveToList_Click_TheAccountNameCannotBeEmpty);
                return;
            }

            var entryWithSameName = CredentialList.MediaServicesAccounts.Where(c => c.ReturnAccountName().ToLower().Trim() == LoginCredentials.ReturnAccountName().ToLower().Trim()).FirstOrDefault();

            // if found the same name
            if (entryWithSameName != null)
            {
                CredentialList.MediaServicesAccounts[CredentialList.MediaServicesAccounts.IndexOf(entryWithSameName)] = LoginCredentials;
            }
            else
            {
                CredentialList.MediaServicesAccounts.Add(LoginCredentials);
                //listViewAccounts.Items.Add(ReturnAccountName(LoginCredentials));
                AddItemToListviewAccounts(LoginCredentials);
            }
            Properties.Settings.Default.LoginListJSON = JsonConvert.SerializeObject(CredentialList);
            Program.SaveAndProtectUserConfig();
        }
        private void AddItemToListviewAccounts(CredentialsEntry c)
        {
            var item = listViewAccounts.Items.Add(c.ReturnAccountName());

            if (!c.UseAADInteract && !c.UseAADServicePrincipal)
            {
                listViewAccounts.Items[item.Index].ForeColor   = Color.Red;
                listViewAccounts.Items[item.Index].ToolTipText = "Configured for deprecated ACS authentication. Please use Azure Active Directory.";
            }
            else
            {
                listViewAccounts.Items[item.Index].ForeColor   = Color.Black;
                listViewAccounts.Items[item.Index].ToolTipText = null;
            }
        }
        private void listViewAccounts_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoginCredentials = GenerateLoginCredentials;

            buttonDeleteAccountEntry.Enabled = (listViewAccounts.SelectedIndices.Count > 0); // no selected item, so login button not active
            buttonExport.Enabled             = (listViewAccounts.Items.Count > 0);
            if (listViewAccounts.SelectedIndices.Count > 0)                                  // one selected
            {
                if (LoginCredentials != null)
                {
                    var entryWithSameName = CredentialList.MediaServicesAccounts.Where(c => c.ReturnAccountName().ToLower().Trim() == LoginCredentials.ReturnAccountName().ToLower().Trim()).FirstOrDefault();

                    if (entryWithSameName != null && !LoginCredentials.Equals(entryWithSameName))
                    {
                        var result = MessageBox.Show(string.Format(AMSExplorer.Properties.Resources.AMSLogin_buttonLogin_Click_DoYouWantToUpdateTheCredentialsFor0, LoginCredentials.ReturnAccountName()), AMSExplorer.Properties.Resources.AMSLogin_listBoxAccounts_SelectedIndexChanged_UpdateCredentials, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
                        if (result == DialogResult.Yes) // ok to update the credentials
                        {
                            CredentialList.MediaServicesAccounts[CredentialList.MediaServicesAccounts.IndexOf(entryWithSameName)] = LoginCredentials;
                            Properties.Settings.Default.LoginListJSON = JsonConvert.SerializeObject(CredentialList);
                            Program.SaveAndProtectUserConfig();
                        }
                    }
                }

                var entry = CredentialList.MediaServicesAccounts[listViewAccounts.SelectedIndices[0]];

                radioButtonAADAut.Checked = entry.UseAADInteract || entry.UseAADServicePrincipal;
                radioButtonAADServicePrincipal.Checked = entry.UseAADServicePrincipal;
                radioButtonAADInteractive.Checked      = !radioButtonAADServicePrincipal.Checked;

                if (entry.ADCustomSettings != null)  // custom settings
                {
                    radioButtonAADOther.Checked          = true;
                    comboBoxAADMappingList.SelectedIndex = comboBoxAADMappingList.Items.Count - 1; // last one

                    textBoxAADAMSResource.Text      = entry.ADCustomSettings.MediaServicesResource;
                    textBoxAADClienid.Text          = entry.ADCustomSettings.MediaServicesSdkClientId;
                    textBoxAADRedirect.Text         = entry.ADCustomSettings.MediaServicesSdkRedirectUri.ToString();
                    textBoxAADAzureEndpoint.Text    = entry.ADCustomSettings.ActiveDirectoryEndpoint.ToString();
                    textBoxAADManagementPortal.Text = entry.OtherManagementPortal;
                }
                else if (entry.ADDeploymentName != null)
                {
                    radioButtonAADOther.Checked = true;
                    int index = 0;
                    foreach (var it in comboBoxAADMappingList.Items)
                    {
                        if (((Item)it).Value == entry.ADDeploymentName)
                        {
                            break;
                        }
                        index++;
                    }
                    comboBoxAADMappingList.SelectedIndex = index;
                }
                else
                {
                    radioButtonAADProd.Checked = true;
                }

                textBoxAccountName.Text      = entry.AccountName;
                textBoxAccountKey.Text       = entry.AccountKey;
                textBoxAADtenant.Text        = entry.ADTenantDomain;
                textBoxRestAPIEndpoint.Text  = entry.ADRestAPIEndpoint;
                textBoxBlobKey.Text          = entry.DefaultStorageKey;
                textBoxDescription.Text      = entry.Description;
                radioButtonACSAut.Checked    = !entry.UseAADInteract && !entry.UseAADServicePrincipal;;
                radioButtonAADAut.Checked    = entry.UseAADInteract || entry.UseAADServicePrincipal;
                radioButtonPartner.Checked   = entry.UsePartnerAPI;
                radioButtonOther.Checked     = entry.UseOtherAPI;
                textBoxAPIServer.Text        = entry.OtherAPIServer;
                textBoxScope.Text            = entry.OtherScope;
                textBoxACSBaseAddress.Text   = entry.OtherACSBaseAddress;
                textBoxAzureEndpoint.Text    = entry.OtherAzureEndpoint;
                textBoxManagementPortal.Text = entry.OtherManagementPortal;

                // if not partner or other, then defaut
                if (!radioButtonPartner.Checked && !radioButtonOther.Checked)
                {
                    radioButtonProd.Checked = true;
                }

                // to clear or set the error
                CheckTextBox((object)textBoxAccountName);
                CheckTextBox((object)textBoxAccountKey);
                CheckTextBox((object)textBoxAADtenant);
                CheckTextBox((object)textBoxRestAPIEndpoint);

                UpdateTexboxUI();
            }
        }
        private void buttonLogin_Click(object sender, EventArgs e)
        {
            LoginCredentials = GenerateLoginCredentials;

            if (string.IsNullOrEmpty(LoginCredentials.ReturnAccountName()))
            {
                MessageBox.Show(string.Format("The {0} cannot be empty.", labelE1.Text), "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            var accName = LoginCredentials.ReturnAccountName();

            var entryWithSameName = CredentialList.MediaServicesAccounts.Where(c => c.ReturnAccountName().ToLower().Trim() == accName.ToLower().Trim()).FirstOrDefault();

            // if found the same name
            if (entryWithSameName == null)  // not found
            {
                var result = MessageBox.Show(string.Format(AMSExplorer.Properties.Resources.AMSLogin_buttonLogin_Click_DoYouWantToSaveTheCredentialsFor0, accName), AMSExplorer.Properties.Resources.AMSLogin_buttonLogin_Click_SaveCredentials, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
                if (result == DialogResult.Yes) // ok to save
                {
                    CredentialList.MediaServicesAccounts.Add(LoginCredentials);
                    Properties.Settings.Default.LoginListJSON = JsonConvert.SerializeObject(CredentialList);
                    Program.SaveAndProtectUserConfig();

                    AddItemToListviewAccounts(LoginCredentials);
                    //listViewAccounts.Items.Add(ReturnAccountName(LoginCredentials));
                }
                else if (result == DialogResult.Cancel)
                {
                    return;
                }
            }
            else // found
            {
                if (!LoginCredentials.Equals(entryWithSameName)) // changed ?
                {
                    var result = MessageBox.Show(string.Format(AMSExplorer.Properties.Resources.AMSLogin_buttonLogin_Click_DoYouWantToUpdateTheCredentialsFor0, accName), AMSExplorer.Properties.Resources.AMSLogin_listBoxAccounts_SelectedIndexChanged_UpdateCredentials, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
                    if (result == DialogResult.Yes) // ok to update the credentials
                    {
                        CredentialList.MediaServicesAccounts[CredentialList.MediaServicesAccounts.IndexOf(entryWithSameName)] = LoginCredentials;
                        Properties.Settings.Default.LoginListJSON = JsonConvert.SerializeObject(CredentialList);
                        Program.SaveAndProtectUserConfig();
                    }
                    else if (result == DialogResult.Cancel)
                    {
                        return;
                    }
                }
            }

            if (LoginCredentials.UseAADServicePrincipal)  // service principal mode
            {
                var spcrendentialsform = new AMSLoginServicePrincipal();
                if (spcrendentialsform.ShowDialog() == DialogResult.OK)
                {
                    LoginCredentials.ADSPClientId     = spcrendentialsform.ClientId;
                    LoginCredentials.ADSPClientSecret = spcrendentialsform.ClientSecret;
                }
                else
                {
                    return;
                }
            }

            // OLD ACS Mode - let's warm the user
            if (!LoginCredentials.UseAADServicePrincipal && !LoginCredentials.UseAADInteract)
            {
                var f = new DisplayBox("Warning", AMSExplorer.Properties.Resources.AMSLogin_buttonLogin_Click_ACSAuthenticationWarning, 10);
                f.ShowDialog();
            }

            // Context creation
            this.Cursor = Cursors.WaitCursor;

            context = Program.ConnectAndGetNewContext(LoginCredentials, false, true);

            accName = LoginCredentials.ReturnAccountName();

            try
            {
                var a = context.Assets.FirstOrDefault();
                this.Cursor = Cursors.Default;
            }
            catch (Exception ex)
            {
                MessageBox.Show(Program.GetErrorMessage(ex), "Login error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.Cursor = Cursors.Default;
                return;
            }

            accountName       = accName;
            this.DialogResult = DialogResult.OK;  // form will close with OK result
                                                  // else --> form won't close...
        }
Ejemplo n.º 5
0
 private void AttachStorage_Load(object sender, EventArgs e)
 {
     groupBoxAMSAcct.Text = string.Format(groupBoxAMSAcct.Text, _credentials.ReturnAccountName());
 }