인증서 요청 제출 및 인증서 응답 설치(JavaScript를 사용하는 Windows 런타임 앱)

[ 이 문서는 Windows 런타임 앱을 작성하는 Windows에서 8.x 및 Windows Phone 8.x 개발자를 대상으로 합니다. Windows 10용으로 개발하는 경우에는 최신 설명서를 참조하세요.]

다음 예제에서는 Microsoft 인증서 서비스를 운영하는 인증 기관에 인증서 요청을 제출하는 방법과 발급된 인증서를 설치하는 방법을 보여 줍니다.

참고  myRequest 값은 인증서 요청 만들기 항목에서 설명한 예제로 만든 문자열입니다.

 


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

관련 항목

인증서 요청 만들기

인증서 작업