コード例 #1
0
 private static void RemoveCertificateFromStore(System.Security.Cryptography.SafeCertStoreHandle safeCertStoreHandle, System.Security.Cryptography.SafeCertContextHandle safeCertContext)
 {
     if ((safeCertContext != null) && !safeCertContext.IsInvalid)
     {
         if (((safeCertStoreHandle == null) || safeCertStoreHandle.IsInvalid) || safeCertStoreHandle.IsClosed)
         {
             throw new CryptographicException(SR.GetString("Cryptography_X509_StoreNotOpen"));
         }
         System.Security.Cryptography.SafeCertContextHandle handle = CAPI.CertFindCertificateInStore(safeCertStoreHandle, 0x10001, 0, 0xd0000, safeCertContext.DangerousGetHandle(), System.Security.Cryptography.SafeCertContextHandle.InvalidHandle);
         if ((handle != null) && !handle.IsInvalid)
         {
             GC.SuppressFinalize(handle);
             if (!CAPI.CertDeleteCertificateFromStore(handle))
             {
                 throw new CryptographicException(Marshal.GetLastWin32Error());
             }
         }
     }
 }
コード例 #2
0
        private static void RemoveCertificateFromStore(Cryptography.SafeCertStoreHandle safeCertStoreHandle, Cryptography.SafeCertContextHandle safeCertContext)
        {
            if (safeCertContext == null || safeCertContext.IsInvalid)
            {
                return;
            }

            if (safeCertStoreHandle == null || safeCertStoreHandle.IsInvalid || safeCertStoreHandle.IsClosed)
            {
                throw new CryptographicException(SR.GetString(SR.Cryptography_X509_StoreNotOpen));
            }

            // Find the certificate in the store.
            Cryptography.SafeCertContextHandle safeCertContext2 = CAPI.CertFindCertificateInStore(safeCertStoreHandle,
                                                                                                  CAPI.X509_ASN_ENCODING | CAPI.PKCS_7_ASN_ENCODING,
                                                                                                  0,
                                                                                                  CAPI.CERT_FIND_EXISTING,
                                                                                                  safeCertContext.DangerousGetHandle(),
                                                                                                  Cryptography.SafeCertContextHandle.InvalidHandle);

            // The certificate is not present in the store, simply return.
            if (safeCertContext2 == null || safeCertContext2.IsInvalid)
            {
                return;
            }

            // CertDeleteCertificateFromStore always releases the context regardless of success
            // or failure so we don't need to manually release it
            GC.SuppressFinalize(safeCertContext2);

            // Remove from the store.
            if (!CAPI.CertDeleteCertificateFromStore(safeCertContext2))
            {
                throw new CryptographicException(Marshal.GetLastWin32Error());
            }
        }