---
title: "Get X.509 subject alternative names in C#"
url: https://recursiveai.org/onecrypto/cookbook/read-x509-subject-alternative-names/
description: "Read the DNS names in an X.509 certificate’s Subject Alternative Name extension in C#. Handle certificates without a SAN and distinguish inspection from validation."
version: "0.4.0-preview.2"
---

# Get X.509 subject alternative 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/read-x509-subject-alternative-names/read-x509-subject-alternative-names.zip)

Read `SubjectAlternativeName` to get the identities listed in a certificate. The zip’s `with-san.der` has two DNS names and one IP address.

```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 path = args.FirstOrDefault()
    ?? "with-san.der";
var der = File.ReadAllBytes(path);
var cert = Certificate.Parse(der.AsMemory());
var san = cert.SubjectAlternativeName;
if (san is null)
{
    Console.WriteLine("No SAN extension.");
    return;
}
foreach (var name in san.Names)
    if (name.Kind == GeneralNameKind.DnsName)
        Console.WriteLine(name.Value);
```

Unzip [the example](https://recursiveai.org/onecrypto/cookbook/read-x509-subject-alternative-names/read-x509-subject-alternative-names.zip), then `dotnet run`.

```text
api.example.test
www.example.test
```

A certificate without the extension has a `null` `SubjectAlternativeName`; if the extension is present but malformed, the property throws `X509ParseException`.

## Variations

**Inspect a certificate with no SAN.** The zip’s `no-san.der` has the subject `CN=fixture.example` but no SAN extension. Pass it as the input instead of the default file:

```shell
dotnet run -- no-san.der
```

```text
No SAN extension.
```

**Read a PEM certificate.** With a PEM certificate named `sample.pem` (for example, from the [DER-to-PEM recipe](https://recursiveai.org/onecrypto/cookbook/convert-der-certificate-to-pem/)), replace the input-reading lines through `var cert` with:

```csharp
var cert = Certificate.FromPem(File.ReadAllText("sample.pem"));
```

## Related recipes

- [Convert a DER certificate to PEM in C#](https://recursiveai.org/onecrypto/cookbook/convert-der-certificate-to-pem/)
- [Validate an X.509 certificate chain in C#](https://recursiveai.org/onecrypto/cookbook/validate-x509-certificate-chain/)

---

## The short answer

Parse the local X.509 certificate with `Certificate.Parse`. Read `SubjectAlternativeName`, then print the `Value` of each entry whose `Kind` is `GeneralNameKind.DnsName`. Check for `null` before enumerating the names. This reads what the certificate says; it doesn’t decide whether to trust it.

## What is the subject alternative name in a certificate?

The SAN extension carries identities associated with the certificate’s public key. It isn’t just a list of hostnames: [RFC 5280 §4.2.1.6](https://www.rfc-editor.org/rfc/rfc5280#section-4.2.1.6) also defines IP addresses, email addresses and URIs, among other forms. A DNS name and an IP address have different entry types. The snippet at the top filters by type, so it prints both DNS names but not `192.0.2.1`.

## How do I check the subject alternative names in a certificate?

For binary DER input, parse the bytes as the sample does. If you already have PEM text, use `Certificate.FromPem` to read its certificate block and inspect the same property. Check the extension itself, not the subject’s common name (`CN`): `no-san.der` has a common name but no SAN. If your app already uses `X509Certificate2`, use .NET 10’s built-in [`X509SubjectAlternativeNameExtension.EnumerateDnsNames()`](https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.x509certificates.x509subjectalternativenameextension.enumeratednsnames?view=net-10.0) to read the DNS names without adding a package.

A present SAN with only IP or URI entries is a different case. The loop would print no DNS names, but the property would not be `null`.

For an inspector that should keep running after a malformed extension, `TryGetExtension<SubjectAlternativeName>` distinguishes an absent extension (returns `true` with a `null` value) from a malformed one (returns `false`). See the [X.509 docs](https://recursiveai.org/onecrypto/docs/x509/) for the typed extension API.

## How do I tell whether a subject alternative name matches a hostname?

Listing names is not a hostname check. Use hostname validation that applies the rules for DNS names, wildcard names and IP literals rather than comparing the loop’s printed values. Hostname validation is also separate from chain validation: a certificate can list the right name and still come from an issuer you don’t trust. The [path validation docs](https://recursiveai.org/onecrypto/docs/validation/) describe the checks and policy settings for using a certificate in a TLS server role.
