1 | using System;
|
---|
2 | using System.ComponentModel;
|
---|
3 | using System.Security.Cryptography.X509Certificates;
|
---|
4 | using System.Windows.Forms;
|
---|
5 |
|
---|
6 | namespace HeuristicLab.Clients.Hive.CloudManager.Views {
|
---|
7 | public partial class AddCertificate : Form {
|
---|
8 | private BackgroundWorker worker = new BackgroundWorker();
|
---|
9 | public bool ErrorOccured { get; set; }
|
---|
10 | public string CertificateFile { get; set; }
|
---|
11 | public string CertificatePassword { get; set; }
|
---|
12 |
|
---|
13 | public AddCertificate() {
|
---|
14 | InitializeComponent();
|
---|
15 | ErrorOccured = true;
|
---|
16 |
|
---|
17 | worker.DoWork += new DoWorkEventHandler(AddCertificateTask);
|
---|
18 | worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(WorkerCompleted);
|
---|
19 | }
|
---|
20 |
|
---|
21 | private void btnBrowseCertificateFile_Click(object sender, EventArgs e) {
|
---|
22 | OpenFileDialog ofd = new OpenFileDialog();
|
---|
23 | ofd.Filter = "All Certificate Files|*.pfx";
|
---|
24 | if (ofd.ShowDialog() == DialogResult.OK) {
|
---|
25 | tbCertificateFile.Text = ofd.FileName;
|
---|
26 | }
|
---|
27 | }
|
---|
28 |
|
---|
29 | private void btnCancel_Click(object sender, EventArgs e) {
|
---|
30 | this.Close();
|
---|
31 | }
|
---|
32 |
|
---|
33 | private void btnOk_Click(object sender, EventArgs e) {
|
---|
34 | CertificateFile = tbCertificateFile.Text;
|
---|
35 | CertificatePassword = tbCertificatePassword.Text;
|
---|
36 |
|
---|
37 | if (CertificateFile.Length == 0) {
|
---|
38 | MessageBox.Show("Certificate file is required", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
---|
39 | } else if (CertificatePassword.Length == 0) {
|
---|
40 | MessageBox.Show("Password is required", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
---|
41 | } else {
|
---|
42 | SetControlsEnabled(false);
|
---|
43 | progressBar.Visible = true;
|
---|
44 | var parameters = Tuple.Create<string, string>(CertificateFile, CertificatePassword);
|
---|
45 | worker.RunWorkerAsync(parameters);
|
---|
46 | }
|
---|
47 | }
|
---|
48 |
|
---|
49 | private void SetControlsEnabled(bool isEnabled) {
|
---|
50 | tbCertificatePassword.Enabled = isEnabled;
|
---|
51 | btnBrowseCertificateFile.Enabled = isEnabled;
|
---|
52 | btnCancel.Enabled = isEnabled;
|
---|
53 | btnOk.Enabled = isEnabled;
|
---|
54 | }
|
---|
55 |
|
---|
56 | private void AddCertificateTask(object sender, DoWorkEventArgs e) {
|
---|
57 | Tuple<string, string> parameters = e.Argument as Tuple<string, string>;
|
---|
58 | string file = parameters.Item1;
|
---|
59 | string pw = parameters.Item2;
|
---|
60 |
|
---|
61 | X509Certificate cert = new X509Certificate(file, pw);
|
---|
62 | }
|
---|
63 |
|
---|
64 | private void WorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
|
---|
65 | progressBar.Visible = false;
|
---|
66 |
|
---|
67 | if (e.Error == null) {
|
---|
68 | ErrorOccured = false;
|
---|
69 | this.Close();
|
---|
70 | } else {
|
---|
71 | MessageBox.Show(e.Error.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
---|
72 | CertificateFile = string.Empty;
|
---|
73 | CertificateFile = string.Empty;
|
---|
74 | ErrorOccured = true;
|
---|
75 | this.Show();
|
---|
76 | SetControlsEnabled(true);
|
---|
77 | }
|
---|
78 | }
|
---|
79 | }
|
---|
80 | }
|
---|