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) { PluginInfrastructure.ErrorHandling.ShowErrorDialog(new Exception("Certificate file is required")); } else if (CertificatePassword.Length == 0) { PluginInfrastructure.ErrorHandling.ShowErrorDialog(new Exception("Password is required")); } 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 { PluginInfrastructure.ErrorHandling.ShowErrorDialog(e.Error); CertificateFile = string.Empty; CertificateFile = string.Empty; ErrorOccured = true; this.Show(); SetControlsEnabled(true); } } } }