diff --git a/snippets/csharp/System.Security.Cryptography.X509Certificates/X509Chain/Overview/chainelements-ordering.cs b/snippets/csharp/System.Security.Cryptography.X509Certificates/X509Chain/Overview/chainelements-ordering.cs new file mode 100644 index 00000000000..8e0071a11e4 --- /dev/null +++ b/snippets/csharp/System.Security.Cryptography.X509Certificates/X509Chain/Overview/chainelements-ordering.cs @@ -0,0 +1,25 @@ +using System; +using System.Security.Cryptography.X509Certificates; + +public class ChainElementsOrdering +{ + public static void DemonstrateChainElementsOrdering(X509Certificate2 certificate) + { +// + using var chain = new X509Chain(); + chain.Build(certificate); + + // chain.ChainElements[0] is the leaf (end-entity) certificate + // chain.ChainElements[^1] is the root (trust anchor) certificate + + Console.WriteLine("Certificate chain from leaf to root:"); + for (int i = 0; i < chain.ChainElements.Count; i++) + { + var cert = chain.ChainElements[i].Certificate; + var role = i == 0 ? "Leaf" : + i == chain.ChainElements.Count - 1 ? "Root" : "Intermediate"; + Console.WriteLine($"[{i}] {role}: {cert.Subject}"); + } +// + } +} \ No newline at end of file diff --git a/xml/System.Security.Cryptography.X509Certificates/X509Chain.xml b/xml/System.Security.Cryptography.X509Certificates/X509Chain.xml index 7beb89f252b..6a66282b914 100644 --- a/xml/System.Security.Cryptography.X509Certificates/X509Chain.xml +++ b/xml/System.Security.Cryptography.X509Certificates/X509Chain.xml @@ -464,9 +464,13 @@ A chain element consists of an object, an structure, and an extra information string. - + 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. ## Examples +The following code example demonstrates the ordering of chain elements: + +:::code language="csharp" source="~/snippets/csharp/System.Security.Cryptography.X509Certificates/X509Chain/Overview/chainelements-ordering.cs" id="Snippet6"::: + 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. :::code language="csharp" source="~/snippets/csharp/System.Security.Cryptography.X509Certificates/X509Chain/Overview/x509chaintest.cs" id="Snippet4":::