我试图在证书申请/接受过程中设置证书友好名称。我知道这是微软商店的一个属性而不是证书,并且想知道可能会使用什么.net / c#技术来设置它。

有帮助吗?

解决方案 2

所以这是一个如何执行此操作的命令行示例。你需要来自microsoft的CAPICOM来包装CryptoAPI。

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

代码将两个参数作为cert文件的路径和您希望设置的友好名称。

代码: -

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

其他提示

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

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

好的,在这里找到答案:

您好,

请查看此内容以确定它是否符合您的需求:

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

"失败 - 使用CLSID ....“

检索组件的COM类工厂

E.g。在CMS导出/导入服务器端.net code = " ExportSiteContentIncremental(...)失败 - 由于以下原因导致CLSID {CA0752B3-021C-4F99-82E3-2C0F19C5E953}的组件的COM类工厂失败错误:80040154。“

替代方法:

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

根本原因

VSS Interop是使用32位Framework的托管程序集,dll包含32位COM对象。如果在64位环境中运行此COM dll,则会收到错误消息。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top