Read SubjectAlternativeName to get the identities listed in a certificate. The zip’s with-san.der has two DNS names and one IP address.
dotnet add package RecursiveAI.OneCrypto --prereleaseRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
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, then dotnet run.
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:
dotnet run -- no-san.derNo SAN extension.Read a PEM certificate. With a PEM certificate named sample.pem (for example, from the DER-to-PEM recipe), replace the input-reading lines through var cert with:
var cert = Certificate.FromPem(File.ReadAllText("sample.pem"));Related recipes
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 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() 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 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 describe the checks and policy settings for using a certificate in a TLS server role.