X509Certificate2Collection X509Certificate2Collection X509Certificate2Collection X509Certificate2Collection Class

Definition

Represents a collection of X509Certificate2 objects. This class cannot be inherited.

public ref class X509Certificate2Collection : System::Security::Cryptography::X509Certificates::X509CertificateCollection
public class X509Certificate2Collection : System.Security.Cryptography.X509Certificates.X509CertificateCollection
type X509Certificate2Collection = class
    inherit X509CertificateCollection
Public Class X509Certificate2Collection
Inherits X509CertificateCollection
Inheritance

Examples

The following code example opens the current user's personal certificate store, selects only valid certificates, allows the user to select a certificate, and then writes certificate and certificate chain information to the console. The output depends on the certificate the user selects.

#using <System.dll>
#using <System.Security.dll>

using namespace System;
using namespace System::Security::Cryptography;
using namespace System::Security::Permissions;
using namespace System::IO;
using namespace System::Security::Cryptography::X509Certificates;
int main()
{
   try
   {
      X509Store ^ store = gcnew X509Store( "MY",StoreLocation::CurrentUser );
      store->Open( static_cast<OpenFlags>(OpenFlags::ReadOnly | OpenFlags::OpenExistingOnly) );
      X509Certificate2Collection ^ collection = dynamic_cast<X509Certificate2Collection^>(store->Certificates);
      X509Certificate2Collection ^ fcollection = dynamic_cast<X509Certificate2Collection^>(collection->Find( X509FindType::FindByTimeValid, DateTime::Now, false ));
      X509Certificate2Collection ^ scollection = X509Certificate2UI::SelectFromCollection(fcollection, "Test Certificate Select","Select a certificate from the following list to get information on that certificate",X509SelectionFlag::MultiSelection);
      Console::WriteLine( "Number of certificates: {0}{1}", scollection->Count, Environment::NewLine );
      System::Collections::IEnumerator^ myEnum = scollection->GetEnumerator();
      while ( myEnum->MoveNext() )
      {
         X509Certificate2 ^ x509 = safe_cast<X509Certificate2 ^>(myEnum->Current);
         array<Byte>^rawdata = x509->RawData;
         Console::WriteLine( "Content Type: {0}{1}", X509Certificate2::GetCertContentType( rawdata ), Environment::NewLine );
         Console::WriteLine( "Friendly Name: {0}{1}", x509->FriendlyName, Environment::NewLine );
         Console::WriteLine( "Certificate Verified?: {0}{1}", x509->Verify(), Environment::NewLine );
         Console::WriteLine( "Simple Name: {0}{1}", x509->GetNameInfo( X509NameType::SimpleName, true ), Environment::NewLine );
         Console::WriteLine( "Signature Algorithm: {0}{1}", x509->SignatureAlgorithm->FriendlyName, Environment::NewLine );
         Console::WriteLine( "Private Key: {0}{1}", x509->PrivateKey->ToXmlString( false ), Environment::NewLine );
         Console::WriteLine( "Public Key: {0}{1}", x509->PublicKey->Key->ToXmlString( false ), Environment::NewLine );
         Console::WriteLine( "Certificate Archived?: {0}{1}", x509->Archived, Environment::NewLine );
         Console::WriteLine( "Length of Raw Data: {0}{1}", x509->RawData->Length, Environment::NewLine );
         x509->Reset();
      }
      store->Close();
   }
   catch ( CryptographicException^ ) 
   {
      Console::WriteLine( "Information could not be written out for this certificate." );
   }

}

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

class CertSelect
{
    static void Main()
    {
        X509Store store = new X509Store("MY",StoreLocation.CurrentUser);
        store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);

        X509Certificate2Collection collection = (X509Certificate2Collection)store.Certificates;
        X509Certificate2Collection fcollection = (X509Certificate2Collection)collection.Find(X509FindType.FindByTimeValid,DateTime.Now,false);
        X509Certificate2Collection scollection = X509Certificate2UI.SelectFromCollection(fcollection, "Test Certificate Select","Select a certificate from the following list to get information on that certificate",X509SelectionFlag.MultiSelection);
        Console.WriteLine("Number of certificates: {0}{1}",scollection.Count,Environment.NewLine);

        foreach (X509Certificate2 x509 in scollection)
        {
            try
            {
                byte[] rawdata = x509.RawData;
                Console.WriteLine("Content Type: {0}{1}",X509Certificate2.GetCertContentType(rawdata),Environment.NewLine);
                Console.WriteLine("Friendly Name: {0}{1}",x509.FriendlyName,Environment.NewLine);
                Console.WriteLine("Certificate Verified?: {0}{1}",x509.Verify(),Environment.NewLine);
                Console.WriteLine("Simple Name: {0}{1}",x509.GetNameInfo(X509NameType.SimpleName,true),Environment.NewLine);
                Console.WriteLine("Signature Algorithm: {0}{1}",x509.SignatureAlgorithm.FriendlyName,Environment.NewLine);
                Console.WriteLine("Private Key: {0}{1}",x509.PrivateKey.ToXmlString(false),Environment.NewLine);
                Console.WriteLine("Public Key: {0}{1}",x509.PublicKey.Key.ToXmlString(false),Environment.NewLine);
                Console.WriteLine("Certificate Archived?: {0}{1}",x509.Archived,Environment.NewLine);
                Console.WriteLine("Length of Raw Data: {0}{1}",x509.RawData.Length,Environment.NewLine);
                X509Certificate2UI.DisplayCertificate(x509);
                x509.Reset();
            }
            catch (CryptographicException)
            {
                Console.WriteLine("Information could not be written out for this certificate.");
            }
        }
        store.Close();
    }
}
Imports System
Imports System.Security.Cryptography
Imports System.Security.Permissions
Imports System.IO
Imports System.Security.Cryptography.X509Certificates

Class CertSelect

    Shared Sub Main()

        Dim store As New X509Store("MY", StoreLocation.CurrentUser)
        store.Open(OpenFlags.ReadOnly Or OpenFlags.OpenExistingOnly)

        Dim collection As X509Certificate2Collection = CType(store.Certificates, X509Certificate2Collection)
        Dim fcollection As X509Certificate2Collection = CType(collection.Find(X509FindType.FindByTimeValid, DateTime.Now, False), X509Certificate2Collection)
        Dim scollection As X509Certificate2Collection = X509Certificate2UI.SelectFromCollection(fcollection, "Test Certificate Select", "Select a certificate from the following list to get information on that certificate", X509SelectionFlag.MultiSelection)
        Console.WriteLine("Number of certificates: {0}{1}", scollection.Count, Environment.NewLine)
         
        For Each x509 As X509Certificate2 In scollection
            Try
                Dim rawdata As Byte() = x509.RawData
                Console.WriteLine("Content Type: {0}{1}", X509Certificate2.GetCertContentType(rawdata), Environment.NewLine)
                Console.WriteLine("Friendly Name: {0}{1}", x509.FriendlyName, Environment.NewLine)
                Console.WriteLine("Certificate Verified?: {0}{1}", x509.Verify(), Environment.NewLine)
                Console.WriteLine("Simple Name: {0}{1}", x509.GetNameInfo(X509NameType.SimpleName, True), Environment.NewLine)
                Console.WriteLine("Signature Algorithm: {0}{1}", x509.SignatureAlgorithm.FriendlyName, Environment.NewLine)
                Console.WriteLine("Private Key: {0}{1}", x509.PrivateKey.ToXmlString(False), Environment.NewLine)
                Console.WriteLine("Public Key: {0}{1}", x509.PublicKey.Key.ToXmlString(False), Environment.NewLine)
                Console.WriteLine("Certificate Archived?: {0}{1}", x509.Archived, Environment.NewLine)
                Console.WriteLine("Length of Raw Data: {0}{1}", x509.RawData.Length, Environment.NewLine)
                X509Certificate2UI.DisplayCertificate(x509)
                x509.Reset()         
             Catch cExcept As CryptographicException
                 Console.WriteLine("Information could not be written out for this certificate.")
             End Try
        Next x509

        store.Close()
    End Sub
End Class

Remarks

When an X509Certificate2 store is opened, the result is represented by an X509Certificate2Collection object. If you are familiar with the unmanaged Cryptographic API constructs, you can think of an X509Certificate2Collection as a memory store of X509Certificate2 objects.

Constructors

X509Certificate2Collection() X509Certificate2Collection() X509Certificate2Collection() X509Certificate2Collection()

Initializes a new instance of the X509Certificate2Collection class without any X509Certificate2 information.

X509Certificate2Collection(X509Certificate2) X509Certificate2Collection(X509Certificate2) X509Certificate2Collection(X509Certificate2) X509Certificate2Collection(X509Certificate2)

Initializes a new instance of the X509Certificate2Collection class using an X509Certificate2 object.

X509Certificate2Collection(X509Certificate2Collection) X509Certificate2Collection(X509Certificate2Collection) X509Certificate2Collection(X509Certificate2Collection) X509Certificate2Collection(X509Certificate2Collection)

Initializes a new instance of the X509Certificate2Collection class using the specified certificate collection.

X509Certificate2Collection(X509Certificate2[]) X509Certificate2Collection(X509Certificate2[]) X509Certificate2Collection(X509Certificate2[]) X509Certificate2Collection(X509Certificate2[])

Initializes a new instance of the X509Certificate2Collection class using an array of X509Certificate2 objects.

Properties

Capacity Capacity Capacity Capacity

Gets or sets the number of elements that the CollectionBase can contain.

(Inherited from CollectionBase)
Count Count Count Count Inherited from X509CertificateCollection
ICollection.IsSynchronized ICollection.IsSynchronized ICollection.IsSynchronized ICollection.IsSynchronized Inherited from X509CertificateCollection
ICollection.SyncRoot ICollection.SyncRoot ICollection.SyncRoot ICollection.SyncRoot Inherited from X509CertificateCollection
IList.IsFixedSize IList.IsFixedSize IList.IsFixedSize IList.IsFixedSize Inherited from X509CertificateCollection
IList.IsReadOnly IList.IsReadOnly IList.IsReadOnly IList.IsReadOnly Inherited from X509CertificateCollection
IList.Item[Int32] IList.Item[Int32] IList.Item[Int32] IList.Item[Int32] Inherited from X509CertificateCollection
InnerList InnerList InnerList InnerList

Gets an ArrayList containing the list of elements in the CollectionBase instance.

(Inherited from CollectionBase)
Item[Int32] Item[Int32] Item[Int32] Item[Int32]

Gets or sets the element at the specified index.

List List List List

Gets an IList containing the list of elements in the CollectionBase instance.

(Inherited from CollectionBase)

Methods

Add(X509Certificate) Add(X509Certificate) Add(X509Certificate) Add(X509Certificate)

Adds an X509Certificate with the specified value to the current X509CertificateCollection.

(Inherited from X509CertificateCollection)
Add(X509Certificate2) Add(X509Certificate2) Add(X509Certificate2) Add(X509Certificate2)

Adds an object to the end of the X509Certificate2Collection.

AddRange(X509Certificate2Collection) AddRange(X509Certificate2Collection) AddRange(X509Certificate2Collection) AddRange(X509Certificate2Collection)

Adds multiple X509Certificate2 objects in an X509Certificate2Collection object to another X509Certificate2Collection object.

AddRange(X509Certificate2[]) AddRange(X509Certificate2[]) AddRange(X509Certificate2[]) AddRange(X509Certificate2[])

Adds multiple X509Certificate2 objects in an array to the X509Certificate2Collection object.

AddRange(X509CertificateCollection) AddRange(X509CertificateCollection) AddRange(X509CertificateCollection) AddRange(X509CertificateCollection)

Copies the elements of the specified X509CertificateCollection to the end of the current X509CertificateCollection.

(Inherited from X509CertificateCollection)
AddRange(X509Certificate[]) AddRange(X509Certificate[]) AddRange(X509Certificate[]) AddRange(X509Certificate[])

Copies the elements of an array of type X509Certificate to the end of the current X509CertificateCollection.

(Inherited from X509CertificateCollection)
Clear() Clear() Clear() Clear() Inherited from X509CertificateCollection
Contains(X509Certificate) Contains(X509Certificate) Contains(X509Certificate) Contains(X509Certificate)

Gets a value indicating whether the current X509CertificateCollection contains the specified X509Certificate.

(Inherited from X509CertificateCollection)
Contains(X509Certificate2) Contains(X509Certificate2) Contains(X509Certificate2) Contains(X509Certificate2)

Determines whether the X509Certificate2Collection object contains a specific certificate.

CopyTo(X509Certificate[], Int32) CopyTo(X509Certificate[], Int32) CopyTo(X509Certificate[], Int32) CopyTo(X509Certificate[], Int32)

Copies the X509Certificate values in the current X509CertificateCollection to a one-dimensional Array instance at the specified index.

(Inherited from X509CertificateCollection)
Equals(Object) Equals(Object) Equals(Object) Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
Export(X509ContentType) Export(X509ContentType) Export(X509ContentType) Export(X509ContentType)

Exports X.509 certificate information into a byte array.

Export(X509ContentType, String) Export(X509ContentType, String) Export(X509ContentType, String) Export(X509ContentType, String)

Exports X.509 certificate information into a byte array using a password.

Find(X509FindType, Object, Boolean) Find(X509FindType, Object, Boolean) Find(X509FindType, Object, Boolean) Find(X509FindType, Object, Boolean)

Searches an X509Certificate2Collection object using the search criteria specified by the X509FindType enumeration and the findValue object.

GetEnumerator() GetEnumerator() GetEnumerator() GetEnumerator()

Returns an enumerator that can iterate through a X509Certificate2Collection object.

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

Builds a hash value based on all values contained in the current X509CertificateCollection.

(Inherited from X509CertificateCollection)
GetType() GetType() GetType() GetType()

Gets the Type of the current instance.

(Inherited from Object)
Import(Byte[]) Import(Byte[]) Import(Byte[]) Import(Byte[])

Imports a certificate in the form of a byte array into a X509Certificate2Collection object.

Import(Byte[], String, X509KeyStorageFlags) Import(Byte[], String, X509KeyStorageFlags) Import(Byte[], String, X509KeyStorageFlags) Import(Byte[], String, X509KeyStorageFlags)

Imports a certificate, in the form of a byte array that requires a password to access the certificate, into a X509Certificate2Collection object.

Import(String) Import(String) Import(String) Import(String)

Imports a certificate file into a X509Certificate2Collection object.

Import(String, String, X509KeyStorageFlags) Import(String, String, X509KeyStorageFlags) Import(String, String, X509KeyStorageFlags) Import(String, String, X509KeyStorageFlags)

Imports a certificate file that requires a password into a X509Certificate2Collection object.

IndexOf(X509Certificate) IndexOf(X509Certificate) IndexOf(X509Certificate) IndexOf(X509Certificate)

Returns the index of the specified X509Certificate in the current X509CertificateCollection.

(Inherited from X509CertificateCollection)
Insert(Int32, X509Certificate) Insert(Int32, X509Certificate) Insert(Int32, X509Certificate) Insert(Int32, X509Certificate)

Inserts a X509Certificate into the current X509CertificateCollection at the specified index.

(Inherited from X509CertificateCollection)
Insert(Int32, X509Certificate2) Insert(Int32, X509Certificate2) Insert(Int32, X509Certificate2) Insert(Int32, X509Certificate2)

Inserts an object into the X509Certificate2Collection object at the specified index.

MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
OnClear() OnClear() OnClear() OnClear()

Performs additional custom processes when clearing the contents of the CollectionBase instance.

(Inherited from CollectionBase)
OnClearComplete() OnClearComplete() OnClearComplete() OnClearComplete()

Performs additional custom processes after clearing the contents of the CollectionBase instance.

(Inherited from CollectionBase)
OnInsert(Int32, Object) OnInsert(Int32, Object) OnInsert(Int32, Object) OnInsert(Int32, Object)

Performs additional custom processes before inserting a new element into the CollectionBase instance.

(Inherited from CollectionBase)
OnInsertComplete(Int32, Object) OnInsertComplete(Int32, Object) OnInsertComplete(Int32, Object) OnInsertComplete(Int32, Object)

Performs additional custom processes after inserting a new element into the CollectionBase instance.

(Inherited from CollectionBase)
OnRemove(Int32, Object) OnRemove(Int32, Object) OnRemove(Int32, Object) OnRemove(Int32, Object)

Performs additional custom processes when removing an element from the CollectionBase instance.

(Inherited from CollectionBase)
OnRemoveComplete(Int32, Object) OnRemoveComplete(Int32, Object) OnRemoveComplete(Int32, Object) OnRemoveComplete(Int32, Object)

Performs additional custom processes after removing an element from the CollectionBase instance.

(Inherited from CollectionBase)
OnSet(Int32, Object, Object) OnSet(Int32, Object, Object) OnSet(Int32, Object, Object) OnSet(Int32, Object, Object)

Performs additional custom processes before setting a value in the CollectionBase instance.

(Inherited from CollectionBase)
OnSetComplete(Int32, Object, Object) OnSetComplete(Int32, Object, Object) OnSetComplete(Int32, Object, Object) OnSetComplete(Int32, Object, Object)

Performs additional custom processes after setting a value in the CollectionBase instance.

(Inherited from CollectionBase)
OnValidate(Object) OnValidate(Object) OnValidate(Object) OnValidate(Object) Inherited from X509CertificateCollection
Remove(X509Certificate) Remove(X509Certificate) Remove(X509Certificate) Remove(X509Certificate)

Removes a specific X509Certificate from the current X509CertificateCollection.

(Inherited from X509CertificateCollection)
Remove(X509Certificate2) Remove(X509Certificate2) Remove(X509Certificate2) Remove(X509Certificate2)

Removes the first occurrence of a certificate from the X509Certificate2Collection object.

RemoveAt(Int32) RemoveAt(Int32) RemoveAt(Int32) RemoveAt(Int32) Inherited from X509CertificateCollection
RemoveRange(X509Certificate2Collection) RemoveRange(X509Certificate2Collection) RemoveRange(X509Certificate2Collection) RemoveRange(X509Certificate2Collection)

Removes multiple X509Certificate2 objects in an X509Certificate2Collection object from another X509Certificate2Collection object.

RemoveRange(X509Certificate2[]) RemoveRange(X509Certificate2[]) RemoveRange(X509Certificate2[]) RemoveRange(X509Certificate2[])

Removes multiple X509Certificate2 objects in an array from an X509Certificate2Collection object.

ToString() ToString() ToString() ToString()

Returns a string that represents the current object.

(Inherited from Object)

Explicit Interface Implementations

ICollection.CopyTo(Array, Int32) ICollection.CopyTo(Array, Int32) ICollection.CopyTo(Array, Int32) ICollection.CopyTo(Array, Int32) Inherited from X509CertificateCollection
IEnumerable.GetEnumerator() IEnumerable.GetEnumerator() IEnumerable.GetEnumerator() IEnumerable.GetEnumerator() Inherited from X509CertificateCollection
IList.Add(Object) IList.Add(Object) IList.Add(Object) IList.Add(Object) Inherited from X509CertificateCollection
IList.Contains(Object) IList.Contains(Object) IList.Contains(Object) IList.Contains(Object) Inherited from X509CertificateCollection
IList.IndexOf(Object) IList.IndexOf(Object) IList.IndexOf(Object) IList.IndexOf(Object) Inherited from X509CertificateCollection
IList.Insert(Int32, Object) IList.Insert(Int32, Object) IList.Insert(Int32, Object) IList.Insert(Int32, Object) Inherited from X509CertificateCollection
IList.Remove(Object) IList.Remove(Object) IList.Remove(Object) IList.Remove(Object) Inherited from X509CertificateCollection

Extension Methods

Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable)

Casts the elements of an IEnumerable to the specified type.

OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable)

Filters the elements of an IEnumerable based on a specified type.

AsParallel(IEnumerable) AsParallel(IEnumerable) AsParallel(IEnumerable) AsParallel(IEnumerable)

Enables parallelization of a query.

AsQueryable(IEnumerable) AsQueryable(IEnumerable) AsQueryable(IEnumerable) AsQueryable(IEnumerable)

Converts an IEnumerable to an IQueryable.

Applies to