Skip to content
This repository was archived by the owner on Apr 23, 2025. It is now read-only.

Latest commit

 

History

History
39 lines (29 loc) · 1.53 KB

File metadata and controls

39 lines (29 loc) · 1.53 KB
page_type urlFragment languages products description
sample
TPMdevicetest
windows
windows-iot
windows-uwp
Connect TPM with the Azure IoT Hub with Windows 10 IoT Core.

Connecting TPM with the Azure IoT Hub

To connect to the Azure IoT Hub from a provisioned device, use the TpmDevice class from the Microsoft.Devices.Tpm library (available as the NuGet package). Get the device information stored in the desired slot (typically slot 0), then retrieve the name of the IoT Hub, the device ID, and the SAS token (the string containing the HMAC produced from the shared access key) and use that to create the DeviceClient:

TpmDevice myDevice = new TpmDevice(0); // Use TPM slot 0
string hubUri = myDevice.GetHostName();
string deviceId = myDevice.GetDeviceId();
string sasToken = myDevice.GetSASToken();

var deviceClient = DeviceClient.Create(
    hubUri,
    AuthenticationMethodFactory.
        CreateAuthenticationWithToken(deviceId, sasToken), TransportType.Amqp);

var str = "Hello, Cloud!";
var message = new Message(Encoding.SCII.GetBytes(str));

await deviceClient.SendEventAsync(message);

At this point, you have a connected deviceClient object that you can use to send and receive messages. You can view the full working sample here.

To learn more about building secure apps for Windows IoT Core, you can view the blog post here.