証明書要求の送信と証明書応答のインストール (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);
}