A distinguished name such as CN=Example CA,O=Workshop identifies a certificate’s subject or issuer. Use Matches to compare parsed names.
dotnet add package RecursiveAI.OneCrypto --prereleaseRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using RecursiveAI.OneCrypto; var first = DistinguishedName.FromString( "CN=Example CA,O=Workshop"); var second = DistinguishedName.FromString( "CN=example ca,O=Workshop"); Console.WriteLine(first.Matches(second));
Unzip the example, then dotnet run.
TrueThe full program
The two name strings differ in the capitalization of Example CA and in the number of spaces. The program also compares their encoded bytes and tries swapping the two comma-separated parts.
using RecursiveAI.OneCrypto; var first = DistinguishedName.FromString( "CN=Example CA,O=Workshop"); var second = DistinguishedName.FromString( "CN=example ca,O=Workshop"); Console.WriteLine(first.Matches(second)); Console.WriteLine($"Same encoding: {first.Equals(second)}"); var swapped = DistinguishedName.FromString( "O=Workshop,CN=example ca"); Console.WriteLine($"RDNs reordered: {first.Matches(swapped)}");
dotnet run prints:
True Same encoding: False RDNs reordered: False
For name strings that come from users, use TryFromString if you want to handle invalid input without an exception.
Variations
Read the name from its encoded bytes. Append this to the snippet at the top. This uses the first name’s bytes to show how FromDer and FromString can feed the same comparison.
var encoded = DistinguishedName.FromDer(first.Encoded); Console.WriteLine(encoded.Matches(second));
TrueCompare names on two certificates. Run this as a separate Program.cs. The download includes leaf.pem, issued to CN=Workshop Leaf,O=Workshop, and ca.pem, whose subject is CN=Workshop Test CA,O=Workshop and whose key signed the leaf. Only the public certificates are included.
using System.Security.Cryptography.X509Certificates; using RecursiveAI.OneCrypto; var leaf = Certificate.FromPem(File.ReadAllText("leaf.pem")); var ca = Certificate.FromPem(File.ReadAllText("ca.pem")); Console.WriteLine(leaf.Issuer.Matches(ca.Subject)); using var cert = X509Certificate2.CreateFromPem(File.ReadAllText("ca.pem")); var subject = DistinguishedName.FromDer(cert.SubjectName.RawData); Console.WriteLine(leaf.Issuer.Matches(subject));
True True
The second check reads the subject from an X509Certificate2 instead. Both checks compare names only; neither verifies the signature or trust.
Related recipes
The short answer
Parse text with DistinguishedName.FromString and pass the parsed names to Matches. When the input is the encoded name from a certificate rather than its display text, use DistinguishedName.FromDer. In OneCrypto, Equals checks the encoded bytes; Matches compares the names. Neither call validates a certificate.
What is an X.509 distinguished name?
X.509 certificates have issuer and subject fields. The issuer identifies the entity that issued the certificate, while the subject identifies the entity associated with its public key. A distinguished name (DN) is built from a sequence of relative distinguished names (RDNs). CN=Example CA,O=Workshop is the RFC 4514 text form of a name with two RDNs: one for the common name (CN) and one for the organization (O).
A name on a screen is a rendering, not a unique representation of the underlying name. Use the certificate’s parsed Subject or Issuer with OneCrypto when you already have a parsed Certificate. That avoids converting the name to text and parsing it again. If you compare a candidate issuer to a certificate’s issuer field, a name match is only a candidate match; other certificates can share that subject name. It doesn’t say whether the candidate signed the certificate or whether it’s trusted.
Does the order of attributes matter in a distinguished name?
Order matters across the comma-separated RDNs. The program’s third line is False because moving O=Workshop changes the sequence. Inside a single RDN, an unescaped + joins attributes. For example, CN=Example CA+OU=Ops,O=Workshop has two attributes in its first RDN. Matches doesn’t require those two attributes to appear in the same order. Changing the + to a comma instead puts them in different RDNs and changes the name’s structure.
Attribute types must still match, and values that aren’t strings are compared byte for byte. For string values the pinned implementation trims and collapses ASCII spaces and tabs and uppercases characters with invariant casing. The OneCrypto X.509 guide gives the important qualification: “It does not apply the full RFC 4518 mapping; names that differ only by that mapping are not matched.” Don’t assume Unicode spellings that look alike compare equal, or substitute this call for an application’s required name-matching policy.