Skip to content

Commit 20a51c9

Browse files
authored
Document ordering guarantee for X509Chain.ChainElements collection (#11475)
1 parent 1bab578 commit 20a51c9

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System;
2+
using System.Security.Cryptography.X509Certificates;
3+
4+
public class ChainElementsOrdering
5+
{
6+
public static void DemonstrateChainElementsOrdering(X509Certificate2 certificate)
7+
{
8+
//<SNIPPET6>
9+
using var chain = new X509Chain();
10+
chain.Build(certificate);
11+
12+
// chain.ChainElements[0] is the leaf (end-entity) certificate
13+
// chain.ChainElements[^1] is the root (trust anchor) certificate
14+
15+
Console.WriteLine("Certificate chain from leaf to root:");
16+
for (int i = 0; i < chain.ChainElements.Count; i++)
17+
{
18+
var cert = chain.ChainElements[i].Certificate;
19+
var role = i == 0 ? "Leaf" :
20+
i == chain.ChainElements.Count - 1 ? "Root" : "Intermediate";
21+
Console.WriteLine($"[{i}] {role}: {cert.Subject}");
22+
}
23+
//</SNIPPET6>
24+
}
25+
}

xml/System.Security.Cryptography.X509Certificates/X509Chain.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,9 +464,13 @@
464464
465465
A chain element consists of an <xref:System.Security.Cryptography.X509Certificates.X509Certificate2> object, an <xref:System.Security.Cryptography.X509Certificates.X509ChainStatus> structure, and an extra information string.
466466
467-
467+
The `ChainElements` collection is ordered from the end-entity (leaf) certificate at index 0, through any intermediate certificates, to the trust anchor (root certificate) at the final index. This ordering is consistent across all platforms.
468468
469469
## Examples
470+
The following code example demonstrates the ordering of chain elements:
471+
472+
:::code language="csharp" source="~/snippets/csharp/System.Security.Cryptography.X509Certificates/X509Chain/Overview/chainelements-ordering.cs" id="Snippet6":::
473+
470474
The following code example opens the current user's personal certificate store, allows you to select a certificate, then writes certificate and certificate chain information to the console. The output depends on the certificate you select.
471475
472476
:::code language="csharp" source="~/snippets/csharp/System.Security.Cryptography.X509Certificates/X509Chain/Overview/x509chaintest.cs" id="Snippet4":::

0 commit comments

Comments
 (0)