简体   繁体   English

如何从.NET中的X509证书中提取电子邮件?

[英]How to extract the Email from a X509 Certificate in .NET?

I'm looking for the way to get the Email (string) from X509 Certificate. 我正在寻找从X509证书获取电子邮件(字符串)的方法。 I can't find ready property or method for this. 我找不到准备好的财产或方法。 So the best for me (most flexible for future tasks) is to get the value by ASN OID (1.2.840.113549.1.9.1). 因此,对我来说最好(对于未来任务最灵活)是通过ASN OID(1.2.840.113549.1.9.1)获取值。 How can I do this using native .NET class? 如何使用本机.NET类执行此操作?

I tried to use AsnEncodedData.format but without any effect. 我试图使用AsnEncodedData.format但没有任何影响。 Is there a way to do this? 有没有办法做到这一点?

If it is ok to use 3rd party tools, then you may look at my Powershell PKI module. 如果可以使用第三方工具,那么您可以查看我的Powershell PKI模块。 This module contains a PKI.Core.dll library which is a set of API. 该模块包含一个PKI.Core.dll库,它是一组API。 APIs are fairly well documented in the Library documentation API文档中有相当详细的API 文档

With thid library I would go with the following static method and custom class: 使用这个库我会使用以下静态方法和自定义类:

using PKI.ASN;
using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Text;

namespace MyNamespace {
    public class RdnAttribute {
        public Oid OID { get; set; }
        public String Value { get; set; }
    }
    public class MyClass {
        public static List<RdnAttribute> GetRdnAttributes(X500DistinguishedName name) {
            List<RdnAttribute> retValue = new List<RdnAttribute>();
            ASN1 asn = new ASN1(name.RawData);
            asn.MoveNext();
            do {
                ASN1 asn2 = new ASN1(asn.Payload);
                asn2.MoveNext();
                List<Byte> oidRawData = new List<Byte>(asn2.Header);
                oidRawData.AddRange(asn2.Payload);
                Oid oid = ASN1.DecodeObjectIdentifier(oidRawData.ToArray());
                asn2.MoveNext();
                String value;
                switch (asn2.Tag) {
                    case (Byte)ASN1Tags.UniversalString:
                        value = Encoding.UTF32.GetString(asn2.Payload);
                        break;
                    case (Byte)ASN1Tags.BMPString:
                        value = Encoding.BigEndianUnicode.GetString(asn2.Payload);
                        break;
                    default:
                        value = Encoding.UTF8.GetString(asn2.Payload);
                        break;
                }
                retValue.Add(new RdnAttribute { OID = oid, Value = value });
            } while (asn.MoveNextCurrentLevel());
            return retValue;
        }
    }
}

The method returns an array (unordered) of RDN attributes, where OID property contains RDN object identifier and Value property contains RDN text value. 该方法返回RDN属性的数组(无序),其中OID属性包含RDN对象标识符, Value属性包含RDN文本值。 If you can use Linq, then you can quickly search through collection: somearray.Where(x => x.OID.Value == "1.2.840.113549.1.9.1"); 如果你可以使用Linq,那么你可以快速搜索集合: somearray.Where(x => x.OID.Value == "1.2.840.113549.1.9.1"); . Note that particular RDN attributes may appear multiple times, therefore you should not use First* or Single* Linq methods. 请注意,特定的RDN属性可能会出现多次,因此您不应使用First*Single* Linq方法。

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

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