Beispiel #1
0
        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);
            }
        }