3

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

4

3 回答 3

2

因此,这是一个如何执行此操作的命令行示例。你需要来自微软的 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;
            }
        }
    }
}
于 2009-09-11T10:43:34.120 回答
1

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

X509Certificate2 certificate = new X509Certificate2(...);
certificate.FriendlyName = "MyName";
File.WriteAllBytes(path, certificate.Export(X509ContentType.Pkcs12));
于 2010-04-25T17:04:23.200 回答
0

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

你好,

请查看此以检查它是否适合您的需要:

当您在 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,您将收到错误消息。

于 2010-02-21T16:07:22.660 回答