C#开发快速导入ssl安全证书

首先需要引入命名空间:  System.Security.Cryptography.X509Certificates

首先需要了解:

<h1 class="title" style="font-family: 'Microsoft YaHei UI', 'Microsoft YaHei', SimSun, 'Segoe UI', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif; font-weight: normal; font-size: 2.769em; line-height: 1.167; margin-top: 0px;">X509Certificate 类</h1>

提供帮助使用 X.509 v.3 证书的方法。

参考:https://msdn.microsoft.com/zh-cn/library/9yc7tebx

X509Store 构造函数 (StoreName, StoreLocation)

使用指定的  StoreName  和  StoreLocation  值初始化  X509Store  类的新实例。

参考:https://msdn.microsoft.com/zh-cn/library/ms148581.aspx

特别注意以下参数

StoreName 枚举

  成员名称 说明
  AddressBook 其他用户的 X.509 证书存储区。
  AuthRoot 第三方证书颁发机构 (CA) 的 X.509 证书存储区。
  CertificateAuthority 中间证书颁发机构 (CA) 的 X.509 证书存储区。
  Disallowed 吊销的证书的 X.509 证书存储区。
  My 个人证书的 X.509 证书存储区。
  Root 受信任的根证书颁发机构 (CA) 的 X.509 证书存储区。
  TrustedPeople 直接受信任的人和资源的 X.509 证书存储区。
  TrustedPublisher 直接受信任的发行者的 X.509 证书存储区。
参考: https://msdn.microsoft.com/zh-cn/library/system.security.cryptography.x509certificates.storename.aspx

StoreLocation 枚举

指定 X.509 证书存储区的位置。

  成员名称 说明
  CurrentUser 当前用户使用的 X.509 证书存储区。
  LocalMachine 分配给本地计算机的 X.509 证书存储区。
参考: https://msdn.microsoft.com/zh-cn/library/system.security.cryptography.x509certificates.storelocation.aspx

OpenFlags 枚举

指定打开 X.509 证书存储区的方式。

  成员名称 说明
  IncludeArchived 打开 X.509 证书存储区并添加存档证书。
  MaxAllowed 以允许最高级访问的方式打开 X.509 证书存储区。
  OpenExistingOnly 仅打开现有存储区。如果不存在任何存储区,Open 方法不会创建新的存储区。
  ReadOnly 以只读方式打开 X.509 证书存储区。
  ReadWrite 以读写方式打开 X.509 证书存储区。

参考:https://msdn.microsoft.com/zh-cn/library/system.security.cryptography.x509certificates.openflags.aspx?TPSecNotice




下面代码来自贴子:http://bbs.csdn.net/topics/390345557

public static bool InstallCertificate(string certFilePath, string password, StoreLocation location, StoreName storeName)
        {
            try
            {
<span style="white-space:pre">		</span>//判断证书是否存在
                if (!File.Exists(certFilePath))
                {
                    SubDebug(string.Format("Certificate file not exist.\t{0}", certFilePath));
                    return false;
                }
                byte[] certData = File.ReadAllBytes(certFilePath);
               X509Certificate2 cert = new X509Certificate2(certData, password,X509KeyStorageFlags.Exportable|X509KeyStorageFlags.MachineKeySet|X509KeyStorageFlags.PersistKeySet);
                X509Store store = new X509Store(storeName, location);
                store.Open(OpenFlags.MaxAllowed);
                store.Remove(cert);
                store.Add(cert);
                store.Close();
                SubDebug(string.Format("Certificate installed.\tCertificate file:{0}\tStore:{1}", certFilePath, storeName));
                return true;
            }
            catch (Exception ex)
            {
                SubDebug(string.Format("Install certificate fail.\tCertificate file:{0}\t{1}\tStore:{1}", certFilePath, ex.Message));
                return false;
            }
        }

代码调用也是相当简单

MyClass.InstallCertificate("XXX.cer","你的证书密码", StoreLocation.CurrentUser,  StoreName.Root);

这样只要执行代码就能完成导入证书到IE了。

但目前还有一个问题就是在导入证书时由于使用了StoreName.Root即“受信任的根证书颁发机构 (CA) 的 X.509 证书存储区。”导入时要有安全提示。

这点还是没有办法处理,或请高手告知处理手段了。


  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值