Use .Export (), then Convert.ToBase64String () and save as NVARCHAR (MAX)
To save it:
var cert = new X509Certificate2(filename); var stringOfCertWithPrivateKey = Convert.ToBase64String(cert.Export(X509ContentType.Pkcs12));
Then simply restore (after returning from the database) with:
var certBytes = Convert.FromBase64String(stringOfCertWithPrivateKey); var cert = new X509Certificate2(certBytes);
Using Export () is better than .RawData, since you can choose whether to keep the private key or not (using .RawData will always deprive it).
jezpez
source share