X509Chain.ChainElements Propriété

Définition

Obtient une collection d’objets X509ChainElement .

public:
 property System::Security::Cryptography::X509Certificates::X509ChainElementCollection ^ ChainElements { System::Security::Cryptography::X509Certificates::X509ChainElementCollection ^ get(); };
public System.Security.Cryptography.X509Certificates.X509ChainElementCollection ChainElements { get; }
member this.ChainElements : System.Security.Cryptography.X509Certificates.X509ChainElementCollection
Public ReadOnly Property ChainElements As X509ChainElementCollection

Valeur de propriété

Objet X509ChainElementCollection.

Exemples

L’exemple de code suivant illustre l’ordre des éléments de chaîne :

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}");
}

L’exemple de code suivant ouvre le magasin de certificats personnel de l’utilisateur actuel, vous permet de sélectionner un certificat, puis d’écrire des informations de certificat et de chaîne de certificats dans la console. La sortie dépend du certificat que vous sélectionnez.

    //Output chain element information.
    Console.WriteLine ("Chain Element Information");
    Console.WriteLine ("Number of chain elements: {0}", ch.ChainElements.Count);
    Console.WriteLine ("Chain elements synchronized? {0} {1}", ch.ChainElements.IsSynchronized, Environment.NewLine);

    foreach (X509ChainElement element in ch.ChainElements)
    {
        Console.WriteLine ("Element issuer name: {0}", element.Certificate.Issuer);
        Console.WriteLine ("Element certificate valid until: {0}", element.Certificate.NotAfter);
        Console.WriteLine ("Element certificate is valid: {0}", element.Certificate.Verify ());
        Console.WriteLine ("Element error status length: {0}", element.ChainElementStatus.Length);
        Console.WriteLine ("Element information: {0}", element.Information);
        Console.WriteLine ("Number of element extensions: {0}{1}", element.Certificate.Extensions.Count, Environment.NewLine);

        if (ch.ChainStatus.Length > 1)
        {
            for (int index = 0; index < element.ChainElementStatus.Length; index++)
            {
                Console.WriteLine (element.ChainElementStatus[index].Status);
                Console.WriteLine (element.ChainElementStatus[index].StatusInformation);
            }
        }
    }
    store.Close();
'Output chain element information.
Console.WriteLine("Chain Element Information")
Console.WriteLine("Number of chain elements: {0}", ch.ChainElements.Count)
Console.WriteLine("Chain elements synchronized? {0} {1}", ch.ChainElements.IsSynchronized, Environment.NewLine)

Dim element As X509ChainElement
For Each element In ch.ChainElements
    Console.WriteLine("Element issuer name: {0}", element.Certificate.Issuer)
    Console.WriteLine("Element certificate valid until: {0}", element.Certificate.NotAfter)
    Console.WriteLine("Element certificate is valid: {0}", element.Certificate.Verify())
    Console.WriteLine("Element error status length: {0}", element.ChainElementStatus.Length)
    Console.WriteLine("Element information: {0}", element.Information)
    Console.WriteLine("Number of element extensions: {0}{1}", element.Certificate.Extensions.Count, Environment.NewLine)

    If ch.ChainStatus.Length > 1 Then
        Dim index As Integer
        For index = 0 To element.ChainElementStatus.Length
            Console.WriteLine(element.ChainElementStatus(index).Status)
            Console.WriteLine(element.ChainElementStatus(index).StatusInformation)
        Next index
    End If
Next element
store.Close()

Remarques

Chaque X509ChainElement objet est une représentation d’un élément dans la chaîne qui a été généré pendant un appel à la Build méthode.

Un élément de chaîne se compose d’un X509Certificate2 objet, d’une X509ChainStatus structure et d’une chaîne d’informations supplémentaire.

La ChainElements collection est triée du certificat d’entité de fin (feuille) à l’index 0, via tous les certificats intermédiaires, vers l’ancre d’approbation (certificat racine) à l’index final. Cet ordre est cohérent sur toutes les plateformes.

S’applique à