---
title: "Compare X.509 distinguished names in C#"
url: https://recursiveai.org/onecrypto/cookbook/compare-x509-distinguished-names/
description: "Compare two X.509 distinguished names in C# with OneCrypto’s Matches method. See how it handles case, spaces, attribute order, and encoded equality."
version: "0.4.0-preview.2"
---

# Compare X.509 distinguished names in C#

**OneCrypto** is a free, open-source cryptography library for .NET from Recursive AI. MIT or Apache-2.0, preview, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneCrypto) · [GitHub](https://github.com/recursive-ai-oss/onecrypto-csharp) · [Docs](https://recursiveai.org/onecrypto/docs/) · [Download example (.zip)](https://recursiveai.org/onecrypto/cookbook/compare-x509-distinguished-names/compare-x509-distinguished-names.zip)

A distinguished name such as `CN=Example CA,O=Workshop` identifies a certificate’s subject or issuer. Use `Matches` to compare parsed names.

```shell
dotnet add package RecursiveAI.OneCrypto --prerelease
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
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](https://recursiveai.org/onecrypto/cookbook/compare-x509-distinguished-names/compare-x509-distinguished-names.zip), then `dotnet run`.

```text
True
```

## The 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.

```csharp
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:

```text
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.

```csharp
var encoded = DistinguishedName.FromDer(first.Encoded);
Console.WriteLine(encoded.Matches(second));
```

```text
True
```

**Compare 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.

```csharp
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));
```

```text
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

- [Read X.509 subject alternative names](https://recursiveai.org/onecrypto/cookbook/read-x509-subject-alternative-names/)
- [Validate an X.509 certificate chain](https://recursiveai.org/onecrypto/cookbook/validate-x509-certificate-chain/)

## 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](https://www.rfc-editor.org/rfc/rfc5280.html#section-4.1.2.4) 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](https://www.rfc-editor.org/rfc/rfc4514.html) 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](https://recursiveai.org/onecrypto/docs/x509/#names) 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.
