X509Certificate2Collection 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
表示 X509Certificate2 物件的集合。 無法繼承這個類別。
public ref class X509Certificate2Collection : System::Security::Cryptography::X509Certificates::X509CertificateCollection
public ref class X509Certificate2Collection : System::Security::Cryptography::X509Certificates::X509CertificateCollection, System::Collections::Generic::IEnumerable<System::Security::Cryptography::X509Certificates::X509Certificate2 ^>
public class X509Certificate2Collection : System.Security.Cryptography.X509Certificates.X509CertificateCollection
public class X509Certificate2Collection : System.Security.Cryptography.X509Certificates.X509CertificateCollection, System.Collections.Generic.IEnumerable<System.Security.Cryptography.X509Certificates.X509Certificate2>
type X509Certificate2Collection = class
inherit X509CertificateCollection
type X509Certificate2Collection = class
inherit X509CertificateCollection
interface seq<X509Certificate2>
interface IEnumerable
Public Class X509Certificate2Collection
Inherits X509CertificateCollection
Public Class X509Certificate2Collection
Inherits X509CertificateCollection
Implements IEnumerable(Of X509Certificate2)
- 繼承
- 繼承
- 實作
範例
下列程式代碼範例會開啟目前使用者的個人證書存儲、只選取有效的憑證、允許用戶選取憑證,然後將憑證和憑證鏈結資訊寫入控制台。 輸出取決於用戶選取的憑證。
#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("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.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("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
備註
開啟 X509Certificate2 存放區時,結果會以 X509Certificate2Collection 物件表示。 如果您熟悉 Unmanaged 密碼編譯 API 建構,您可以將 X509Certificate2Collection 視為 X509Certificate2 物件的記憶體存放區。
建構函式
X509Certificate2Collection() |
在沒有任何 X509Certificate2 信息的情況下,初始化 X509Certificate2Collection 類別的新實例。 |
X509Certificate2Collection(X509Certificate2) |
使用 X509Certificate2 物件,初始化 X509Certificate2Collection 類別的新實例。 |
X509Certificate2Collection(X509Certificate2[]) |
使用 X509Certificate2 對象的陣列,初始化 X509Certificate2Collection 類別的新實例。 |
X509Certificate2Collection(X509Certificate2Collection) |
使用指定的憑證集合,初始化 X509Certificate2Collection 類別的新實例。 |
屬性
Capacity |
取得或設定 CollectionBase 可以包含的項目數目。 (繼承來源 CollectionBase) |
Count |
取得集合中包含的項目數目。 (繼承來源 X509CertificateCollection) |
InnerList |
取得 ArrayList,其中包含 CollectionBase 實例中的專案清單。 (繼承來源 CollectionBase) |
Item[Int32] |
取得或設定位於指定索引處的專案。 |
List |
取得 IList,其中包含 CollectionBase 實例中的專案清單。 (繼承來源 CollectionBase) |
方法
明確介面實作
ICollection.CopyTo(Array, Int32) |
從目標陣列的指定索引開始,將整個集合複製到相容的一維陣列。 (繼承來源 X509CertificateCollection) |
ICollection.IsSynchronized |
取得值,指出集合的存取是否同步處理 (線程安全)。 (繼承來源 X509CertificateCollection) |
ICollection.SyncRoot |
取得對象,這個物件可用來同步存取集合。 (繼承來源 X509CertificateCollection) |
IEnumerable.GetEnumerator() |
傳回逐一查看集合的列舉值。 (繼承來源 X509CertificateCollection) |
IEnumerable<X509Certificate2>.GetEnumerator() |
傳回逐一查看集合的列舉值。 |
IList.Add(Object) |
將物件加入至集合的結尾。 (繼承來源 X509CertificateCollection) |
IList.Contains(Object) |
判斷集合是否包含特定專案。 (繼承來源 X509CertificateCollection) |
IList.IndexOf(Object) |
搜尋指定的物件,並傳回集合中第一次出現之以零起始的索引。 (繼承來源 X509CertificateCollection) |
IList.Insert(Int32, Object) |
將專案插入集合中指定的索引處。 (繼承來源 X509CertificateCollection) |
IList.IsFixedSize |
取得值,指出集合是否具有固定大小。 (繼承來源 X509CertificateCollection) |
IList.IsReadOnly |
取得值,指出集合是否為唯讀。 (繼承來源 X509CertificateCollection) |
IList.Item[Int32] |
取得或設定位於指定索引處的專案。 (繼承來源 X509CertificateCollection) |
IList.Remove(Object) |
從集合中移除第一個出現的特定物件。 (繼承來源 X509CertificateCollection) |