利用C#检测证书是否存在,并安装证书

本文介绍如何使用C#检查本地计算机中是否存在指定名称的证书,并提供安装个人证书及CA根证书到对应存储的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C#检查证书是否存在

1 X509Store store = new X509Store(StoreName.Root, StoreLocation.LocalMachine);
2             store.Open(OpenFlags.MaxAllowed);
3             X509Certificate2Collection certs = store.Certificates.Find(X509FindType.FindBySubjectName, "SRCA", false);
4             if (certs.Count == 0 || certs[0].NotAfter < DateTime.Now)
5             {
6                 Console.WriteLine("需要创建证书");
7             }

安装证书:

 1 using System.Security.Cryptography.X509Certificates;
 2 
 3  
 4 
 5 MessageBox.Show("开始");
 6 
 7  
 8 
 9 //添加个人证书
10 
11 X509Certificate2 certificate = new X509Certificate2(Application.StartupPath + \\cert.pfx","证书密码");
12 X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
13 store.Open(OpenFlags.ReadWrite);
14 store.Remove(certificate);   //可省略
15 store.Add(certificate);
16 store.Close();
17 
18 
19  
20 
21 //安装CA的根证书到受信任根证书颁发机构
22 certificate = new X509Certificate2(Application.StartupPath + "\\CA.cer");
23 store = new X509Store(StoreName.Root, StoreLocation.LocalMachine);
24 store.Open(OpenFlags.ReadWrite);
25 store.Remove(certificate);   //可省略
26 store.Add(certificate);
27 store.Close();
28 
29            
30 MessageBox.Show("结束");

 

转载于:https://www.cnblogs.com/lyghost/archive/2012/09/23/2699389.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值