本文整理汇总了C#中X509Certificate2Collection类的典型用法代码示例。如果您正苦于以下问题:C# X509Certificate2Collection类的具体用法?C# X509Certificate2Collection怎么用?C# X509Certificate2Collection使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
X509Certificate2Collection类属于命名空间,在下文中一共展示了X509Certificate2Collection类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: X509Certificate2CollectionConstructors
public static void X509Certificate2CollectionConstructors()
{
using (X509Certificate2 c1 = new X509Certificate2())
using (X509Certificate2 c2 = new X509Certificate2())
using (X509Certificate2 c3 = new X509Certificate2())
{
X509Certificate2Collection cc = new X509Certificate2Collection(new X509Certificate2[] { c1, c2, c3 });
Assert.Equal(3, cc.Count);
Assert.Same(c1, cc[0]);
Assert.Same(c2, cc[1]);
Assert.Same(c3, cc[2]);
X509Certificate2Collection cc2 = new X509Certificate2Collection(cc);
Assert.Equal(3, cc2.Count);
Assert.Same(c1, cc2[0]);
Assert.Same(c2, cc2[1]);
Assert.Same(c3, cc2[2]);
Assert.Throws<ArgumentNullException>(() => new X509Certificate2Collection(new X509Certificate2[] { c1, c2, null, c3 }));
using (X509Certificate c4 = new X509Certificate())
{
X509Certificate2Collection collection = new X509Certificate2Collection { c1, c2, c3 };
((IList)collection).Add(c4); // Add non-X509Certificate2 object
Assert.Throws<InvalidCastException>(() => new X509Certificate2Collection(collection));
}
}
}
示例2: ImportPkcs7PemBytes_Empty
public static void ImportPkcs7PemBytes_Empty()
{
var collection = new X509Certificate2Collection();
collection.Import(TestData.Pkcs7EmptyPemBytes);
Assert.Equal(0, collection.Count);
}
示例3: X509Certificate2CollectionEnumerator
public static void X509Certificate2CollectionEnumerator()
{
using (X509Certificate2 c1 = new X509Certificate2())
using (X509Certificate2 c2 = new X509Certificate2())
using (X509Certificate2 c3 = new X509Certificate2())
{
X509Certificate2Collection cc = new X509Certificate2Collection(new X509Certificate2[] { c1, c2, c3 });
X509Certificate2Enumerator e = cc.GetEnumerator();
object ignored;
// Not started
Assert.Throws<InvalidOperationException>(() => ignored = e.Current);
Assert.True(e.MoveNext());
Assert.Same(c1, e.Current);
Assert.True(e.MoveNext());
Assert.Same(c2, e.Current);
Assert.True(e.MoveNext());
Assert.Same(c3, e.Current);
Assert.False(e.MoveNext());
Assert.False(e.MoveNext());
Assert.False(e.MoveNext());
Assert.False(e.MoveNext());
Assert.False(e.MoveNext());
// ended.
Assert.Throws<InvalidOperationException>(() => ignored = e.Current);
}
}
示例4: ImportX509PemBytes
public static void ImportX509PemBytes()
{
var collection = new X509Certificate2Collection();
collection.Import(TestData.MsCertificatePemBytes);
Assert.Equal(1, collection.Count);
}
示例5: ImportX509PemFile
public static void ImportX509PemFile()
{
var collection = new X509Certificate2Collection();
collection.Import(Path.Combine("TestData", "MS.pem"));
Assert.Equal(1, collection.Count);
}
示例6: ImportedCollection
public ImportedCollection(X509Certificate2Collection collection)
{
// Make an independent copy of the certs to dispose (in case the test mutates the collection after we return.)
_certs = new X509Certificate2[collection.Count];
collection.CopyTo(_certs, 0);
Collection = collection;
}
示例7: ExportEmpty_Cert
public static void ExportEmpty_Cert()
{
var collection = new X509Certificate2Collection();
byte[] exported = collection.Export(X509ContentType.Cert);
Assert.Null(exported);
}
示例8: ImportEmpty_Pkcs12
public static void ImportEmpty_Pkcs12()
{
var collection = new X509Certificate2Collection();
collection.Import(TestData.EmptyPfx);
Assert.Equal(0, collection.Count);
}
示例9: ExportEmpty_Pkcs12
public static void ExportEmpty_Pkcs12()
{
var collection = new X509Certificate2Collection();
byte[] exported = collection.Export(X509ContentType.Pkcs12);
// The empty PFX is legal, the answer won't be null.
Assert.NotNull(exported);
}
示例10: ExportMultiplePrivateKeys
public static void ExportMultiplePrivateKeys()
{
var collection = new X509Certificate2Collection();
try
{
collection.Import(Path.Combine("TestData", "DummyTcpServer.pfx"), (string)null, X509KeyStorageFlags.Exportable | Cert.EphemeralIfPossible);
collection.Import(TestData.PfxData, TestData.PfxDataPassword, X509KeyStorageFlags.Exportable | Cert.EphemeralIfPossible);
// Pre-condition, we have multiple private keys
int originalPrivateKeyCount = collection.OfType<X509Certificate2>().Count(c => c.HasPrivateKey);
Assert.Equal(2, originalPrivateKeyCount);
// Export, re-import.
byte[] exported;
try
{
exported = collection.Export(X509ContentType.Pkcs12);
}
catch (PlatformNotSupportedException)
{
// [ActiveIssue(2743, TestPlatforms.AnyUnix)]
// Our Unix builds can't export more than one private key in a single PFX, so this is
// their exit point.
//
// If Windows gets here, or any exception other than PlatformNotSupportedException is raised,
// let that fail the test.
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
throw;
}
return;
}
// As the other half of issue 2743, if we make it this far we better be Windows (or remove the catch
// above)
Assert.True(RuntimeInformation.IsOSPlatform(OSPlatform.Windows), "RuntimeInformation.IsOSPlatform(OSPlatform.Windows)");
using (ImportedCollection ic = Cert.Import(exported))
{
X509Certificate2Collection importedCollection = ic.Collection;
Assert.Equal(collection.Count, importedCollection.Count);
int importedPrivateKeyCount = importedCollection.OfType<X509Certificate2>().Count(c => c.HasPrivateKey);
Assert.Equal(originalPrivateKeyCount, importedPrivateKeyCount);
}
}
finally
{
foreach (X509Certificate2 cert in collection)
{
cert.Dispose();
}
}
}
示例11: AddDoesNotClone
public static void AddDoesNotClone()
{
using (X509Certificate2 c1 = new X509Certificate2())
{
X509Certificate2Collection coll = new X509Certificate2Collection();
coll.Add(c1);
Assert.Same(c1, coll[0]);
}
}
示例12: AddCertificatesToStore
static void AddCertificatesToStore(string cert , string password , StoreLocation loc)
{
//Import the pfx certificates
X509Certificate2Collection certificates = new X509Certificate2Collection() ;
certificates.Import( cert , password , X509KeyStorageFlags.Exportable | X509KeyStorageFlags.PersistKeySet);
//Add the Certificate
X509Store store = new X509Store( storeName , loc) ; // , "Cool Store" ) ;
store.Open( OpenFlags.ReadWrite ) ;
store.AddRange( certificates ) ;
store.Close() ;
}
示例13: X509CertificateCollectionsProperties
public static void X509CertificateCollectionsProperties()
{
IList ilist = new X509CertificateCollection();
Assert.False(ilist.IsSynchronized);
Assert.False(ilist.IsFixedSize);
Assert.False(ilist.IsReadOnly);
ilist = new X509Certificate2Collection();
Assert.False(ilist.IsSynchronized);
Assert.False(ilist.IsFixedSize);
Assert.False(ilist.IsReadOnly);
}
示例14: BuildChainExtraStoreUntrustedRoot
public static void BuildChainExtraStoreUntrustedRoot()
{
using (var testCert = new X509Certificate2(Path.Combine("TestData", "test.pfx"), TestData.ChainPfxPassword))
{
X509Certificate2Collection collection = new X509Certificate2Collection();
collection.Import(Path.Combine("TestData", "test.pfx"), TestData.ChainPfxPassword, X509KeyStorageFlags.DefaultKeySet);
X509Chain chain = new X509Chain();
chain.ChainPolicy.ExtraStore.AddRange(collection);
chain.ChainPolicy.RevocationMode = X509RevocationMode.NoCheck;
chain.ChainPolicy.VerificationTime = new DateTime(2015, 9, 22, 12, 25, 0);
bool valid = chain.Build(testCert);
Assert.False(valid);
Assert.Contains(chain.ChainStatus, s => s.Status == X509ChainStatusFlags.UntrustedRoot);
}
}
示例15: MultipleImport
public static void MultipleImport()
{
var collection = new X509Certificate2Collection();
collection.Import(Path.Combine("TestData", "DummyTcpServer.pfx"), null, default(X509KeyStorageFlags));
collection.Import(TestData.PfxData, TestData.PfxDataPassword, default(X509KeyStorageFlags));
Assert.Equal(3, collection.Count);
}