当前位置: 首页>>代码示例>>C#>>正文


C# X509Certificate2.Dispose方法代码示例

本文整理汇总了C#中X509Certificate2.Dispose方法的典型用法代码示例。如果您正苦于以下问题:C# X509Certificate2.Dispose方法的具体用法?C# X509Certificate2.Dispose怎么用?C# X509Certificate2.Dispose使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在X509Certificate2的用法示例。


在下文中一共展示了X509Certificate2.Dispose方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: TestByteArrayConstructor

        public static void TestByteArrayConstructor()
        {
            byte[] expectedThumbPrint = new byte[]
            {
                0x10, 0x8e, 0x2b, 0xa2, 0x36, 0x32, 0x62, 0x0c,
                0x42, 0x7c, 0x57, 0x0b, 0x6d, 0x9d, 0xb5, 0x1a,
                0xc3, 0x13, 0x87, 0xfe,
            };

            using (X509Certificate2 c = new X509Certificate2(TestData.MsCertificate))
            {
                IntPtr h = c.Handle;
                object ignored;
                Assert.NotEqual(IntPtr.Zero, h);
                byte[] actualThumbprint = c.GetCertHash();
                Assert.Equal(expectedThumbPrint, actualThumbprint);

                c.Dispose();

                // For compat reasons, Dispose() acts like the now-defunct Reset() method rather than causing ObjectDisposedExceptions.
                h = c.Handle;
                Assert.Equal(IntPtr.Zero, h);
                Assert.Throws<CryptographicException>(() => c.GetCertHash());
                Assert.Throws<CryptographicException>(() => c.GetKeyAlgorithm());
                Assert.Throws<CryptographicException>(() => c.GetKeyAlgorithmParameters());
                Assert.Throws<CryptographicException>(() => c.GetKeyAlgorithmParametersString());
                Assert.Throws<CryptographicException>(() => c.GetPublicKey());
                Assert.Throws<CryptographicException>(() => c.GetSerialNumber());
                Assert.Throws<CryptographicException>(() => ignored = c.Issuer);
                Assert.Throws<CryptographicException>(() => ignored = c.Subject);
            }
        }
开发者ID:johnhhm,项目名称:corefx,代码行数:32,代码来源:CtorTests.cs

示例2: UseAfterDispose

        public static void UseAfterDispose()
        {
            using (X509Certificate2 c = new X509Certificate2(TestData.MsCertificate))
            {
                IntPtr h = c.Handle;

                // Do a couple of things that would only be true on a valid certificate, as a precondition.
                Assert.NotEqual(IntPtr.Zero, h);
                byte[] actualThumbprint = c.GetCertHash();

                c.Dispose();

                // For compat reasons, Dispose() acts like the now-defunct Reset() method rather than
                // causing ObjectDisposedExceptions.
                h = c.Handle;
                Assert.Equal(IntPtr.Zero, h);

                // State held on X509Certificate
                Assert.ThrowsAny<CryptographicException>(() => c.GetCertHash());
                Assert.ThrowsAny<CryptographicException>(() => c.GetKeyAlgorithm());
                Assert.ThrowsAny<CryptographicException>(() => c.GetKeyAlgorithmParameters());
                Assert.ThrowsAny<CryptographicException>(() => c.GetKeyAlgorithmParametersString());
                Assert.ThrowsAny<CryptographicException>(() => c.GetPublicKey());
                Assert.ThrowsAny<CryptographicException>(() => c.GetSerialNumber());
                Assert.ThrowsAny<CryptographicException>(() => c.Issuer);
                Assert.ThrowsAny<CryptographicException>(() => c.Subject);
                Assert.ThrowsAny<CryptographicException>(() => c.NotBefore);
                Assert.ThrowsAny<CryptographicException>(() => c.NotAfter);

                // State held on X509Certificate2
                Assert.ThrowsAny<CryptographicException>(() => c.RawData);
                Assert.ThrowsAny<CryptographicException>(() => c.SignatureAlgorithm);
                Assert.ThrowsAny<CryptographicException>(() => c.Version);
                Assert.ThrowsAny<CryptographicException>(() => c.SubjectName);
                Assert.ThrowsAny<CryptographicException>(() => c.IssuerName);
                Assert.ThrowsAny<CryptographicException>(() => c.PublicKey);
                Assert.ThrowsAny<CryptographicException>(() => c.Extensions);
            #if netstandard17
                Assert.ThrowsAny<CryptographicException>(() => c.PrivateKey);
            #endif
            }
        }
开发者ID:dotnet,项目名称:corefx,代码行数:42,代码来源:CertTests.cs

示例3: TestCopyConstructor_Lifetime_Cloned_Reversed

        public static void TestCopyConstructor_Lifetime_Cloned_Reversed()
        {
            var c1 = new X509Certificate2(TestData.PfxData, TestData.PfxDataPassword);
            var c2 = new X509Certificate2(c1);
            TestPrivateKey(c1, true);
            TestPrivateKey(c2, true);

            c2.Dispose();
            GC.Collect();
            GC.WaitForPendingFinalizers();

            TestPrivateKey(c1, true);
            TestPrivateKey(c2, false);

            c1.Dispose();
            GC.Collect();
            GC.WaitForPendingFinalizers();

            TestPrivateKey(c1, false);
        }
开发者ID:chcosta,项目名称:corefx,代码行数:20,代码来源:CtorTests.cs

示例4: TestCopyConstructor_Lifetime_Independent

        public static void TestCopyConstructor_Lifetime_Independent()
        {
            var c1 = new X509Certificate2(TestData.PfxData, TestData.PfxDataPassword);
            using (var c2 = new X509Certificate2(TestData.PfxData, TestData.PfxDataPassword))
            {
                RSA rsa = c2.GetRSAPrivateKey();
                byte[] hash = new byte[20];
                byte[] sig = rsa.SignHash(hash, HashAlgorithmName.SHA1, RSASignaturePadding.Pkcs1);
                Assert.Equal(TestData.PfxSha1Empty_ExpectedSig, sig);

                c1.Dispose();
                rsa.Dispose();

                GC.Collect();
                GC.WaitForPendingFinalizers();

                // Verify other cert and previous key do not affect cert
                using (rsa = c2.GetRSAPrivateKey())
                {
                    hash = new byte[20];
                    sig = rsa.SignHash(hash, HashAlgorithmName.SHA1, RSASignaturePadding.Pkcs1);
                    Assert.Equal(TestData.PfxSha1Empty_ExpectedSig, sig);
                }
            }
        }
开发者ID:chcosta,项目名称:corefx,代码行数:25,代码来源:CtorTests.cs

示例5: TestCopyConstructor_Lifetime_Cloned

        public static void TestCopyConstructor_Lifetime_Cloned()
        {
            var c1 = new X509Certificate2(TestData.PfxData, TestData.PfxDataPassword);
            var c2 = new X509Certificate2(c1);
            TestPrivateKey(c1, true);
            TestPrivateKey(c2, true);

            c1.Dispose();
            TestPrivateKey(c1, false);
            TestPrivateKey(c2, true);

            c2.Dispose();
            TestPrivateKey(c2, false);
        }
开发者ID:alessandromontividiu03,项目名称:corefx,代码行数:14,代码来源:CtorTests.cs

示例6: UseAfterDispose

        public static void UseAfterDispose()
        {
            using (X509Certificate2 c = new X509Certificate2(TestData.MsCertificate))
            {
                IntPtr h = c.Handle;

                // Do a couple of things that would only be true on a valid certificate, as a precondition.
                Assert.NotEqual(IntPtr.Zero, h);
                byte[] actualThumbprint = c.GetCertHash();

                c.Dispose();

                // For compat reasons, Dispose() acts like the now-defunct Reset() method rather than
                // causing ObjectDisposedExceptions.
                h = c.Handle;
                Assert.Equal(IntPtr.Zero, h);
                Assert.Throws<CryptographicException>(() => c.GetCertHash());
                Assert.Throws<CryptographicException>(() => c.GetKeyAlgorithm());
                Assert.Throws<CryptographicException>(() => c.GetKeyAlgorithmParameters());
                Assert.Throws<CryptographicException>(() => c.GetKeyAlgorithmParametersString());
                Assert.Throws<CryptographicException>(() => c.GetPublicKey());
                Assert.Throws<CryptographicException>(() => c.GetSerialNumber());
                Assert.Throws<CryptographicException>(() => c.Issuer);
                Assert.Throws<CryptographicException>(() => c.Subject);
            }
        }
开发者ID:jmhardison,项目名称:corefx,代码行数:26,代码来源:CertTests.cs


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