.NET Documentation

System.Net.Security

X509Certificate2Collection Class

Represents a collection of X509Certificate2 objects.

Remarks

The X509Certificate2Collection class inherits from the X509CertificateCollection class and provides functionality for managing a collection of X.509 certificates. This class is used in scenarios where you need to store and retrieve multiple certificates, such as in client authentication or server certificate validation.

You can add, remove, and access individual certificates within the collection. The class also supports searching for certificates based on various criteria.

Inheritance

ObjectX509CertificateCollectionX509Certificate2Collection

Thread Safety

Public static (Shared in Visual Basic) members of this type are thread-safe. Any instance members are not guaranteed to be thread-safe.

Constructors

X509Certificate2Collection()

Initializes a new instance of the X509Certificate2Collection class.

X509Certificate2Collection(X509Certificate2Collection value)

Initializes a new instance of the X509Certificate2Collection class with the contents of the specified collection.

X509Certificate2Collection(X509Certificate[] certificates)

Initializes a new instance of the X509Certificate2Collection class with the specified array of X.509 certificates.

Properties

Item(Int32 index)

Gets or sets the certificate at the specified index in the collection.

X509Certificate2 Item(

  • Int32 index
  • )

    Methods

    Add(X509Certificate2 certificate)

    Adds a certificate to the collection.

    Void Add(

  • X509Certificate2 certificate
  • )

    AddRange(X509Certificate2Collection certificates)

    Adds the certificates from another collection to this collection.

    Void AddRange(

  • X509Certificate2Collection certificates
  • )

    AddRange(X509Certificate[] certificates)

    Adds the certificates from an array to this collection.

    Void AddRange(

  • X509Certificate[] certificates
  • )

    Contains(X509Certificate2 certificate)

    Determines whether the collection contains the specified certificate.

    Boolean Contains(

  • X509Certificate2 certificate
  • )

    CopyTo(X509Certificate2[] array, Int32 index)

    Copies the entire collection to a compatible one-dimensional array, starting at the specified index of the target array.

    Void CopyTo(

  • X509Certificate2[] array
  • Int32 index
  • )

    Find(X509FindType findType, Object findValue, Boolean validOnly)

    Searches the collection for certificates that match the specified criteria.

    X509Certificate2Collection Find(

  • X509FindType findType
  • Object findValue
  • Boolean validOnly
  • )

    IndexOf(X509Certificate2 certificate)

    Returns the zero-based index of the first occurrence of the specified certificate in the entire collection.

    Int32 IndexOf(

  • X509Certificate2 certificate
  • )

    Remove(X509Certificate2 certificate)

    Removes the first occurrence of the specified certificate from the collection.

    Boolean Remove(

  • X509Certificate2 certificate
  • )

    RemoveAt(Int32 index)

    Removes the certificate at the specified index from the collection.

    Void RemoveAt(

  • Int32 index
  • )

    Example

    C# Example

    
    using System;
    using System.Net.Security;
    using System.Security.Cryptography.X509Certificates;
    
    public class CertificateCollectionExample
    {
        public static void Main(string[] args)
        {
            // Create a new collection
            X509Certificate2Collection certCollection = new X509Certificate2Collection();
    
            // Load a certificate (replace with your certificate path)
            try
            {
                X509Certificate2 cert1 = new X509Certificate2("mycertificate.pfx", "password");
                certCollection.Add(cert1);
    
                X509Certificate2 cert2 = new X509Certificate2("anothercert.cer");
                certCollection.Add(cert2);
    
                Console.WriteLine($"Number of certificates in collection: {certCollection.Count}");
    
                // Find a certificate by subject name
                X509Certificate2Collection foundCerts = certCollection.Find(
                    X509FindType.FindBySubjectName, "My Test Certificate", true);
    
                if (foundCerts.Count > 0)
                {
                    Console.WriteLine($"Found certificate: {foundCerts[0].Subject}");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"An error occurred: {ex.Message}");
            }
        }
    }