本文整理汇总了C#中X509Certificate2Collection.OfType方法的典型用法代码示例。如果您正苦于以下问题:C# X509Certificate2Collection.OfType方法的具体用法?C# X509Certificate2Collection.OfType怎么用?C# X509Certificate2Collection.OfType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类X509Certificate2Collection
的用法示例。
在下文中一共展示了X509Certificate2Collection.OfType方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ExportUnrelatedPfx
public static void ExportUnrelatedPfx()
{
// Export multiple certificates which are not part of any kind of certificate chain.
// Nothing in the PKCS12 structure requires they're related, but it might be an underlying
// assumption of the provider.
using (var cert1 = new X509Certificate2(TestData.MsCertificate))
using (var cert2 = new X509Certificate2(TestData.ComplexNameInfoCert))
using (var cert3 = new X509Certificate2(TestData.CertWithPolicies))
{
var collection = new X509Certificate2Collection
{
cert1,
cert2,
cert3,
};
byte[] exported = collection.Export(X509ContentType.Pkcs12);
var importedCollection = new X509Certificate2Collection();
importedCollection.Import(exported);
// Verify that the two collections contain the same certificates,
// but the order isn't really a factor.
Assert.Equal(collection.Count, importedCollection.Count);
// Compare just the subject names first, because it's the easiest thing to read out of the failure message.
string[] subjects = new string[collection.Count];
string[] importedSubjects = new string[collection.Count];
for (int i = 0; i < collection.Count; i++)
{
subjects[i] = collection[i].GetNameInfo(X509NameType.SimpleName, false);
importedSubjects[i] = importedCollection[i].GetNameInfo(X509NameType.SimpleName, false);
}
Assert.Equal(subjects, importedSubjects);
// But, really, the collections should be equivalent
// (after being coerced to IEnumerable<X509Certificate2>)
Assert.Equal(collection.OfType<X509Certificate2>(), importedCollection.OfType<X509Certificate2>());
}
}
示例2: ExportMultiplePrivateKeys
public static void ExportMultiplePrivateKeys()
{
var collection = new X509Certificate2Collection();
collection.Import(Path.Combine("TestData", "DummyTcpServer.pfx"), null, X509KeyStorageFlags.Exportable);
collection.Import(TestData.PfxData, TestData.PfxDataPassword, X509KeyStorageFlags.Exportable);
// 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, PlatformID.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)");
var importedCollection = new X509Certificate2Collection();
importedCollection.Import(exported);
Assert.Equal(collection.Count, importedCollection.Count);
int importedPrivateKeyCount = importedCollection.OfType<X509Certificate2>().Count(c => c.HasPrivateKey);
Assert.Equal(originalPrivateKeyCount, importedPrivateKeyCount);
}
示例3: ImportFullChainPfx
public static void ImportFullChainPfx()
{
X509Certificate2Collection certs = new X509Certificate2Collection();
certs.Import(Path.Combine("TestData", "test.pfx"), "test", X509KeyStorageFlags.DefaultKeySet);
int count = certs.Count;
Assert.Equal(3, count);
// Verify that the read ordering is consistent across the platforms
string[] expectedSubjects =
{
"MS Passport Test Sub CA",
"MS Passport Test Root CA",
"test.local",
};
string[] actualSubjects = certs.OfType<X509Certificate2>().
Select(cert => cert.GetNameInfo(X509NameType.SimpleName, false)).
ToArray();
Assert.Equal(expectedSubjects, actualSubjects);
// And verify that we have private keys when we expect them
bool[] expectedHasPrivateKeys =
{
false,
false,
true,
};
bool[] actualHasPrivateKeys = certs.OfType<X509Certificate2>().
Select(cert => cert.HasPrivateKey).
ToArray();
Assert.Equal(expectedHasPrivateKeys, actualHasPrivateKeys);
}