protected override bool ReleaseHandle() { // Make sure not to delete a key that we want to keep in the key container or an ephemeral key if (!_fPersistKeyInCsp && 0 == (_flags & (uint)CapiHelper.CryptAcquireContextFlags.CRYPT_VERIFYCONTEXT)) { // Delete the key container. uint flags = (_flags & (uint)CapiHelper.CryptAcquireContextFlags.CRYPT_MACHINE_KEYSET) | (uint)CapiHelper.CryptAcquireContextFlags.CRYPT_DELETEKEYSET; SafeProvHandle hIgnoredProv; bool ignoredSuccess = CapiHelper.CryptAcquireContext(out hIgnoredProv, _containerName, _providerName, _type, flags); // Ignoring success result code as CryptAcquireContext is being called to delete a key container rather than acquire a context. // If it fails, we can't do anything about it anyway as we're in a dispose method. } bool successfullyFreed = CapiHelper.CryptReleaseContext(handle, 0); Debug.Assert(successfullyFreed); SetHandle(IntPtr.Zero); return(successfullyFreed); }