-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathTest.cs
More file actions
77 lines (71 loc) · 3.02 KB
/
Test.cs
File metadata and controls
77 lines (71 loc) · 3.02 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
namespace RootCert
{
public class Class1
{
public void InstallRootCert()
{
string file = "mytest.pfx"; // Contains name of certificate file
X509Store store = new X509Store(StoreName.Root);
store.Open(OpenFlags.ReadWrite);
// BAD: adding a certificate to the Root store
store.Add(new X509Certificate2(X509Certificate2.CreateFromCertFile(file)));
store.Close();
}
public void InstallRootCert2()
{
string file = "mytest.pfx"; // Contains name of certificate file
X509Store store = new X509Store(StoreName.Root, StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadWrite);
// BAD: adding a certificate to the Root store
store.Add(new X509Certificate2(X509Certificate2.CreateFromCertFile(file)));
store.Close();
}
public void InstallUserCert()
{
string file = "mytest.pfx"; // Contains name of certificate file
X509Store store = new X509Store(StoreName.My);
store.Open(OpenFlags.ReadWrite);
// GOOD: adding a certificate to My store
store.Add(new X509Certificate2(X509Certificate2.CreateFromCertFile(file)));
store.Close();
}
public void RemoveUserCert()
{
string file = "mytest.pfx"; // Contains name of certificate file
X509Store store = new X509Store(StoreName.My);
store.Open(OpenFlags.ReadWrite);
// GOOD: removing a certificate from My store
store.Remove(new X509Certificate2(X509Certificate2.CreateFromCertFile(file)));
store.Close();
}
public void RemoveRootCert()
{
string file = "mytest.pfx"; // Contains name of certificate file
X509Store store = new X509Store(StoreName.Root);
store.Open(OpenFlags.ReadWrite);
// GOOD: removing a certificate from Root store
store.Remove(new X509Certificate2(X509Certificate2.CreateFromCertFile(file)));
store.Close();
}
public void InstallRootCertRange()
{
string file1 = "mytest1.pfx"; // Contains name of certificate file
string file2 = "mytest2.pfx"; // Contains name of certificate file
var certCollection = new X509Certificate2[] {
new X509Certificate2(X509Certificate2.CreateFromCertFile(file1)),
new X509Certificate2(X509Certificate2.CreateFromCertFile(file2)),
};
X509Store store = new X509Store(StoreName.Root, StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadWrite);
// BAD: adding multiple certificates to the Root store
store.AddRange(new X509Certificate2Collection(certCollection));
store.Close();
}
}
}