本文整理汇总了C#中X509Certificate2Collection.Import方法的典型用法代码示例。如果您正苦于以下问题:C# X509Certificate2Collection.Import方法的具体用法?C# X509Certificate2Collection.Import怎么用?C# X509Certificate2Collection.Import使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类X509Certificate2Collection
的用法示例。
在下文中一共展示了X509Certificate2Collection.Import方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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();
}
}
}
示例2: ImportPkcs7PemBytes_Empty
public static void ImportPkcs7PemBytes_Empty()
{
var collection = new X509Certificate2Collection();
collection.Import(TestData.Pkcs7EmptyPemBytes);
Assert.Equal(0, collection.Count);
}
示例3: ImportX509PemBytes
public static void ImportX509PemBytes()
{
var collection = new X509Certificate2Collection();
collection.Import(TestData.MsCertificatePemBytes);
Assert.Equal(1, collection.Count);
}
示例4: ImportX509PemFile
public static void ImportX509PemFile()
{
var collection = new X509Certificate2Collection();
collection.Import(Path.Combine("TestData", "MS.pem"));
Assert.Equal(1, collection.Count);
}
示例5: ImportEmpty_Pkcs12
public static void ImportEmpty_Pkcs12()
{
var collection = new X509Certificate2Collection();
collection.Import(TestData.EmptyPfx);
Assert.Equal(0, collection.Count);
}
示例6: 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() ;
}
示例7: 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);
}
}
示例8: 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);
}
}
}
示例9: X509Certificate2CollectionThrowsArgumentNullException
public static void X509Certificate2CollectionThrowsArgumentNullException()
{
using (X509Certificate2 certificate = new X509Certificate2())
{
Assert.Throws<ArgumentNullException>(() => new X509Certificate2Collection((X509Certificate2[])null));
Assert.Throws<ArgumentNullException>(() => new X509Certificate2Collection((X509Certificate2Collection)null));
X509Certificate2Collection collection = new X509Certificate2Collection { certificate };
Assert.Throws<ArgumentNullException>(() => collection[0] = null);
Assert.Throws<ArgumentNullException>(() => collection.Add((X509Certificate)null));
Assert.Throws<ArgumentNullException>(() => collection.Add((X509Certificate2)null));
Assert.Throws<ArgumentNullException>(() => collection.AddRange((X509Certificate[])null));
Assert.Throws<ArgumentNullException>(() => collection.AddRange((X509CertificateCollection)null));
Assert.Throws<ArgumentNullException>(() => collection.AddRange((X509Certificate2[])null));
Assert.Throws<ArgumentNullException>(() => collection.AddRange((X509Certificate2Collection)null));
Assert.Throws<ArgumentNullException>(() => collection.CopyTo(null, 0));
Assert.Throws<ArgumentNullException>(() => collection.Insert(0, (X509Certificate)null));
Assert.Throws<ArgumentNullException>(() => collection.Insert(0, (X509Certificate2)null));
Assert.Throws<ArgumentNullException>(() => collection.Remove((X509Certificate)null));
Assert.Throws<ArgumentNullException>(() => collection.Remove((X509Certificate2)null));
Assert.Throws<ArgumentNullException>(() => collection.RemoveRange((X509Certificate2[])null));
Assert.Throws<ArgumentNullException>(() => collection.RemoveRange((X509Certificate2Collection)null));
Assert.Throws<ArgumentNullException>(() => collection.Import((byte[])null));
Assert.Throws<ArgumentNullException>(() => collection.Import((string)null));
IList ilist = (IList)collection;
Assert.Throws<ArgumentNullException>(() => ilist[0] = null);
Assert.Throws<ArgumentNullException>(() => ilist.Add(null));
Assert.Throws<ArgumentNullException>(() => ilist.CopyTo(null, 0));
Assert.Throws<ArgumentNullException>(() => ilist.Insert(0, null));
Assert.Throws<ArgumentNullException>(() => ilist.Remove(null));
}
}
示例10: 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>());
}
}
示例11: 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));
}
}
}
示例12: ImportNull
public static void ImportNull()
{
X509Certificate2Collection cc2 = new X509Certificate2Collection();
Assert.Throws<ArgumentNullException>(() => cc2.Import((byte[])null));
Assert.Throws<ArgumentNullException>(() => cc2.Import((String)null));
}
示例13: 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);
}
示例14: ImportPkcs7DerFile_Chain
public static void ImportPkcs7DerFile_Chain()
{
var collection = new X509Certificate2Collection();
collection.Import(Path.Combine("TestData", "certchain.p7b"));
Assert.Equal(3, collection.Count);
}
示例15: ImportPkcs12Bytes_Single
public static void ImportPkcs12Bytes_Single()
{
X509Certificate2Collection cc2 = new X509Certificate2Collection();
cc2.Import(TestData.PfxData, TestData.PfxDataPassword, X509KeyStorageFlags.DefaultKeySet);
int count = cc2.Count;
Assert.Equal(1, count);
}