创建X509Certificate2时禁止创建文件

本文关键字:创建 文件 禁止 X509Certificate2 | 更新日期: 2023-09-27 17:52:41

我们在ASP中创建一个X509Certificate2对象。NET应用程序进行定期的传出连接。每次创建这些证书中的一个时,都会在以下目录中创建一个新文件:

C: ' ProgramData '微软' RSA加密' ' MachineKeys

那个文件夹现在有400万个文件没有被清理。我已经试过移除持久化标志

new X509Certificate2(certBytes, p12Pwd,X509KeyStorageFlags.MachineKeySet);

//不X509KeyStorageFlags。PersistKeySet

但是这没有帮助——每次调用仍然得到2Kb的文件。

我得到了我的希望,当我看到这个答案,但这是一个2008 R2服务器,和临时文件不是0字节,所以它似乎是一个不同的情况。

如何在不填满磁盘的情况下使用X509Certificate2 ?

创建X509Certificate2时禁止创建文件

使用。net 4.6:

X509Certificate2实现了IDisposable接口,从.NET Framework 4.6;在以前版本的。net框架中,X509Certificate2类没有实现这个接口,并且因此Dispose方法不存在。

我在我们的服务器上有同样的问题。目前我发现最好的方法是从代码中删除文件。

using System;
using System.Security.Cryptography;
using System.Security.Permissions;
using System.IO;
using System.Security.Cryptography.X509Certificates;
namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            Byte[] bytes = File.ReadAllBytes(@"D:'tmp'111111111111.p12");
            X509Certificate2 x509 = new X509Certificate2(bytes, "qwerty", X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable);
            var privateKey = x509.PrivateKey as RSACryptoServiceProvider;
            string uniqueKeyContainerName = privateKey.CspKeyContainerInfo.UniqueKeyContainerName;
            x509.Reset();
            File.Delete(string.Format(@"C:'ProgramData'Microsoft'Crypto'RSA'MachineKeys'{0}", uniqueKeyContainerName));
        }
    }
}

为了重现您的问题,我创建了这个示例代码。我的测试环境是Windows 8.1 64位,应用程序是用。net 4.5编写的。

using System.IO;
using System.Security.Cryptography.X509Certificates;
namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            var certBytes = File.ReadAllBytes(@"c:'cert.p12");
            var p12Pwd = "somepassword";
            for (var i = 0; i < 1000; i++)
            {
                var cert = new X509Certificate2(certBytes, p12Pwd, X509KeyStorageFlags.MachineKeySet);
                // this line helped keep filesize from growing   
                // cert.Reset(); 
            }
        }
    }
}

我很震惊,C:'ProgramData'Microsoft'Crypto'RSA'MachineKeys的文件大小上升到2MB。然后应用程序退出,文件大小下降到20K(这可能是它的初始大小)。

然后我添加了cert.Reset();(我在上面的代码中注释了它)。当您不再需要X509Certificate2实例时,应该调用该函数。之后,C:'ProgramData'Microsoft'Crypto'RSA'MachineKeys的文件大小在20K和22K之间波动。

所以我的建议是当你不再需要X509Certificate2实例时调用cert.Reset();

我在我们的服务器上有同样的问题。目前我发现最好的方法是通过脚本删除文件(除了有用的文件)。

例外列表:

  • Microsoft Internet Information Server -> c2319c42033a5ca7f44e731bfd3fa2b5…
  • NetFrameworkConfigurationKey -> d6d986f09a1ee04e24c949879fdb506c…
  • iisWasKey -> 76944fb33636aeddb9590521c2e8815a…
  • WMSvc证书密钥容器-> bedbf0b4da5f8061b6444baedf4c00b1…
  • iisConfigurationKey -> 6de9cb26d2b98c01ec4e9e8b34824aa2…
  • MS IIS DCOM Server -> 7a436fe806e483969f48a894af2fe9a1…
  • TSSecKeySet1 -> f686aace6942fb7f7ceb231212eef4a4…
  • https://forums.whirlpool.net.au/archive/1683713
通过https://forums.iis.net/t/1224708.aspx?C

+ ProgramData +微软+ RSA加密+ + MachineKeys +是+磁盘+空间+填充+

此解决方案仍然是补丁。最好不要在MachineKeys文件夹中生成文件