C# 如何从.NET中的X509证书中提取电子邮件?
我正在寻找从X509证书获取电子邮件(字符串)的方法。 我找不到此的就绪属性或方法。因此,对我来说最好(对未来任务最灵活)的方法是通过ASN OID(1.2.840.113549.1.9.1)获取值。如何使用本机.NET类实现这一点C# 如何从.NET中的X509证书中提取电子邮件?,c#,.net,x509certificate,asn.1,C#,.net,X509certificate,Asn.1,我正在寻找从X509证书获取电子邮件(字符串)的方法。 我找不到此的就绪属性或方法。因此,对我来说最好(对未来任务最灵活)的方法是通过ASN OID(1.2.840.113549.1.9.1)获取值。如何使用本机.NET类实现这一点 我尝试使用AsnEncodedData.format,但没有任何效果。有办法做到这一点吗 如果可以使用第三方工具,那么您可以查看我的模块。此模块包含一个PKI.Core.dll库,它是一组API。API在 对于thid库,我将使用以下静态方法和自定义类: using
我尝试使用
AsnEncodedData.format
,但没有任何效果。有办法做到这一点吗 如果可以使用第三方工具,那么您可以查看我的模块。此模块包含一个PKI.Core.dll库,它是一组API。API在
对于thid库,我将使用以下静态方法和自定义类:
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;
}
}
}
使用PKI.ASN;
使用制度;
使用System.Collections.Generic;
使用System.Security.Cryptography;
使用System.Security.Cryptography.X509证书;
使用系统文本;
名称空间MyNamespace{
公共类属性{
公共Oid Oid{get;set;}
公共字符串值{get;set;}
}
公共类MyClass{
公共静态列表GetRndAttributes(X500 DifferentizedName名称){
List retValue=新列表();
ASN1 asn=新ASN1(名称.RawData);
asn.MoveNext();
做{
ASN1 asn2=新ASN1(asn有效载荷);
asn2.MoveNext();
列表oidRawData=新列表(asn2.标题);
oidRawData.AddRange(asn2.Payload);
Oid Oid=ASN1.DecodeObjectIdentifier(oidRawData.ToArray());
asn2.MoveNext();
字符串值;
开关(asn2.Tag){
大小写(字节)ASN1Tags.UniversalString:
value=Encoding.UTF32.GetString(asn2.Payload);
打破
大小写(字节)ASN1Tags.BMPString:
value=Encoding.bigendianucode.GetString(asn2.Payload);
打破
违约:
value=Encoding.UTF8.GetString(asn2.Payload);
打破
}
添加(新属性{OID=OID,Value=Value});
}while(asn.MoveNextCurrentLevel());
返回值;
}
}
}
该方法返回RDN属性的数组(无序),其中
OID
property包含RDN对象标识符,Value
property包含RDN文本值。如果可以使用Linq,则可以快速搜索集合:somearray.Where(x=>x.OID.Value==“1.2.840.113549.1.9.1”)代码>。请注意,特定的RDN属性可能会出现多次,因此不应使用First*
或Single*
Linq方法。上的示例可能会有所帮助?谢谢!就这样!但是你对另一个解决方案有什么建议(通过相应的OID获得任何扩展,如电子邮件)?对不起,我没有这方面的经验,只是对谷歌很好,你没有其他的评论/答案:)这是不可能通过内置工具实现的。您必须寻找第三方库。但是谢谢你的想法,真的值得考虑。谢谢,CryptoGuy!这将节省我很多时间!对于最后一个问题,您更喜欢哪种第三方库?有没有没有其他没有很好文档记录的BouncyCastle替代方案?