当前位置:首页 > Windows程序 > 正文

C#导入PFX和Cer证书的工具类

2024-03-31 Windows程序

public class CertificationHelper { public static bool importPFX(string certPath, string certPass) { // Create a collection object and populate it using the PFX file X509Certificate2Collection collection = new X509Certificate2Collection(); try { collection.Import(certPath, certPass, X509KeyStorageFlags.PersistKeySet); foreach (X509Certificate2 cert in collection) { X509Store store = new X509Store(); store.Open(OpenFlags.MaxAllowed); store.Add(cert); store.Close(); // Import the certificate into an X509Store object } } catch (Exception e) { //throw(e); return false; } return true; } public static bool importCER(string cerFileName) { try { X509Certificate2 certificate = new X509Certificate2(cerFileName); X509Store store = new X509Store(StoreName.TrustedPublisher, StoreLocation.LocalMachine); store.Open(OpenFlags.ReadWrite); store.Add(certificate); store.Close(); } catch { return false; } return true; } }

  具体使用的定名空间已经忘了,,可以本身去找找。

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/33888.html

Jm-杰米博客Jamie
草根站长的技术交流乐园!IT不会不要紧快来好好学习吧!
  • 20786文章总数
  • 7494587访问次数
  • 建站天数
  • 友情链接