System.Net.Security
X509CertificateCollection Class
public class X509CertificateCollection : ICollection, IEnumerable
Represents a collection of X509Certificate objects. This class cannot be inherited.
Remarks
The X509CertificateCollection
class provides methods to manage a collection of X.509 certificates. It is useful for scenarios where you need to store and access multiple certificates, such as in SSL/TLS communication or authentication processes.
This collection is designed for use with the X509Certificate class and other classes within the System.Net.Security
namespace.
Members
Name | Description |
---|---|
Count |
Gets the number of certificates in the collection. |
IsSynchronized |
Gets a value indicating whether access to the collection is synchronized (thread safe). |
SyncRoot |
Gets an object that can be used to synchronize access to the collection. |
Add(X509Certificate certificate) |
Adds an X509Certificate object to the collection. |
Clear() |
Removes all certificates from the collection. |
Contains(X509Certificate certificate) |
Determines whether the collection contains the specified X509Certificate object. |
CopyTo(X509Certificate[] array, int index) |
Copies the certificates in the collection to a one-dimensional array, starting at the specified index. |
GetEnumerator() |
Returns an X509CertificateEnumerator object that can iterate through the collection. |
IndexOf(X509Certificate certificate) |
Returns the zero-based index of the first occurrence of a certificate in the collection. |
Insert(int index, X509Certificate certificate) |
Inserts an X509Certificate object into the collection at the specified zero-based index. |
Remove(X509Certificate certificate) |
Removes the first occurrence of a specific X509Certificate object from the collection. |
RemoveAt(int index) |
Removes the X509Certificate object at the specified zero-based index from the collection. |
Example
The following code example demonstrates how to create an X509CertificateCollection
, add certificates to it, and iterate through the collection.
using System;
using System.Net.Security;
public class CertificateCollectionExample
{
public static void Main(string[] args)
{
// Assuming you have X509Certificate objects
// For demonstration, we'll use dummy placeholders.
// In a real scenario, you would load certificates from files or the certificate store.
X509Certificate cert1 = new X509Certificate("path/to/your/certificate1.cer");
X509Certificate cert2 = new X509Certificate("path/to/your/certificate2.cer");
// Create a new collection
X509CertificateCollection certCollection = new X509CertificateCollection();
// Add certificates to the collection
certCollection.Add(cert1);
certCollection.Add(cert2);
Console.WriteLine($"Number of certificates in the collection: {certCollection.Count}");
// Iterate through the collection
Console.WriteLine("Certificates in the collection:");
foreach (X509Certificate cert in certCollection)
{
Console.WriteLine($"- Subject: {cert.Subject}");
Console.WriteLine($" Issuer: {cert.Issuer}");
}
// Check if a certificate exists
if (certCollection.Contains(cert1))
{
Console.WriteLine("Certificate 1 is in the collection.");
}
// Remove a certificate
certCollection.Remove(cert2);
Console.WriteLine($"Number of certificates after removing cert2: {certCollection.Count}");
}
}