码迷,mamicode.com
首页 > Windows程序 > 详细

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

时间:2018-04-01 20:52:31      阅读:228      评论:0      收藏:0      [点我收藏+]

标签:oca   dwr   arp   lag   ring   空间   csharp   工具   rust   

代码:

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

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

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

标签:oca   dwr   arp   lag   ring   空间   csharp   工具   rust   

原文地址:https://www.cnblogs.com/DM428/p/8687976.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!