public CertificateAuthorityRequestResponse Sign(CertificateRequest csr, string templateName, KeyUsage keyusage = KeyUsage.ServerAuthentication) { ICertRequest3 objCertRequest = (ICertRequest3)Activator.CreateInstance(Type.GetTypeFromProgID("CertificateAuthority.Request")); string templateArg = $"CertificateTemplate:{templateName}"; string serverArg = string.Concat(ServerName, @"\", CommonName); int requestStatus = 0x0; try { if (AuthenticationType == MicrosoftCertificateAuthorityAuthenticationType.UsernamePassword) { using (WindowsImpersonation context = new WindowsImpersonation(username, authRealm, password)) { requestStatus = objCertRequest.Submit((CR_IN_BASE64 | CR_IN_FORMATANY), csr.EncodedCsr, templateArg, serverArg); } } else { requestStatus = objCertRequest.Submit((CR_IN_BASE64 | CR_IN_FORMATANY), csr.EncodedCsr, templateArg, serverArg); } } catch (Exception e) { if (objCertRequest.GetRequestId() == 0) { throw new CertificateAuthorityUnavailibleException("Service error while attempting to sign certificate"); } requestStatus = 0x6; } switch (requestStatus) { case CR_DISP_ISSUED: return(new CertificateAuthorityRequestResponse(objCertRequest.GetRequestId(), CertificateRequestStatus.Issued, CommonName, objCertRequest.GetCertificate(CR_OUT_BASE64))); case CR_DISP_UNDER_SUBMISSION: return(new CertificateAuthorityRequestResponse(objCertRequest.GetRequestId(), CertificateRequestStatus.Pending, CommonName)); case CR_DISP_DENIED: return(new CertificateAuthorityRequestResponse(CertificateRequestStatus.Denied, CommonName)); case CR_DISP_ERROR: return(new CertificateAuthorityRequestResponse(CertificateRequestStatus.Error, CommonName)); default: return(new CertificateAuthorityRequestResponse(CertificateRequestStatus.Error, CommonName)); } }
private int SubmitRequestWindowsAuthentication(CertificateRequest csr, string templateName) { ICertRequest3 objCertRequest = (ICertRequest3)Activator.CreateInstance(Type.GetTypeFromProgID("CertificateAuthority.Request")); string templateArg = $"CertificateTemplate:{templateName}"; string serverArg = String.Format(@"{0}\{1}", ServerName, CommonName); return(objCertRequest.Submit((CR_IN_BASE64 | CR_IN_FORMATANY), csr.EncodedCsr, templateArg, serverArg)); }