private X509Certificate2 EnrollCertificate(String subject, String password, Object certificateRequest)
{
// Final enrollment process
//CX509Enrollment lEnroll = new CX509Enrollment();
Type lEnrollmentType = Type.GetTypeFromProgID("X509Enrollment.CX509Enrollment");
Object lEnroll = Activator.CreateInstance(lEnrollmentType);
// Load the certificate
//lEnroll.InitializeFromRequest(lRequestCertificate);
lEnrollmentType.InvokeMember("InitializeFromRequest", BindingFlags.InvokeMethod, null, lEnroll, new Object[] { certificateRequest });
// Add a friendly name
//lEnroll.CertificateFriendlyName = subject;
lEnrollmentType.InvokeMember("CertificateFriendlyName", BindingFlags.SetProperty, null, lEnroll, new Object[] { subject });
// Output the request in base64 and install it back as the response
//String csr = lEnroll.CreateRequest();
//lEnroll.InstallResponse(InstallResponseRestrictionFlags.XCN_ALLOW_UNTRUSTED, csr, EncodingType.XCN_CRYPT_STRING_BASE64, password);
Object csr = lEnrollmentType.InvokeMember("CreateRequest", BindingFlags.InvokeMethod, null, lEnroll, new Object[] { });
lEnrollmentType.InvokeMember("InstallResponse", BindingFlags.InvokeMethod, null, lEnroll,
new Object[] { XCN_ALLOW_UNTRUSTED, csr, XCN_CRYPT_STRING_BASE64, password });
// Output a base64 encoded PKCS#12 so we can import it back to the .Net security classes
//String lCertificateBase64encoded = lEnroll.CreatePFX(password, PFXExportOptions.XCN_EXPORT_WITH_ROOT);
String lCertificateBase64encoded = (String)lEnrollmentType.InvokeMember("CreatePFX", BindingFlags.InvokeMethod, null, lEnroll,
new Object[] { password, XCN_EXPORT_WITH_ROOT });
// Instantiate the certificate
return new X509Certificate2(Convert.FromBase64String(lCertificateBase64encoded), password, X509KeyStorageFlags.Exportable);
}