zoukankan      html  css  js  c++  java
  • C#导入PFX和Cer证书的工具类

    代码:

    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;
            }
        }
    

      具体使用的命名空间已经忘了,可以自己去找找。

  • 相关阅读:
    错误提示窗口-“操作系统当前的配置不能运行此应用程序”
    打印机无法打印的10种解决方法
    开发进度三
    人月神话阅读笔记二
    开发进度二
    开发进度1
    人月神话阅读笔记一
    库存物资管理系统
    四则运算
    动手动脑5
  • 原文地址:https://www.cnblogs.com/DM428/p/8687976.html
Copyright © 2011-2022 走看看