Senden einer Zertifikatanforderung und Installieren der Zertifikatantwort (Windows-Runtime-Apps mit JavaScript)

[ Dieser Artikel richtet sich an Windows 8.x- und Windows Phone 8.x-Entwickler, die Windows-Runtime-Apps schreiben. Wenn Sie für Windows 10 entwickeln, finden Sie weitere Informationen unter neueste Dokumentation]

Im folgenden Beispiel wird gezeigt, wie Sie eine Zertifikatanforderung an eine Zertifizierungsstelle senden, die Microsoft-Zertifikatdienste ausführt, und das ausgestellte Zertifikat installieren.

Hinweis  Der myRequest-Wert ist eine Zeichenfolge, die von dem unter Erstellen einer Zertifikatanforderung beschriebenen Beispiel erstellt wird.

 


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);
}

Verwandte Themen

Erstellen einer Zertifikatanforderung

Arbeiten mit Zertifikaten