Submitting a certificate request and installing the certificate response Windows Runtime app using JavaScript

Applies to Windows and Windows Phone

The following example shows how to submit a certificate request to a certification authority running Microsoft certificate services and how to install the issued certificate.

Note  The myRequest value is a string created by the sample discussed in the Creating a certificate request topic.



function submitCertificateRequest( myRequest ) {

    var myMessage = "Submit certificate request to server ......";

    // Use the XMLHttpRequest object to communicate with a helper service which bridges communication with Certificate Services.
    var xmlHttpRequest = new XMLHttpRequest(); 

    // State change event handler.
    xmlHttpRequest.onreadystatechange = function() {
        var xmlResult;
        var certificate;
        var errorObj;
        var xmlElements;

        // All of the data has been received.
        if(xmlHttpRequest.readyState == 4) { 
            xmlResult = xmlHttpRequest.responseXML;

            // Parse the xml message from the service to retrieve the certificate.
            try {
                xmlElements = xmlResult.getElementsByTagName("SubmitRequestResult");
                if (1 != xmlElements.length) {
                    errorObj = new Error(0, "Server retunred more than 1 results");
                    throw errorObj;
                }
                else if (1 != xmlElements[0].childNodes.length) {
                    errorObj = new Error(0, "SubmitRequestResult element has more than 1 child nodes");
                    throw errorObj;
                }
                else if (3 != xmlElements[0].childNodes[0].nodeType) { //text node
                    errorObj = new Error(0, "SubmitRequestResult element's child node type " + xmlElements[0].childNodes[0].nodeType.toString() + " != 3");
                    throw errorObj;
                }
                else {
                    certificate = xmlElements[0].childNodes[0].nodeValue;
                }

                myMessage = myMessage + "\n\nReceived certificate from server, encoded certificate string =\n" + certificate;
                sdkSample.displayStatus(myMessage);
            }
            catch (ex1) {
                myMessage = myMessage + "\n\nCommunication with server failed. Error description = " + ex1.description;

                // use a custom function (not shown) to display the error.
                sdkSample.displayError(myMessage);
                return;
            }

            // Call the installCertificate method to install the certificate in the app container.
            try {
                Windows.Security.Cryptography.Certificates.CertificateEnrollmentManager.installCertificate(certificate);
                myMessage = myMessage + "\n\nCertificate installation succeeded. The certificate is in the app container certificate store";
                sdkSample.displayStatus(myMessage);
            }
            catch (ex2) {
                myMessage = myMessage + "\n\nCertificate installation failed.";
                myMessage = myMessage + convertErrortoString(ex2);
                sdkSample.displayError(myMessage);
            }
        }
    }

    var body = '<SubmitRequest xmlns="http://somename.org/"><strRequest>' + myRequest + '</strRequest></SubmitRequest>';
    var url = "http://servername/CrossMachineEnrollmentService/SubmitRequest";

    myRequest = "";

    // Send the request.
    xmlHttpRequest.open("POST", url, true);
    xmlHttpRequest.setRequestHeader("Content-type", "text/xml");
    xmlHttpRequest.send(body);

    // Use a custom function (not shown) to display progress.
    sdkSample.displayStatus(myMessage);
}

Related topics

Creating a certificate request
Working with certificates

 

 

Show:
© 2014 Microsoft