本文介绍:
本文介绍如下内容:
1. 使用OPENSSL和 微软提供的示例工具生成自签名证书;
2. 在IoT Hub中配置证书并验证所有权;
3. 模拟设备使用设备证书向IoT Hub 发送遥测消息;
视频讲解:
您可以在B站观看视频讲解:https://www.bilibili.com/video/av92977189/
或在本站观看:
图文讲解:
1. 使用OPENSSL和 微软提供的示例工具生成自签名证书
1.1 准备power shell 和openssl
示例代码:https://codeload.github.com/Azure/azure-iot-sdk-c/zip/master
以管理员权限使用pwoer shell进入 如下目录:
执行如下命令:
Set-ExecutionPolicy -ExecutionPolicy Unrestricted
输入A,如下图:
(注意,此命令在powershell 7 版本下可能无效,本文使用的是win10默认的powershell,版本为5.x,见下图:)
执行如下命令,注意两个“.”之间有一个空格:
. .\ca-certs.ps1
执行如下命令,检查环境配置是否正常,如果出错i,则可能是openssl环境变量配置不正确或者. .\ca-certs.ps1命令格式不争取,部分电脑可能需要重启。
1.2. 创建证书链
执行如下命令:
New-CACertsCertChain rsa
如下图所示,供创建4张证书,一张根证书和三证中间证书
2. 在IoT Hub中配置证书并验证所有权
点击证书,进行所有权验证步骤:
执行如下命令:
New-CACertsVerificationCert "portal 中生成的验证码"
选择新生成的证书,进行验证:
如下图,显示验证通过:
至此,证书所有权验证成功。
3. 模拟设备使用设备证书向IoT Hub 发送遥测消息
3.1 准备设备证书
执行如下命令,生成设备证书,其中mydevice001 表示证书名称,也就是最终注册到IoT Hub中的设备ID:
New-CACertsDevice mydevice
需要输入证书密码,此密码由用户定义,这里我们输入了 12345678
按照提示再次输入密码,即可如下图生成证书,我们在设备中使用的是 mydevice.pfx 证书文件。
3.2 模拟程序(C#)使用设备证书 IoT Hub发送数据
在IoT Hub创建证书:
注意:设备名称必须与证书名称一致
接下来步骤参考如下文档:
示例代码如下:
需要修改的地方有:
using System;
using Microsoft.Azure.Devices.Client;
using Microsoft.Azure.Devices.Shared;
using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks;
using System.Text;
namespace x509device
{
class Program
{
private static int MESSAGE_COUNT = 5;
private const int TEMPERATURE_THRESHOLD = 30;
private static String deviceId = "mydevice";
private static float temperature;
private static float humidity;
private static Random rnd = new Random();
static async Task SendEvent(DeviceClient deviceClient)
{
string dataBuffer;
Console.WriteLine("Device sending {0} messages to IoTHub...\n", MESSAGE_COUNT);
for (int count = 0; count < MESSAGE_COUNT; count++)
{
temperature = rnd.Next(20, 35);
humidity = rnd.Next(60, 80);
dataBuffer = string.Format("{{\"deviceId\":\"{0}\",\"messageId\":{1},\"temperature\":{2},\"humidity\":{3}}}", deviceId, count, temperature, humidity);
Message eventMessage = new Message(Encoding.UTF8.GetBytes(dataBuffer));
eventMessage.Properties.Add("temperatureAlert", (temperature > TEMPERATURE_THRESHOLD) ? "true" : "false");
Console.WriteLine("\t{0}> Sending message: {1}, Data: [{2}]", DateTime.Now.ToLocalTime(), count, dataBuffer);
await deviceClient.SendEventAsync(eventMessage);
}
}
static void Main(string[] args)
{
try
{
var cert = new X509Certificate2("mydevice.pfx", "12345678");
var auth = new DeviceAuthenticationWithX509Certificate("mydevice", cert);
var deviceClient = DeviceClient.Create("seanyuiothub.azure-devices.cn", auth, TransportType.Amqp_Tcp_Only);
if (deviceClient == null)
{
Console.WriteLine("Failed to create DeviceClient!");
}
else
{
Console.WriteLine("Successfully created DeviceClient!");
SendEvent(deviceClient).Wait();
}
Console.WriteLine("Exiting...\n");
}
catch (Exception ex)
{
Console.WriteLine("Error in sample: {0}", ex.Message);
}
}
}
}
运行程序,看到如下发送遥测结果成功:
使用
ps7
执行Set-ExecutionPolicy -ExecutionPolicy Unrestricted
,会有问题,建议使用win10
自带的ps
。