在没有Azure IoT中心的Windows IoT核心上更新证书

时间:2019-05-03 08:03:18

标签: windows-iot-core-10

我们有一台Windows IoT设备,没有互联网连接。我们如何像在SSL中一样更新Azure IoT Hub证书。

Set up X.509 security in your Azure IoT hub

我们如何在C#中做到这一点?

1 个答案:

答案 0 :(得分:0)

请参考以下代码。CertificateEnrollmentManager可用于从个人信息交换(PFX)消息中导入证书。首先,您需要从uri读取远程文件,将缓冲区解析为base64字符串,然后将证书导入到您的商店。

            string pfxCertificate = null;
            string pfxPassword = "";

            var remoteUri = "http://XXXXXXXX";
            IRandomAccessStreamReference thumbnail = RandomAccessStreamReference.CreateFromUri(new Uri(remoteUri));
            file = await Windows.Storage.StorageFile.CreateStreamedFileFromUriAsync(".pdf",new Uri(remoteUri), thumbnail);

            var buffer = await Windows.Storage.FileIO.ReadBufferAsync(file);
            using (DataReader dataReader = DataReader.FromBuffer(buffer))
            {
                byte[] bytes = new byte[buffer.Length];
                dataReader.ReadBytes(bytes);
                // convert to Base64 for using with ImportPfx
                pfxCertificate = System.Convert.ToBase64String(bytes);
            }

            await CertificateEnrollmentManager.UserCertificateEnrollmentManager.ImportPfxDataAsync(
                        pfxCertificate,
                        pfxPassword,
                        ExportOption.NotExportable,
                        KeyProtectionLevel.NoConsent,
                        InstallOptions.None,
                        "Test");