X509Chain Klass

Definition

Representerar en kedjebyggnadsmotor för X509Certificate2 certifikat.

public ref class X509Chain
public ref class X509Chain : IDisposable
public class X509Chain
public class X509Chain : IDisposable
type X509Chain = class
type X509Chain = class
    interface IDisposable
Public Class X509Chain
Public Class X509Chain
Implements IDisposable
Arv
X509Chain
Implementeringar

Exempel

I följande kodexempel öppnas den aktuella användarens personliga certifikatarkiv, där du kan välja ett certifikat och sedan skriva information om certifikat- och certifikatkedjan till konsolen. Utdata beror på vilket certifikat du väljer.

using System;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.IO;

class TestX509Chain
{
    static void Main(string[] args)
    {
        //Create new X509 store from local certificate store.
        X509Store store = new X509Store("MY", StoreLocation.CurrentUser);
        store.Open(OpenFlags.OpenExistingOnly | OpenFlags.ReadWrite);

        //Output store information.
        Console.WriteLine ("Store Information");
        Console.WriteLine ("Number of certificates in the store: {0}", store.Certificates.Count);
        Console.WriteLine ("Store location: {0}", store.Location);
        Console.WriteLine ("Store name: {0} {1}", store.Name, Environment.NewLine);
    
        //Put certificates from the store into a collection so user can select one.
        X509Certificate2Collection fcollection = (X509Certificate2Collection)store.Certificates;
        X509Certificate2Collection collection = X509Certificate2UI.SelectFromCollection(fcollection, "Select an X509 Certificate", "Choose a certificate to examine.", X509SelectionFlag.SingleSelection);
        X509Certificate2 certificate = collection[0];
        X509Certificate2UI.DisplayCertificate(certificate);

        //Output chain information of the selected certificate.
        X509Chain ch = new X509Chain();
        ch.ChainPolicy.RevocationMode = X509RevocationMode.Online;
        ch.Build (certificate);
        Console.WriteLine ("Chain Information");
        Console.WriteLine ("Chain revocation flag: {0}", ch.ChainPolicy.RevocationFlag);
        Console.WriteLine ("Chain revocation mode: {0}", ch.ChainPolicy.RevocationMode);
        Console.WriteLine ("Chain verification flag: {0}", ch.ChainPolicy.VerificationFlags);
        Console.WriteLine ("Chain verification time: {0}", ch.ChainPolicy.VerificationTime);
        Console.WriteLine ("Chain status length: {0}", ch.ChainStatus.Length);
        Console.WriteLine ("Chain application policy count: {0}", ch.ChainPolicy.ApplicationPolicy.Count);
        Console.WriteLine ("Chain certificate policy count: {0} {1}", ch.ChainPolicy.CertificatePolicy.Count, Environment.NewLine);

        //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();
    }
}
Imports System.Security.Cryptography
Imports System.Security.Cryptography.X509Certificates
Imports System.IO

Class TestX509Chain

    Shared Sub Main(ByVal args() As String)
        'Create new X509 store from local certificate store.
        Dim store As New X509Store("MY", StoreLocation.CurrentUser)
        store.Open(OpenFlags.OpenExistingOnly Or OpenFlags.ReadWrite)

        'Output store information.
        Console.WriteLine("Store Information")
        Console.WriteLine("Number of certificates in the store: {0}", store.Certificates.Count)
        Console.WriteLine("Store location: {0}", store.Location)
        Console.WriteLine("Store name: {0} {1}", store.Name, Environment.NewLine)

        'Put certificates from the store into a collection so user can select one.
        Dim fcollection As X509Certificate2Collection = CType(store.Certificates, X509Certificate2Collection)
        Dim collection As X509Certificate2Collection = X509Certificate2UI.SelectFromCollection(fcollection, "Select an X509 Certificate", "Choose a certificate to examine.", X509SelectionFlag.SingleSelection)
        Dim certificate As X509Certificate2 = collection(0)
        X509Certificate2UI.DisplayCertificate(certificate)

        'Output chain information of the selected certificate.
        Dim ch As New X509Chain()
        ch.ChainPolicy.RevocationMode = X509RevocationMode.Online
        ch.Build(certificate)
        Console.WriteLine("Chain Information")
        Console.WriteLine("Chain revocation flag: {0}", ch.ChainPolicy.RevocationFlag)
        Console.WriteLine("Chain revocation mode: {0}", ch.ChainPolicy.RevocationMode)
        Console.WriteLine("Chain verification flag: {0}", ch.ChainPolicy.VerificationFlags)
        Console.WriteLine("Chain verification time: {0}", ch.ChainPolicy.VerificationTime)
        Console.WriteLine("Chain status length: {0}", ch.ChainStatus.Length)
        Console.WriteLine("Chain application policy count: {0}", ch.ChainPolicy.ApplicationPolicy.Count)
        Console.WriteLine("Chain certificate policy count: {0} {1}", ch.ChainPolicy.CertificatePolicy.Count, Environment.NewLine)

        '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()
    End Sub
End Class

Kommentarer

Objektet X509Chain har en global felstatus med namnet ChainStatus som ska användas för certifikatverifiering. Reglerna för certifikatverifiering är komplexa och det är enkelt att förenkla valideringslogik genom att ignorera felstatusen för ett eller flera av de berörda elementen. Den globala felstatusen tar hänsyn till statusen för varje element i kedjan.

Important

Från och med .NET Framework 4.6 implementerar den här typen gränssnittet IDisposable. När du har använt typen bör du kassera den på ett direkt eller indirekt sätt. Om du vill ta bort typen direkt anropar du dess Dispose metod i ett try/catch block. Om du vill ta bort det indirekt använder du en språkkonstruktion som using (i C#) eller Using (i Visual Basic). Mer information finns i avsnittet "Använda ett objekt som implementerar IDisposable" i IDisposable gränssnittet.

För appar som riktar sig mot .NET Framework 4.5.2 och tidigare versioner implementerar klassen X509Chain inte gränssnittet IDisposable och har därför ingen Dispose-metod.

Vid disponering X509Chaintas inte certifikaten i ChainElements bort. Du bör ta bort certifikaten i den här samlingen när certifikatinstanserna inte längre behövs.

Konstruktorer

Name Description
X509Chain()

Initierar en ny instans av X509Chain klassen.

X509Chain(Boolean)

Initierar en ny instans av X509Chain klassen som anger ett värde som anger om datorkontexten ska användas.

X509Chain(IntPtr)

Initierar en ny instans av klassen med hjälp av X509Chain ett IntPtr handtag till en X.509-kedja.

Egenskaper

Name Description
ChainContext

Hämtar ett handtag till en X.509-kedja.

ChainElements

Hämtar en samling X509ChainElement objekt.

ChainPolicy

Hämtar eller anger den X509ChainPolicy som ska användas när du skapar en X.509-certifikatkedja.

ChainStatus

Hämtar status för varje element i ett X509Chain objekt.

SafeHandle

Hämtar ett säkert handtag för den här instansen X509Chain .

Metoder

Name Description
Build(X509Certificate2)

Skapar en X.509-kedja med principen som anges i X509ChainPolicy.

Create()

Skapar ett X509Chain objekt efter att ha frågat efter mappningen som definierats i CryptoConfig-filen och mappar kedjan till den mappningen.

Dispose()

Släpper alla resurser som används av den här X509Chain.

Dispose(Boolean)

Släpper de ohanterade resurser som används av den här X509Chainoch släpper eventuellt de hanterade resurserna.

Equals(Object)

Avgör om det angivna objektet är lika med det aktuella objektet.

(Ärvd från Object)
GetHashCode()

Fungerar som standard-hash-funktion.

(Ärvd från Object)
GetType()

Hämtar den aktuella instansen Type .

(Ärvd från Object)
MemberwiseClone()

Skapar en ytlig kopia av den aktuella Object.

(Ärvd från Object)
Reset()

Rensar det aktuella X509Chain objektet.

ToString()

Returnerar en sträng som representerar det aktuella objektet.

(Ärvd från Object)

Gäller för