本文整理汇总了C#中X509Certificate2Collection.Export方法的典型用法代码示例。如果您正苦于以下问题:C# X509Certificate2Collection.Export方法的具体用法?C# X509Certificate2Collection.Export怎么用?C# X509Certificate2Collection.Export使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类X509Certificate2Collection
的用法示例。
在下文中一共展示了X509Certificate2Collection.Export方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ExportEmpty_Cert
public static void ExportEmpty_Cert()
{
var collection = new X509Certificate2Collection();
byte[] exported = collection.Export(X509ContentType.Cert);
Assert.Null(exported);
}
示例2: 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);
}
示例3: 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);
}
示例4: 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);
// TODO (#3207): Make this test be order-required once ordering is guaranteed on all platforms.
AssertEqualUnordered(collection, importedCollection);
}
}
示例5: TestExportStore
private static void TestExportStore(X509ContentType ct)
{
using (var msCer = new X509Certificate2(TestData.MsCertificate))
using (var pfxCer = new X509Certificate2(TestData.PfxData, TestData.PfxDataPassword))
{
X509Certificate2Collection cc = new X509Certificate2Collection(new X509Certificate2[] { msCer, pfxCer });
byte[] blob = cc.Export(ct);
Assert.Equal(ct, X509Certificate2.GetCertContentType(blob));
X509Certificate2Collection cc2 = new X509Certificate2Collection();
cc2.Import(blob);
int count = cc2.Count;
Assert.Equal(2, count);
X509Certificate2[] cs = cc2.ToArray().OrderBy(c => c.Subject).ToArray();
using (X509Certificate2 first = cs[0])
{
Assert.NotSame(msCer, first);
Assert.Equal(msCer, first);
}
using (X509Certificate2 second = cs[1])
{
Assert.NotSame(pfxCer, second);
Assert.Equal(pfxCer, second);
}
}
}
示例6: TestExportSingleCert
private static void TestExportSingleCert(X509ContentType ct)
{
using (var msCer = new X509Certificate2(TestData.MsCertificate))
using (var pfxCer = new X509Certificate2(TestData.PfxData, TestData.PfxDataPassword))
{
X509Certificate2Collection cc = new X509Certificate2Collection(new X509Certificate2[] { msCer, pfxCer });
byte[] blob = cc.Export(ct);
Assert.Equal(ct, X509Certificate2.GetCertContentType(blob));
X509Certificate2Collection cc2 = new X509Certificate2Collection();
cc2.Import(blob);
int count = cc2.Count;
Assert.Equal(1, count);
using (X509Certificate2 c = cc2[0])
{
Assert.NotSame(msCer, c);
Assert.NotSame(pfxCer, c);
Assert.True(msCer.Equals(c) || pfxCer.Equals(c));
}
}
}
示例7: 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>());
}
}
示例8: ExportSerializedStore_Unix
public static void ExportSerializedStore_Unix()
{
using (var msCer = new X509Certificate2(TestData.MsCertificate))
using (var ecdsa256Cer = new X509Certificate2(TestData.ECDsa256Certificate))
{
X509Certificate2Collection cc = new X509Certificate2Collection(new[] { msCer, ecdsa256Cer });
Assert.Throws<PlatformNotSupportedException>(() => cc.Export(X509ContentType.SerializedStore));
}
}