简体   繁体   English

如何获得数字证书X509Certificate2的名称

[英]How to get the name of a Digital Certificate X509Certificate2

I'm working with digital certificates X509Certificate2 in C# and I'm having problems to find the certificate's name without it having a friendly name. 我正在使用C#中的数字证书X509Certificate2,但是在没有友好名称的情况下无法找到证书名称。

My software builds a combobox with all the certificates present in the client computer. 我的软件使用客户端计算机中存在的所有证书构建一个组合框。

In my cenario, one of my clients cannot configure a friendly name manually for the certificate, so I manage to get the name from the SubjectName (breaking the string and get the one that starts with "CN="). 在我的方案中,我的一个客户端无法为证书手动配置一个友好名称,因此我设法从SubjectName获得名称(将字符串断开并获得以“ CN =“开头的名称)。

But with some certificates, this method does not work well and I wish to know if there is a more elegant way to get the friendly name if it exists, if not, then get the real certificate's name. 但是对于某些证书,此方法不能很好地工作,我想知道是否存在一种更友好的方式来获取友好名称(如果存在),如果不存在,则获取真实证书的名称。

Also I wish to get a list of valid certificates from the store, excluding the root ones that is not used for digital signatures. 我也希望从商店获得有效证书的列表,但不包括未用于数字签名的根证书。

In order to load only the certificates for perform digital signature (avoiding CA's and certificates with only public part) you can use: 为了仅加载用于执行数字签名的证书(避免仅使用公共部分的CA和证书),您可以使用:

X509Store store = new X509Store("My");

Specifying "My" you load only: The X.509 certificate store for personal certificates. 如果指定“我的”,则仅加载:个人证书的X.509证书存储。

You can see all options here , also it's possible to use StoreLocation.CurrentUser , however I think "My" is what you are looking for. 您可以在这里看到所有选项,也可以使用StoreLocation.CurrentUser ,但是我认为您正在寻找“我的”。

About the certificate name you can use X509Certificate2 properties: 关于证书名称,可以使用X509Certificate2属性:

if FriendlyName property is null or empty you can use SubjectName property. 如果FriendlyName属性为null或为空,则可以使用SubjectName属性。 In the case which both properties doesn't work, you has a last chance with SerialNumber property, SerialNumber is not user friendly to identify certificate, however for the certificate issuer SerialNumber must be unique so you can identify the certificate with this property. 在两个属性都不起作用的情况下,您最后一次获得SerialNumber属性的机会是,SerialNumber并不便于用户识别证书,但是对于证书颁发者,SerialNumber必须唯一,以便您可以使用此属性来识别证书。

Hope this helps, 希望这可以帮助,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM