author | ms.author | ms.service | ms.topic | ms.date |
---|---|---|---|---|
dominicbetts |
dobett |
iot-develop |
include |
12/17/2021 |
To modify the sample code to use the X.509 certificates:
-
Navigate to the azure-iot-sdk-node/device/samples/javascript folder that contains the pnp_temperature_controller.js application and run the following command to install the X.509 package:
npm install azure-iot-security-x509 --save
-
Open the pnp_temperature_controller.js file in a text editor.
-
Edit the
require
statements to include the following code:const fs = require('fs'); const X509Security = require('azure-iot-security-x509').X509Security;
-
Add the following four lines to the "DPS connection information" section to initialize the
deviceCert
variable:const deviceCert = { cert: fs.readFileSync(process.env.IOTHUB_DEVICE_X509_CERT).toString(), key: fs.readFileSync(process.env.IOTHUB_DEVICE_X509_KEY).toString() };
-
Edit the
provisionDevice
function that creates the client by replacing the first line with the following code:var provSecurityClient = new X509Security(registrationId, deviceCert);
-
In the same function, modify the line that sets the
deviceConnectionString
variable as follows:deviceConnectionString = 'HostName=' + result.assignedHub + ';DeviceId=' + result.deviceId + ';x509=true';
-
In the
main
function, add the following line after the line that callsClient.fromConnectionString
:client.setOptions(deviceCert);
Save the changes.