方式一,直接导入到证书管理器中,可选中导入地点,是否可导出等选项。

 1 /// <summary>
 2         /// 证书导入
 3         /// </summary>
 4         public static bool ImportCert(string certPath, string certPwd)
 5         {
 6             try
 7             {
 8                 //添加个人证书
 9                 X509Certificate2 certificate = new X509Certificate2(certPath, certPwd, X509KeyStorageFlags.Exportable | X509KeyStorageFlags.UserKeySet | X509KeyStorageFlags.PersistKeySet);
10                 X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
11                 store.Open(OpenFlags.ReadWrite);
12                 store.Remove(certificate);//可省略
13                 store.Add(certificate);
14                 store.Close();
15                 return true;
16             }
17             catch
18             {
19                 return false;
20             }
21         }
22 
23 /// <summary>
24         /// 证书导入
25         /// </summary>
26         public static bool ImportCert(byte[] certBytes, string certPwd)
27         {
28             try
29             {
30                 //添加个人证书
31                 X509Certificate2 certificate = new X509Certificate2(certBytes, certPwd, X509KeyStorageFlags.Exportable | X509KeyStorageFlags.UserKeySet | X509KeyStorageFlags.PersistKeySet);
32                 X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
33                 store.Open(OpenFlags.ReadWrite);
34                 store.Remove(certificate);//可省略
35                 store.Add(certificate);
36                 store.Close();
37                 return true;
38             }
39             catch
40             {
41                 return false;
42             }
43         }
c#直接导入方式

相关文章: