Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract Common Name from Distinguished Name

Tags:

Is there a call in .NET that parses the CN from a rfc-2253 encoded distinguished name? I know there are some third-party libraries that do this, but I would prefer to use native .NET libraries if possible.

Examples of a string encoded DN

CN=L. Eagle,O=Sue\, Grabbit and Runn,C=GB

CN=Jeff Smith,OU=Sales,DC=Fabrikam,DC=COM

like image 725
FunLovinCoder Avatar asked Oct 07 '11 14:10

FunLovinCoder


People also ask

What is common name and distinguished name?

An entry is made up of a collection of attributes that have a unique identifier called a Distinguished Name (DN). A DN has a unique name that identifies the entry at the respective hierarchy. In the example above, John Doe and Jane Doe are different common names (cn) that identify different entries at that same level.

What is DN and RDN in LDAP?

The LDAP API references an LDAP object by its distinguished name (DN). A DN is a sequence of relative distinguished names (RDN) connected by commas. An RDN is an attribute with an associated value in the form attribute=value; normally expressed in a UTF-8 string format.

What is DN and relative DN?

Every entry in the directory has a distinguished name (DN). The DN is the name that uniquely identifies an entry in the directory. The first component of the DN is referred to as the Relative Distinguished Name (RDN).


1 Answers

If you are working with an X509Certificate2, there is a native method that you can use to extract the Simple Name. The Simple Name is equivalent to the Common Name RDN within the Subject field of the main certificate:

x5092Cert.GetNameInfo(X509NameType.SimpleName, false);

Alternatively, X509NameType.DnsName can be used to retrieve the Subject Alternative Name, if present; otherwise, it will default to the Common Name:

x5092Cert.GetNameInfo(X509NameType.DnsName, false);
like image 60
digital_jedi Avatar answered Sep 23 '22 21:09

digital_jedi