private void ImportAndIssueCertificateBTN_Click(object sender, EventArgs e) { try { const string caption = "Select Certificate to Import"; // set current directory. if (m_currentDirectory == null) { m_currentDirectory = Utils.GetAbsoluteDirectoryPath("%LocalApplicationData%", false, false, false); } // open file dialog. OpenFileDialog dialog = new OpenFileDialog(); dialog.CheckFileExists = true; dialog.CheckPathExists = true; dialog.DefaultExt = ".pfx"; dialog.Filter = "Certificate Files (*.der)|*.der|All Files (*.*)|*.*"; dialog.Multiselect = false; dialog.ValidateNames = true; dialog.Title = "Open Certificate File"; dialog.FileName = null; dialog.InitialDirectory = m_currentDirectory; dialog.RestoreDirectory = true; if (dialog.ShowDialog() != DialogResult.OK) { return; } FileInfo fileInfo = new FileInfo(dialog.FileName); m_currentDirectory = fileInfo.Directory.FullName; X509Certificate2 certificate = new X509Certificate2(fileInfo.FullName); if (certificate == null) { return; } CertificateStoreIdentifier store = new CertificateStoreIdentifier(); store.StoreType = ManagedStoreCTRL.StoreType; store.StorePath = ManagedStoreCTRL.StorePath; CertificateIdentifier id = new CreateCertificateDlg().ShowDialog(store, IssuerKeyFilePathTB.Text, certificate); if (id == null) { return; } certificate = id.Find(true); MessageBox.Show( this, certificate.Subject + " issued.", caption, MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception exception) { GuiUtils.HandleException(this.Text, MethodBase.GetCurrentMethod(), exception); } }
/// <summary> /// Creates a new self signed for a certificate. /// </summary> private void CreateApplicationCertificateBTN_Click(object sender, EventArgs e) { try { // get application. ManagedApplication application = ApplicationToManageCTRL.GetSelectedApplication();; if (application == null) { return; } // load the configuration. application.Reload(); // can't set application certificate for non-sdk apps. if (!application.IsSdkCompatible) { return; } // create the certificate. CertificateIdentifier certificate = new CreateCertificateDlg().ShowDialog(application.Application); if (certificate == null) { return; } // save the configuration. CertificateStoreIdentifier store = new CertificateStoreIdentifier(); store.StorePath = certificate.StorePath; store.StoreType = certificate.StoreType; m_currentStore = store; UpdateApplicationCertificate(application.Application, store, certificate.Certificate); } catch (Exception exception) { GuiUtils.HandleException(this.Text, MethodBase.GetCurrentMethod(), exception); } }
private void SelectAndIssueCertificateBTN_Click(object sender, EventArgs e) { try { const string caption = "Select Certificate to Issue"; if (m_currentStore == null) { m_currentStore = new CertificateStoreIdentifier(); m_currentStore.StoreType = Utils.DefaultStoreType; m_currentStore.StorePath = Utils.DefaultStorePath; } CertificateIdentifier id = new CertificateListDlg().ShowDialog(m_currentStore, true); if (id == null) { return; } m_currentStore.StoreType = id.StoreType; m_currentStore.StorePath = id.StorePath; X509Certificate2 certificate = id.Find(); if (certificate == null) { return; } CertificateIdentifier newId = new CreateCertificateDlg().ShowDialog(m_currentStore, IssuerKeyFilePathTB.Text, certificate); if (newId == null) { return; } X509Certificate2 newCertificate = id.Find(); MessageBox.Show( this, newCertificate.Subject + " issued.", caption, MessageBoxButtons.OK, MessageBoxIcon.Information); // check if original certificate should be deleted. if (new YesNoDlg().ShowDialog("Delete orginal certificate?", caption) == DialogResult.Yes) { ICertificateStore physicalStore = id.OpenStore(); try { physicalStore.Delete(certificate.Thumbprint); } finally { physicalStore.Close(); } } } catch (Exception exception) { GuiUtils.HandleException(this.Text, MethodBase.GetCurrentMethod(), exception); } }