Skip to content

Files

Latest commit

 

History

History
53 lines (38 loc) · 1.64 KB

iot-central-x509-javascript-code.md

File metadata and controls

53 lines (38 loc) · 1.64 KB
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:

  1. 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
    
  2. Open the pnp_temperature_controller.js file in a text editor.

  3. Edit the require statements to include the following code:

    const fs = require('fs');
    const X509Security = require('azure-iot-security-x509').X509Security;
  4. 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()
    };
  5. Edit the provisionDevice function that creates the client by replacing the first line with the following code:

    var provSecurityClient = new X509Security(registrationId, deviceCert);
  6. In the same function, modify the line that sets the deviceConnectionString variable as follows:

    deviceConnectionString = 'HostName=' + result.assignedHub + ';DeviceId=' + result.deviceId + ';x509=true';
  7. In the main function, add the following line after the line that calls Client.fromConnectionString:

    client.setOptions(deviceCert);

    Save the changes.