【问题标题】:Setting Certificate Friendly Name设置证书友好名称
【发布时间】:2010-11-27 02:12:57
【问题描述】:

我试图在证书请求/接受过程中设置证书友好名称。我知道这是 microsoft store 的属性而不是证书,并且想知道可以使用什么 .net/c# 技术来设置它。

【问题讨论】:

    标签: c# certificate pki


    【解决方案1】:

    所以这里有一个命令行示例来说明如何执行此操作。您需要来自 Microsoft 的 CAPICOM,它封装了 CryptoAPI。

    友好名称是证书存储而不是证书的属性,因此此代码将证书导入证书存储并设置友好名称。

    代码采用两个参数,即证书文件的路径和您希望设置的友好名称。

    代码:-

    using System;
    
    using System.Collections.Generic;
    
    using System.Text;
    
    using CAPICOM;
    
    using System.Collections;
    
    using System.Runtime.InteropServices;
    
    
    namespace CertTool
    
    {
    
        class Program
        {
            const uint CERT_SYSTEM_STORE_LOCAL_MACHINE = 0x20000;
            const int CAPICOM_PROPID_FRIENDLY_NAME = 11;
            const int CAPICOM_ENCODE_BINARY = 1;
    
            static private String _currStoreName = "My";
            static private String _FriendlyName = "Not Set";
            static private String _CertPath = "C:\\test.cer";
            static StoreClass _oCurrStore;
            static ExtendedPropertyClass _friendlyProp;
            static CertificateClass _certificate;
            static ExtendedProperties _extendedProp;
    
            static void Main(string[] args)
            {
                try
                {
                    //Friendly name Argument
                    if (args.Length > 0)
                    {
                        _FriendlyName = args[0];
                    }
                    //Certpath argument
                    if (args.Length > 1) 
                    {
                        _CertPath = args[1];
                    }
                    //Set and open the Store
                    _oCurrStore = new StoreClass();
                    _oCurrStore.Open(
                        CAPICOM_STORE_LOCATION.CAPICOM_LOCAL_MACHINE_STORE,
                        _currStoreName,
                        CAPICOM_STORE_OPEN_MODE.CAPICOM_STORE_OPEN_EXISTING_ONLY |
                        CAPICOM_STORE_OPEN_MODE.CAPICOM_STORE_OPEN_MAXIMUM_ALLOWED);
                    //Call the import certificate function
                    importCert();
                }
                catch(Exception ex){
                    Console.WriteLine(ex.Message);
                    Console.WriteLine(args[0]);
                }
            }
            //Function import the certificate to the machine store and sets the friendly name
            static bool importCert()
            {
                try
                {
                    //Create Certificate Object
                    _certificate = new CertificateClass();
                    //Load the certificate into the obejct from file
                    _certificate.Load(_CertPath, "", CAPICOM_KEY_STORAGE_FLAG.CAPICOM_KEY_STORAGE_EXPORTABLE, CAPICOM_KEY_LOCATION.CAPICOM_LOCAL_MACHINE_KEY);
                    //Create extended property Class for friendly name
                    _friendlyProp = new ExtendedPropertyClass();
                    _friendlyProp.PropID =  CAPICOM_PROPID.CAPICOM_PROPID_FRIENDLY_NAME;
                    _friendlyProp.set_Value(CAPICOM_ENCODING_TYPE.CAPICOM_ENCODE_BINARY, _FriendlyName);
    
                    //Add extendedProp on cert object
                    _extendedProp = _certificate.ExtendedProperties();
                    //Set extendded prop to friendly name object
                    _extendedProp.Add(_friendlyProp);
                    _oCurrStore.Add(_certificate);
                    return true;
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    Console.WriteLine(_CertPath);
                    return true;
                }
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      使用 X509Certificate2.FriendlyName。但是,您必须将证书导出为 PFX/PKCS#12:

      X509Certificate2 certificate = new X509Certificate2(...);
      certificate.FriendlyName = "MyName";
      File.WriteAllBytes(path, certificate.Export(X509ContentType.Pkcs12));
      

      【讨论】:

      • 为什么“必须将证书导出为 PFX/PKCS#12”?有什么见解吗?如果是 .CER 怎么办?
      【解决方案3】:

      好的,在这里找到了答案:

      嗨,

      请看一下它是否适合您的需要:

      当您在 X64 环境中运行 .net 代码时,您将收到以下错误消息。

      " 失败 -- 检索具有 CLSID 的组件的 COM 类工厂 ...."

      例如在 CMS 导出/导入服务器端 .net 代码 = "ExportSiteContentIncremental(...) 失败 -- 检索具有 CLSID {CA0752B3-021C-4F99-82E3-2C0F19C5E953} 的组件的 COM 类工厂因以下错误而失败:80040154。”

      解决方法:

      可能的解决方法是将项目的平台从“任何 CPU”修改为“X86”(在项目的属性、构建/平台的目标中)

      根本原因

      VSS 互操作是一个使用 32 位框架的托管程序集,并且 dll 包含一个 32 位 COM 对象。如果你在 64 位环境下运行这个 COM dll,你会得到错误信息。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-09-12
        • 1970-01-01
        • 2015-06-13
        • 1970-01-01
        • 2018-04-08
        • 2013-05-10
        • 2021-10-18
        • 1970-01-01
        相关资源
        最近更新 更多