示例#1
0
        private void m_pSslToolbar_ItemClicked(object sender,ToolStripItemClickedEventArgs e)
        {
            if(e.ClickedItem.Name == null){
            }
            else if(e.ClickedItem.Name == "create"){
                wfrm_sys_CreateCertificate dlgSsl = new wfrm_sys_CreateCertificate(m_pHostName.Text);
                if(dlgSsl.ShowDialog(this) == DialogResult.OK){
                    try{
                        X509Certificate2 cert = new X509Certificate2(dlgSsl.Certificate,"",X509KeyStorageFlags.Exportable);
                        if(!cert.HasPrivateKey){
                            MessageBox.Show(this,"Certificate is not server certificate, private key is missing !","Error:",MessageBoxButtons.OK,MessageBoxIcon.Error);
                            return;
                        }

                        m_pCert = cert;

                        UpdateCertStatus();
                    }
                    catch{
                        MessageBox.Show(this,"Invalid or not supported certificate file !","Error:",MessageBoxButtons.OK,MessageBoxIcon.Error);
                    }
                }
            }
            else if(e.ClickedItem.Name == "add"){
                OpenFileDialog dlg = new OpenFileDialog();
                if(dlg.ShowDialog(this) == DialogResult.OK){
                    try{
                        X509Certificate2 cert = new X509Certificate2(dlg.FileName,"",X509KeyStorageFlags.Exportable);
                        if(!cert.HasPrivateKey){
                            MessageBox.Show(this,"Certificate is not server certificate, private key is missing !","Error:",MessageBoxButtons.OK,MessageBoxIcon.Error);
                            return;
                        }

                        m_pCert = cert;

                        UpdateCertStatus();
                    }
                    catch{
                        MessageBox.Show(this,"Invalid or not supported certificate file !","Error:",MessageBoxButtons.OK,MessageBoxIcon.Error);
                    }
                }
            }
            else if(e.ClickedItem.Name == "delete"){
                if(MessageBox.Show(this,"Are you sure you want to delete active SSL certificate ?","Confirm delete:",MessageBoxButtons.YesNo,MessageBoxIcon.Warning) == DialogResult.Yes){
                    m_pCert = null;

                    UpdateCertStatus();
                }
            }
            else if(e.ClickedItem.Name == "save"){
                SaveFileDialog dlg = new SaveFileDialog();
                dlg.Filter = "*.pfx | *.p12";
                if(dlg.ShowDialog(this) == DialogResult.OK){
                    File.WriteAllBytes(dlg.FileName,m_pCert.Export(X509ContentType.Pfx));
                }
            }
        }
示例#2
0
        private void m_pSslToolbar_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            if (e.ClickedItem.Name == null)
            {
            }
            else if (e.ClickedItem.Name == "create")
            {
                wfrm_sys_CreateCertificate dlgSsl = new wfrm_sys_CreateCertificate(m_pHostName.Text);
                if (dlgSsl.ShowDialog(this) == DialogResult.OK)
                {
                    try{
                        X509Certificate2 cert = new X509Certificate2(dlgSsl.Certificate, "", X509KeyStorageFlags.Exportable);
                        if (!cert.HasPrivateKey)
                        {
                            MessageBox.Show(this, "Certificate is not server certificate, private key is missing !", "Error:", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }

                        m_pCert = cert;

                        UpdateCertStatus();
                    }
                    catch {
                        MessageBox.Show(this, "Invalid or not supported certificate file !", "Error:", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
            else if (e.ClickedItem.Name == "add")
            {
                OpenFileDialog dlg = new OpenFileDialog();
                if (dlg.ShowDialog(this) == DialogResult.OK)
                {
                    try{
                        X509Certificate2 cert = new X509Certificate2(dlg.FileName, "", X509KeyStorageFlags.Exportable);
                        if (!cert.HasPrivateKey)
                        {
                            MessageBox.Show(this, "Certificate is not server certificate, private key is missing !", "Error:", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }

                        m_pCert = cert;

                        UpdateCertStatus();
                    }
                    catch {
                        MessageBox.Show(this, "Invalid or not supported certificate file !", "Error:", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
            else if (e.ClickedItem.Name == "delete")
            {
                if (MessageBox.Show(this, "Are you sure you want to delete active SSL certificate ?", "Confirm delete:", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
                {
                    m_pCert = null;

                    UpdateCertStatus();
                }
            }
            else if (e.ClickedItem.Name == "save")
            {
                SaveFileDialog dlg = new SaveFileDialog();
                dlg.Filter = "*.pfx | *.p12";
                if (dlg.ShowDialog(this) == DialogResult.OK)
                {
                    File.WriteAllBytes(dlg.FileName, m_pCert.Export(X509ContentType.Pfx));
                }
            }
        }