我试图在证书申请/接受过程中设置证书友好名称。我知道这是微软商店的一个属性,而不是证书,而且我想知道可以用什么.net/c#技术来设置它。设置证书友好名称
Q
设置证书友好名称
2
A
回答
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));
相关问题
- 1. 如何设置证书友好名称
- 2. 证书友好名称
- 3. 阅读数字证书的友好名称
- 4. 与好友名称注射
- 5. Android蓝牙友好名称
- 6. 枚举友好名称
- 7. 如何为Google服务帐户设置友好名称?
- 8. Qt - 设置好友
- 9. 设置构建设置以显示用户友好的变量名称而不是帽名称
- 10. 设置SSL证书的通用名称以保护Plesk面板
- 11. 提取证书名称
- 12. 设置SSL(自签名证书)和Tomcat
- 13. 重写规则的友好URL名称
- 14. 友好'从'名称与asp.net Webmail帮手
- 15. Java中COM端口的友好名称?
- 16. Django的:list_filter使用友好的名称
- 17. 在CheckedListBox中显示友好名称
- 18. Google使用OpenID的友好名称?
- 19. 什么USB设备(友好名称)当前连接到PC?
- 20. 名称和友好名称有什么区别?
- 21. 如何在web.config的MailSetting部分设置友好的电子邮件名称?
- 22. 如何检索友好的ICC配置文件名称?
- 23. 使用列名称或对称密钥获取证书名称
- 24. 通过PowerShell中的“证书模板名称”识别证书
- 25. 匿名友好CMS
- 26. 为KeyStore.TrustedCertificateEntry设置证书?
- 27. Debian,mysql yaSSL,设置证书
- 28. X509证书目的设置
- 29. lua nginx ssl证书设置
- 30. SslStream的SSL证书设置
为什么“您必须将证书导出为PFX/PKCS#12”?任何见解?如果它是.CER会怎么样? – VoodooChild 2013-04-23 04:08:57