-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathNewOrEditCertificateDialog.xaml.cs
157 lines (141 loc) · 5.84 KB
/
NewOrEditCertificateDialog.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using AutomationISE.Model;
using System.Security.Cryptography.X509Certificates;
using System.IO;
namespace AutomationISE
{
/// <summary>
/// Interaction logic for NewOrEditCertificateDialog.xaml
/// </summary>
public partial class NewOrEditCertificateDialog : Window
{
private string _certPath;
private string _password;
private string _thumbprint;
private bool _encrypted;
private bool _exportable;
private string _certBase64;
public string certPath { get { return _certPath; } }
public string thumbprint { get { return _thumbprint; } }
public string password { get { return _password; } }
public bool encrypted { get { return _encrypted; } }
public bool exportable { get { return _exportable; } }
public string certBase64 { get { return _certBase64; } }
public NewOrEditCertificateDialog(AutomationCertificate cert)
{
try
{
InitializeComponent();
exportableComboBox.Items.Clear();
exportableComboBox.Items.Add(true);
exportableComboBox.Items.Add(false);
exportableComboBox.SelectedItem = true;
if (cert != null)
{
PasswordTextbox.Password = cert.getPassword();
certificatePathTextbox.Text = cert.getCertPath();
exportableComboBox.SelectedItem = cert.getExportable();
// If certificate is a .cer file, grey out the password & exportable
if (Path.GetExtension(_certPath) == ".cer")
{
PasswordTextbox.Password = null;
exportableComboBox.SelectedItem = false;
this.PasswordTextbox.IsEnabled = false;
this.exportableComboBox.IsEnabled = false;
_encrypted = false;
}
else
{
this.PasswordTextbox.IsEnabled = true;
this.exportableComboBox.IsEnabled = true;
_encrypted = true;
}
this.Title = "Edit Certificate Asset";
}
else
{
this.Title = "New Certificate Asset";
}
}
catch (Exception exception)
{
System.Windows.Forms.MessageBox.Show(exception.Message, "Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
}
}
private void OkButton_Click(object sender, RoutedEventArgs e)
{
try
{
_password = PasswordTextbox.Password;
_exportable = bool.Parse(exportableComboBox.SelectedItem.ToString());
_certPath = certificatePathTextbox.Text;
_thumbprint = importCertificate();
this.DialogResult = true;
}
catch (Exception exception)
{
System.Windows.Forms.MessageBox.Show(exception.Message, "Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
}
}
private void buttonCertificate_Click(object sender, RoutedEventArgs e)
{
var dialog = new System.Windows.Forms.OpenFileDialog();
System.Windows.Forms.DialogResult result = dialog.ShowDialog();
_certPath = dialog.FileName;
this.certificatePathTextbox.Text = _certPath;
// If certificate is a .cer file, grey out the password & exportable
if (Path.GetExtension(_certPath) == ".cer")
{
PasswordTextbox.Password = null;
exportableComboBox.SelectedItem = exportableComboBox.Items[1];
this.PasswordTextbox.IsEnabled = false;
this.exportableComboBox.IsEnabled = false;
_encrypted = false;
}
else
{
this.PasswordTextbox.IsEnabled = true;
this.exportableComboBox.IsEnabled = true;
_encrypted = true;
}
}
private string importCertificate()
{
X509Certificate2 cert = null;
try
{
// Load the certificate into the users current store
cert = new X509Certificate2();
if (Path.GetExtension(_certPath) == ".cer")
{
cert.Import(_certPath);
}
else
{
if (_exportable) cert.Import(_certPath, _password, X509KeyStorageFlags.Exportable | X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet);
else cert.Import(_certPath, _password, X509KeyStorageFlags.DefaultKeySet);
}
var store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadWrite);
store.Add(cert);
store.Close();
_certBase64 = Convert.ToBase64String(cert.Export(X509ContentType.Cert));
}
catch (Exception exception)
{
System.Windows.Forms.MessageBox.Show(exception.Message, "Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
return null;
}
return cert.Thumbprint;
}
private void exportableComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
}
private void Button_Click(object sender, RoutedEventArgs e)
{
}
}
}