Skip to content

Commit 9ea639b

Browse files
Copilotgewarren
andcommitted
Address code review feedback: move example to separate file and fix punctuation
Co-authored-by: gewarren <[email protected]>
1 parent c5039b8 commit 9ea639b

File tree

2 files changed

+29
-19
lines changed

2 files changed

+29
-19
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+
class ChainElementsOrdering
5+
{
6+
static void DemonstrateChainElementsOrdering()
7+
{
8+
//<SNIPPET6>
9+
using var chain = new X509Chain();
10+
chain.Build(serverCertificate);
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: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -467,27 +467,12 @@
467467
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
- On Windows, this matches the `CERT_CHAIN_CONTEXT` structure where `rgpChain[0]` is the end certificate and `rgpChain[cChain–1]` is the final chain element (root).
470-
- On Linux and macOS, this matches OpenSSL's `X509_STORE_CTX_get0_chain()` which returns a `STACK_OF(X509)` ordered from leaf to root.
470+
- On Linux and macOS, this matches OpenSSL's `X509_STORE_CTX_get0_chain()`, which returns a `STACK_OF(X509)` ordered from leaf to root.
471471
472472
## Examples
473-
The following code example demonstrates the ordering of chain elements:
474-
475-
```csharp
476-
using var chain = new X509Chain();
477-
chain.Build(serverCertificate);
478-
479-
// chain.ChainElements[0] is the leaf (end-entity) certificate
480-
// chain.ChainElements[^1] is the root (trust anchor) certificate
481-
482-
Console.WriteLine("Certificate chain from leaf to root:");
483-
for (int i = 0; i < chain.ChainElements.Count; i++)
484-
{
485-
var cert = chain.ChainElements[i].Certificate;
486-
var role = i == 0 ? "Leaf" :
487-
i == chain.ChainElements.Count - 1 ? "Root" : "Intermediate";
488-
Console.WriteLine($"[{i}] {role}: {cert.Subject}");
489-
}
490-
```
473+
The following code example demonstrates the ordering of chain elements:
474+
475+
:::code language="csharp" source="~/snippets/csharp/System.Security.Cryptography.X509Certificates/X509Chain/Overview/chainelements-ordering.cs" id="Snippet6":::
491476
492477
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.
493478

0 commit comments

Comments
 (0)