using System; using System.ComponentModel; using System.Security.Cryptography.X509Certificates; using System.Windows.Forms; namespace HeuristicLab.Clients.Hive.CloudManager.Views { public partial class AddCertificate : Form { private BackgroundWorker worker = new BackgroundWorker(); public bool ErrorOccured { get; set; } public string CertificateFile { get; set; } public string CertificatePassword { get; set; } public AddCertificate() { InitializeComponent(); ErrorOccured = true; worker.DoWork += new DoWorkEventHandler(AddCertificateTask); worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(WorkerCompleted); } private void btnBrowseCertificateFile_Click(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = "All Certificate Files|*.pfx"; if (ofd.ShowDialog() == DialogResult.OK) { tbCertificateFile.Text = ofd.FileName; } } private void btnCancel_Click(object sender, EventArgs e) { this.Close(); } private void btnOk_Click(object sender, EventArgs e) { CertificateFile = tbCertificateFile.Text; CertificatePassword = tbCertificatePassword.Text; if (CertificateFile.Length == 0) { MessageBox.Show("Certificate file is required", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } else if (CertificatePassword.Length == 0) { MessageBox.Show("Password is required", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { SetControlsEnabled(false); progressBar.Visible = true; var parameters = Tuple.Create(CertificateFile, CertificatePassword); worker.RunWorkerAsync(parameters); } } private void SetControlsEnabled(bool isEnabled) { tbCertificatePassword.Enabled = isEnabled; btnBrowseCertificateFile.Enabled = isEnabled; btnCancel.Enabled = isEnabled; btnOk.Enabled = isEnabled; } private void AddCertificateTask(object sender, DoWorkEventArgs e) { Tuple parameters = e.Argument as Tuple; string file = parameters.Item1; string pw = parameters.Item2; X509Certificate cert = new X509Certificate(file, pw); } private void WorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { progressBar.Visible = false; if (e.Error == null) { ErrorOccured = false; this.Close(); } else { MessageBox.Show(e.Error.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); CertificateFile = string.Empty; CertificateFile = string.Empty; ErrorOccured = true; this.Show(); SetControlsEnabled(true); } } } }