ケース - のIoTハブを検証し、通信を開始するためにDPSを介してデバイスによって登録されたX509証明書
この場合の手順:
最初の2つのステップは、前のセクションで導入されていることに注意し、この論文では、第三の工程が開始記載されています。
CAルート証明書のopensslを生成し、MicrosoftのPowershellスクリプトの例1.。
2.ザ・CAルート証明書は、DPSサービスとの完全な所有権の確認にアップロード。
3.マイクロソフトとOpenSSL例Powershllスクリプトによって、デバイス証明書を生成します。
4.シミュレーションプログラム(C#)と認証したIoTハブDPS登録するデバイス証明書を用いて。
シミュレーションは、メッセージにデバイス証明書を使用してテレメトリのIoTハブに直接送信。
ここで、以下のように、図4/5の論理手順は以下のとおりです。
ビデオチュートリアル:
あなたはB駅を説明するビデオを見ることができます。https://www.bilibili.com/video/av93099113/
写真の説明:
3.マイクロソフトとOpenSSL例Powershllスクリプトによって、デバイス証明書を生成します。
デバイス証明書、mydevice001名、デバイスIDのつまりのIoTハブ最終登録を表す証明書を生成し、次のコマンドを実行します。
新CACertsDevice mydevice001
ここでは、証明書のパスワードを入力する必要があり、ユーザーが定義したパスワード、我々は12345678を入力します。
もう一度パスワードを入力するプロンプトに従って、次の図の証明書を生成することができ、我々は、デバイスで使用されるmydevice001.pfx証明書ファイルです。
4.シミュレーションプログラム(C#)と認証したIoTハブDPS登録するデバイス証明書を用いて。
この例では、サンプルコードに私たちは講義のコードを、証明書をコピーします-使用「紺碧のIoT中級(3)(ケース1)使用DPS単一の対称鍵登録装置」で
下载地址:https://codeload.github.com/Azure-Samples/azure-iot-samples-csharp/zip/master
本节使用的是 azure-iot-samples-csharp-master\provisioning\Samples\device\X509Sample 项目
代码中需要修改的地方:
”GlobalDeviceEndpoint“ 改成"global.azure-devices-provisioning.cn"
"s_certificateFileName"改成证书的名称,此处为”mydevice001.pfx“
”s_idScope“改成实际的DPS的 id scope。
using Microsoft.Azure.Devices.Provisioning.Client;using Microsoft.Azure.Devices.Provisioning.Client.Transport;using Microsoft.Azure.Devices.Shared;using System;using System.IO;using System.Security.Cryptography.X509Certificates;using System.Text;namespace Microsoft.Azure.Devices.Provisioning.Client.Samples{ public static class Program { // The Provisioning Hub IDScope. // For this sample either: // - pass this value as a command-prompt argument // - set the DPS_IDSCOPE environment variable // - create a launchSettings.json (see launchSettings.json.template) containing the variable // private static string s_idScope = Environment.GetEnvironmentVariable(""); private static string s_idScope = "0cn0000DC5D"; // In your Device Provisioning Service please go to "Manage enrollments" and select "Individual Enrollments". // Select "Add individual enrollment" then fill in the following: // Mechanism: X.509 // Certificate: // You can generate a self-signed certificate by running the GenerateTestCertificate.ps1 powershell script. // Select the public key 'certificate.cer' file. ('certificate.pfx' contains the private key and is password protected.) // For production code, it is advised that you install the certificate in the CurrentUser (My) store. // DeviceID: iothubx509device1 // X.509 certificates may also be used for enrollment groups. // In your Device Provisioning Service please go to "Manage enrollments" and select "Enrollment Groups". // Select "Add enrollment group" then fill in the following: // Group name: <your group name> // Attestation Type: Certificate // Certificate Type: // choose CA certificate then link primary and secondary certificates // OR choose Intermediate certificate and upload primary and secondary certificate files // You may also change other enrollemtn group parameters according to your needs private const string GlobalDeviceEndpoint = "global.azure-devices-provisioning.cn"; private static string s_certificateFileName = "mydevice001.pfx"; public static int Main(string[] args) { if (string.IsNullOrWhiteSpace(s_idScope) && (args.Length > 0)) { s_idScope = args[0]; } if (string.IsNullOrWhiteSpace(s_idScope)) { Console.WriteLine("ProvisioningDeviceClientX509 <IDScope>"); return 1; } X509Certificate2 certificate = LoadProvisioningCertificate(); using (var security = new SecurityProviderX509Certificate(certificate)) // Select one of the available transports: // To optimize for size, reference only the protocols used by your application. using (var transport = new ProvisioningTransportHandlerAmqp(TransportFallbackType.TcpOnly)) // using (var transport = new ProvisioningTransportHandlerHttp()) // using (var transport = new ProvisioningTransportHandlerMqtt(TransportFallbackType.TcpOnly)) // using (var transport = new ProvisioningTransportHandlerMqtt(TransportFallbackType.WebSocketOnly)) { ProvisioningDeviceClient provClient = ProvisioningDeviceClient.Create(GlobalDeviceEndpoint, s_idScope, security, transport); var sample = new ProvisioningDeviceClientSample(provClient, security); sample.RunSampleAsync().GetAwaiter().GetResult(); } return 0; } private static X509Certificate2 LoadProvisioningCertificate() { string certificatePassword = ReadCertificatePassword(); var certificateCollection = new X509Certificate2Collection(); certificateCollection.Import(s_certificateFileName, certificatePassword, X509KeyStorageFlags.UserKeySet); X509Certificate2 certificate = null; foreach (X509Certificate2 element in certificateCollection) { Console.WriteLine($"Found certificate: {element?.Thumbprint} {element?.Subject}; PrivateKey: {element?.HasPrivateKey}"); if (certificate == null && element.HasPrivateKey) { certificate = element; } else { element.Dispose(); } } if (certificate == null) { throw new FileNotFoundException($"{s_certificateFileName} did not contain any certificate with a private key."); } else { Console.WriteLine($"Using certificate {certificate.Thumbprint} {certificate.Subject}"); } return certificate; } private static string ReadCertificatePassword() { var password = new StringBuilder(); Console.WriteLine($"Enter the PFX password for {s_certificateFileName}:"); while(true) { ConsoleKeyInfo key = Console.ReadKey(true); if (key.Key == ConsoleKey.Backspace) { if (password.Length > 0) { password.Remove(password.Length - 1, 1); Console.Write("\b \b"); } } else if (key.Key == ConsoleKey.Enter) { Console.WriteLine(); break; } else { Console.Write('*'); password.Append(key.KeyChar); } } return password.ToString(); } }}
执行代码前,在DPS中添加组注册,选择证书方式,并从下拉列表里选择上一节内容配置到DPS中的证书:
执行代码:
输入证书密码,程序返回DPS将设备注册到的IoT Hub的名称和设备ID
此时,便完成了设备通过X509证书通过DPS注册到IoT Hub的步骤,可以在Portal-DPS中检查到如下结果:
同时,可以在IoT Hub中看到如下设备,验证方式是SelfSigned:
5. 模拟程序使用设备证书直接向IoT Hub 发送遥测消息
本步骤参考如下文档:
示例代码如下:
需要修改的地方有:
44行mydevice001.pfx, 和 12345678,替换成你的证书路径和 证书密码;
45行mydevice001换成你的deviceid
46行iot hub 换成第四步中,DPS返回的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 = "mydevice001"; 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("mydevice001.pfx", "12345678"); var auth = new DeviceAuthenticationWithX509Certificate("mydevice001", 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); } } }}
プログラムを実行し、以下の結果は、遠隔測定の成功を送って参照してください。
このシリーズの他の記事:
(ビデオ)AzureのIoTを中間体(5) - 証明書チェーンを理解するための準備中DPS / IoTをハブでX509証明書の使用(1)
(ビデオ)アズールのIoT中間体(6) - 自己署名証明書を作成し、所有権を確認する(2)製剤中DPS /のIoTハブでX509証明書を使用して
(ビデオ)AzureのIoTを中間体(7) - (ケース3)のIoTハブにX509証明書の登録を介した機器やDPSを確認した後に通信を開始