2009-09-10 96 views
2

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

回答

1

所以这里是一个命令行示例如何做到这一点。您需要包装CryptoAPI的Microsoft的CAPICOM。

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

该代码需要两个参数指向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; 
      } 
     } 
    } 
} 
0

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

嗨,

请看看这个检查它是否适合你的需要:

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

“失败--Retrieving COM类工厂具有CLSID组件....”

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

解决方法:

可能的解决办法是修改你的项目的平台, '任何CPU' 到 'X86'(以项目的属性,构建/平台的目标)

根本原因

的VSS互操作是一个使用32位框架的托管程序集,并且该DLL包含一个32位COM对象。如果您在64位环境中运行此COM DLL,您将收到错误消息。

1

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

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

为什么“您必须将证书导出为PFX/PKCS#12”?任何见解?如果它是.CER会怎么样? – VoodooChild 2013-04-23 04:08:57