Azure IOT& X509证书

时间:2017-02-20 12:13:20

标签: c# azure x509certificate iot

我一直在关注Azure IOT开发人员指南页面上的示例代码。我有一个程序允许我从云端向我的设备发送消息。

class CloudToDeviceMessagSender
    {
        private static ServiceClient _serviceClient;
        private static string _connectionString = Common.ConnectionStringsAndCerts.ConnectionHelperStrings.ConnectionString;

        static void Main()
        {
            Console.WriteLine("Send Cloud-to-Device message\n");
            _serviceClient = ServiceClient.CreateFromConnectionString(_connectionString, TransportType.Amqp);

            Console.WriteLine("Enter message to send:");
            var message = Console.ReadLine();
            SendCloudToDeviceMessageAsync(message).Wait();
            Console.ReadLine();
        }

        private static async Task SendCloudToDeviceMessageAsync(string message)
        {
            var commandMessage = new Message(Encoding.ASCII.GetBytes(message));
            await _serviceClient.SendAsync("myDeviceId", commandMessage);
        }
    }

当我注册设备时,我使用X509证书进行注册。

这似乎工作正常:

x509 certs work

我的设备会读取从CloudToDeviceMessagSender发送的消息。

public static async void ReceiveCloudToDeviceAsync()
        {
            Console.WriteLine("\nReceiving cloud to device messages from service");
            while (true)
            {
                var receivedMessage = await _deviceClient.ReceiveAsync();
                if (receivedMessage == null) continue;

                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine("Received message: {0}", Encoding.ASCII.GetString(receivedMessage.GetBytes()));
                Console.ResetColor();

                await _deviceClient.CompleteAsync(receivedMessage);
            }
        }

我的问题是,如何让它只接受使用已注册的有效x509证书发送的邮件?

另一个问题是,如果我要使用x509证书来保护设备和云之间的通信,我是否需要每个设备1个证书,或者它是所有设备使用的1个证书?

0 个答案:

没有答案