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表示。 如果熟悉非托管加密 API 构造,则可以将 X509Certificate2Collection 视为 对象的内存存储 X509Certificate2 。
构造函数
X509Certificate2Collection() |
不使用任何 X509Certificate2Collection 信息初始化 X509Certificate2 类的新实例。 |
X509Certificate2Collection(X509Certificate2) |
使用 X509Certificate2 对象初始化 X509Certificate2Collection 类的新实例。 |
X509Certificate2Collection(X509Certificate2[]) |
使用 X509Certificate2Collection 对象的数组初始化 X509Certificate2 类的新实例。 |
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) |
扩展方法
适用于
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈