zoukankan      html  css  js  c++  java
  • 利用C#检测证书是否存在,并安装证书

    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("结束");
  • 相关阅读:
    C#
    C#
    C#
    python——socket网络编程
    Python——面向对象
    Python——函数
    Python——列表深浅拷贝
    Python——文件操作
    多级菜单(增强版)
    Python 编码机制
  • 原文地址:https://www.cnblogs.com/lyghost/p/2699389.html
Copyright © 2011-2022 走看看