Original URL: | https://blogs.msdn.microsoft.com/alejacma/2012/04/04/how-to-export-issued-certificates-from-a-ca-programatically-c/ |
Post name: | How to export issued certificates from a CA programatically (C#) |
Original author: | Alejandro Campos Magencio |
Posting date: | 2012-04-04T07:29:46+00:00 |
Hi all,
The following sample is a simplification of How to get info from client certificates issued by a CA (C#), and gets all the issued certs in the CA database and copies them to a folder:
using System; using System.Windows.Forms; using System.IO; using CERTADMINLib; … // Parameters string strServer = "myserver"; string strCAName = "myserver-CA"; string strPathForCerts = "c:\\test\\"; // Constants const int CV_OUT_BASE64HEADER = 0; const int CV_OUT_BINARY = 2; // Variables CERTADMINLib.CCertView certView = null; CERTADMINLib.IEnumCERTVIEWROW certViewRow = null; CERTADMINLib.IEnumCERTVIEWCOLUMN certViewColumn = null; int iColumnCount = 0; object objValue = null; string strID = ""; StreamWriter objFile = null; // Connecting to the Certificate Authority certView = new CERTADMINLib.CCertView(); certView.OpenConnection(strServer + "\\" + strCAName); // Get a column count and place columns into the view iColumnCount = certView.GetColumnCount(0); certView.SetResultColumnCount(iColumnCount); // Place each column in the view. for (int x = 0; x < iColumnCount; x++) { certView.SetResultColumn(x); } // Open the View and reset the row position certViewRow = certView.OpenView(); certViewRow.Reset(); // Enumerate Row and Column Information // Rows (one per cert) for (int x = 0; certViewRow.Next() != -1; x++) { // Columns with the info we need certViewColumn = certViewRow.EnumCertViewColumn(); while (certViewColumn.Next() != -1) { switch (certViewColumn.GetDisplayName()) { // Request ID case "Request ID": objValue = certViewColumn.GetValue(CV_OUT_BINARY); if (objValue != null) { strID = "Request ID " + objValue.ToString(); } break; // Binary Certificate case "Binary Certificate": objValue = certViewColumn.GetValue(CV_OUT_BASE64HEADER); if (objValue != null) { // Write certificate to file objFile = File.CreateText(strPathForCerts + strID + ".cer"); objFile.Write(objValue.ToString()); objFile.Close(); } break; default: break; } } } MessageBox.Show("We are done!\nCerts have been copied to " + strPathForCerts);
I hope this helps.
Regards,
Alex (Alejandro Campos Magencio)
Comments: