• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C# X509Certificate2Collection类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了C#中X509Certificate2Collection的典型用法代码示例。如果您正苦于以下问题:C# X509Certificate2Collection类的具体用法?C# X509Certificate2Collection怎么用?C# X509Certificate2Collection使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



X509Certificate2Collection类属于命名空间,在下文中一共展示了X509Certificate2Collection类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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));
                }
            }
        }
开发者ID:er0dr1guez,项目名称:corefx,代码行数:29,代码来源:CollectionTests.cs


示例2: ImportPkcs7PemBytes_Empty

        public static void ImportPkcs7PemBytes_Empty()
        {
            var collection = new X509Certificate2Collection();
            collection.Import(TestData.Pkcs7EmptyPemBytes);

            Assert.Equal(0, collection.Count);
        }
开发者ID:er0dr1guez,项目名称:corefx,代码行数:7,代码来源:CollectionImportTests.cs


示例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);
            }
        }
开发者ID:jmhardison,项目名称:corefx,代码行数:32,代码来源:CollectionTests.cs


示例4: ImportX509PemBytes

        public static void ImportX509PemBytes()
        {
            var collection = new X509Certificate2Collection();
            collection.Import(TestData.MsCertificatePemBytes);

            Assert.Equal(1, collection.Count);
        }
开发者ID:er0dr1guez,项目名称:corefx,代码行数:7,代码来源:CollectionImportTests.cs


示例5: ImportX509PemFile

        public static void ImportX509PemFile()
        {
            var collection = new X509Certificate2Collection();
            collection.Import(Path.Combine("TestData", "MS.pem"));

            Assert.Equal(1, collection.Count);
        }
开发者ID:er0dr1guez,项目名称:corefx,代码行数:7,代码来源:CollectionImportTests.cs


示例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;
 }
开发者ID:ESgarbi,项目名称:corefx,代码行数:7,代码来源:Cert.cs


示例7: ExportEmpty_Cert

        public static void ExportEmpty_Cert()
        {
            var collection = new X509Certificate2Collection();
            byte[] exported = collection.Export(X509ContentType.Cert);

            Assert.Null(exported);
        }
开发者ID:dotnet,项目名称:corefx,代码行数:7,代码来源:CollectionTests.cs


示例8: ImportEmpty_Pkcs12

        public static void ImportEmpty_Pkcs12()
        {
            var collection = new X509Certificate2Collection();

            collection.Import(TestData.EmptyPfx);

            Assert.Equal(0, collection.Count);
        }
开发者ID:er0dr1guez,项目名称:corefx,代码行数:8,代码来源:CollectionImportTests.cs


示例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);
        }
开发者ID:dotnet,项目名称:corefx,代码行数:8,代码来源:CollectionTests.cs


示例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();
                }
            }
        }
开发者ID:dotnet,项目名称:corefx,代码行数:58,代码来源:CollectionTests.cs


示例11: AddDoesNotClone

        public static void AddDoesNotClone()
        {
            using (X509Certificate2 c1 = new X509Certificate2())
            {
                X509Certificate2Collection coll = new X509Certificate2Collection();
                coll.Add(c1);

                Assert.Same(c1, coll[0]);
            }
        }
开发者ID:dotnet,项目名称:corefx,代码行数:10,代码来源:CollectionTests.cs


示例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() ; 
		}
开发者ID:aura1213,项目名称:netmf-interpreter,代码行数:12,代码来源:storebug.cs


示例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);
        }
开发者ID:er0dr1guez,项目名称:corefx,代码行数:12,代码来源:CollectionTests.cs


示例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);
            }
        }
开发者ID:er0dr1guez,项目名称:corefx,代码行数:18,代码来源:ChainTests.cs


示例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);
        }
开发者ID:er0dr1guez,项目名称:corefx,代码行数:9,代码来源:CollectionTests.cs


示例16: 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);
            }
        }
开发者ID:er0dr1guez,项目名称:corefx,代码行数:25,代码来源:CollectionTests.cs


示例17: ImportMultiplePrivateKeysPfx

        public static void ImportMultiplePrivateKeysPfx()
        {
            var collection = new X509Certificate2Collection();
            collection.Import(TestData.MultiPrivateKeyPfx);

            Assert.Equal(2, collection.Count);

            foreach (X509Certificate2 cert in collection)
            {
                Assert.True(cert.HasPrivateKey, "cert.HasPrivateKey");
            }
        }
开发者ID:er0dr1guez,项目名称:corefx,代码行数:12,代码来源:CollectionTests.cs


示例18: ImportFromFileTests

        public static void ImportFromFileTests()
        {
            using (var pfxCer = new X509Certificate2(TestData.PfxData, TestData.PfxDataPassword))
            {
                X509Certificate2Collection cc2 = new X509Certificate2Collection();
                cc2.Import(Path.Combine("TestData" ,"My.pfx"), TestData.PfxDataPassword, X509KeyStorageFlags.DefaultKeySet);
                int count = cc2.Count;
                Assert.Equal(1, count);

                using (X509Certificate2 c = cc2[0])
                {
                    // pfxCer was loaded directly, cc2[0] was Imported, two distinct copies.
                    Assert.NotSame(pfxCer, c);

                    Assert.Equal(pfxCer, c);
                    Assert.Equal(pfxCer.Thumbprint, c.Thumbprint);
                }
            }
        }
开发者ID:er0dr1guez,项目名称:corefx,代码行数:19,代码来源:CollectionTests.cs


示例19: ImportStoreSavedAsPfxData

        public static void ImportStoreSavedAsPfxData()
        {
            using (var msCer = new X509Certificate2(TestData.MsCertificate))
            using (var pfxCer = new X509Certificate2(TestData.PfxData, TestData.PfxDataPassword))
            {
                X509Certificate2Collection cc2 = new X509Certificate2Collection();
                cc2.Import(TestData.StoreSavedAsPfxData);
                int count = cc2.Count;
                Assert.Equal(2, count);

                X509Certificate2[] cs = cc2.ToArray().OrderBy(c => c.Subject).ToArray();
                Assert.NotSame(msCer, cs[0]);
                Assert.Equal(msCer, cs[0]);
                Assert.Equal(msCer.Thumbprint, cs[0].Thumbprint);

                Assert.NotSame(pfxCer, cs[1]);
                Assert.Equal(pfxCer, cs[1]);
                Assert.Equal(pfxCer.Thumbprint, cs[1].Thumbprint);
            }
        }
开发者ID:er0dr1guez,项目名称:corefx,代码行数:20,代码来源:CollectionTests.cs


示例20: AssertEqualUnordered

        private static void AssertEqualUnordered(
           X509Certificate2Collection collection,
           X509Certificate2Collection importedCollection)
        {
            // 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];
            X509Certificate2[] importedCertificates = new X509Certificate2[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);
                importedCertificates[i] = importedCollection[i];
            }

            // The best error message would come from a mis-matched subject
            foreach (string subject in subjects)
            {
                Assert.Contains(subject, importedSubjects);
            }

            // But, really, the collections should be equivalent
            foreach (X509Certificate2 expectedCert in collection)
            {
                Assert.Contains(expectedCert, importedCertificates);
            }
        }
开发者ID:er0dr1guez,项目名称:corefx,代码行数:32,代码来源:CollectionTests.cs



注:本文中的X509Certificate2Collection类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C# X509CertificateCollection类代码示例发布时间:2022-05-24
下一篇:
C# X509Certificate2类代码示例发布时间:2022-05-24
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap