c# - Prevent file creation when X509Certificate2 is created? -


we create x509certificate2 object in our asp.net app make periodic outgoing connections. every time 1 of these certificates created new file created in:

c:\programdata\microsoft\crypto\rsa\machinekeys

that folder has 4 million files never cleaned up. i've tried removing persist flag

new x509certificate2(certbytes, p12pwd, x509keystorageflags.machinekeyset);

//no x509keystorageflags.persistkeyset

but doesn't -- still 2kb file on every call.

i got hopes when saw this answer, 2008 r2 server, , temp files not 0 bytes, seems different case.

how can use x509certificate2 without filling disk?

to reproduce problem have created sample code. testing environment windows 8.1 64bit , application written in .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";              (var = 0; < 1000; i++)             {                 var cert = new x509certificate2(certbytes, p12pwd, x509keystorageflags.machinekeyset);                  // line helped keep filesize growing                    // cert.reset();              }         }     } } 

i shocked file size of c:\programdata\microsoft\crypto\rsa\machinekeys went 2mb. application exited , filesize dropped down 20k (that starting size).

then have added cert.reset(); (i have commented in code above). should called when no longer need x509certificate2 instance. after that, filesize of c:\programdata\microsoft\crypto\rsa\machinekeys flapping between 20k , 22k.

so suggestion call cert.reset(); when no longer need x509certificate2 instance.


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -