简体   繁体   English

如何从X509Certificate2获取组织名称?

[英]How do I get the Organization name from an X509Certificate2?

I can get the full subject string from an X509Certificate2 via the Subject property, but is there any way to get a specific value from the subject string (eg just O = "Stack Exchange, Inc." )? 我可以通过Subject属性从X509Certificate2获取完整的主题字符串,但是有什么方法可以从主题字符串获取特定值(例如,仅O = "Stack Exchange, Inc." )? The SubjectName property looked promising, but it seems to only contain more ways of dumping the entire subject into a string. SubjectName属性看起来很有希望,但似乎只包含更多将整个主题转储为字符串的方式。

No you can't, the X509Certificate2 does not have any property besides Subject to get the information that you need. 不,您不能, X509Certificate2除了“ Subject以外没有任何属性来获取您所需的信息。

But if you really need to have the Organization name separeted, you could have it with a simple match: 但是,如果您确实需要分隔组织名称,则可以使用简单的匹配:

var organization = Regex.Match(certificate.Subject, @"O\s?=(.*)\s").Groups[1].ToString();

In the Portable.BouncyCastle library (available via NuGet ) there is an X509Name class that allows you to do something like this: Portable.BouncyCastle库(可通过NuGet获得 )中,有一个X509Name类,该类允许您执行以下操作:

using Org.BouncyCastle.Asn1.X509;
using System.Linq;
using System.Security.Cryptography.X509Certificates;

...

X509Certificate2 certificate = ...

var name = new X509Name(certificate.Subject);
var organization = name
  .GetValueList(X509Name.O)
  .OfType<string>()
  .FirstOrDefault();

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

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