Partager via


Envoi d’une demande de certificat et installation de la réponse de certificat (application Windows Runtime en JavaScript)

[ Cet article est destiné aux développeurs de Windows 8.x et Windows Phone 8.x qui créent des applications Windows Runtime. Si vous développez une application pour Windows 10, voir la Documentation ]

L’exemple suivant montre comment soumettre une demande de certificat à une autorité de certification exécutant les services de certificats Microsoft et comment installer le certificat émis.

Remarque  La valeur myRequest est une chaîne créée par l’exemple abordé dans la rubrique Création d’une demande de certificat.

 


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 = "https://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);
}

Rubriques associées

Création d’une demande de certificat

Utilisation de certificats