Search code examples
c#x509certificate

Store an X509Certificate2 in DB


Is it possible to store an X509Certificate2 in a SQL Server table rather than pull a .p12 file from the file system? I'm sure you can but not sure how to go about this.


Solution

  • This is definitely possible, the X509Certificate2 has a RawData property that can be saved into your SQL Database. To reconstruct the certificate you can use this constructor

    var cert = new X509Certificate2(filename);
    var data = cert.RawData;
    
    // save data to database...
    
    // Fetch data from database...
    
    cert = new X509Certificate2(data);