MQTT Azure IOT集线器连接C# [英] MQTT Azure IOT Hub Connection C#

查看:150
本文介绍了MQTT Azure IOT集线器连接C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I am trying to create secure TCP Client and trying to connect with Microsoft Azure IOT hub over port number 8883 i.e. MQTT Protocol. I have created connect packet for azure IOT hub with below Packet Format. Everytime SSLStream.read returns 0 in my case. Checked with OpenSSL whether connection with iot hub with certificate on port number 8883 is working or not, with Open SSL it is working. What is wrong in my packet or code which fails my connection ?

Connect Packet :
    0x10 - Connect,Remaining Length,Protocol Length,'M','Q','I','S','D','P',Level,Flag,Keep Alive,Client Id Length,Client ID, User Name Length,User Name,Password Length,Password

TcpClient client = new TcpClient(machineName,8883);
            Console.WriteLine("Client connected.");
            // Create an SSL stream that will close the client's stream.
            SslStream sslStream = new SslStream(
                client.GetStream(),
                false,
                new RemoteCertificateValidationCallback(ValidateServerCertificate),
                null
                );


推荐答案

Hello Tech,

Hello Tech,

从您所展示的代码部分,很难说出问题的原因.摘录自 SslStream.Read 我们可以看到:

From the part of code you have shown, it is hard to say the reason of the issue. From document of SslStream.Read we can see that:

您不能打电话此方法,直到您成功通过身份验证.要进行身份验证,请呼叫 , 或 BeginAuthenticateAsClient > ,  AuthenticateAsServer ="color:#2a2a2a; font-family:'Segoe UI','Lucida Grande',Verdana,Arial,Helvetica,sans-serif; font-size:13px"> ,  span> 方法.

You cannot call this method until you have successfully authenticated. To authenticate call one of the AuthenticateAsClient, or BeginAuthenticateAsClientAuthenticateAsServerBeginAuthenticateAsServer methods.

您可能需要这样的代码:

You may need some code like this:

                    var x509CertificateCollection = new X509Certificate2Collection();
                    if (clientCertificate != null)
                    {
                        x509CertificateCollection.Add(clientCertificate);
                    }

                    // TODO: Do we need to make CheckCertificateRevocation user configurable?
                    await sslStream.AuthenticateAsClientAsync(host, x509CertificateCollection, enabledSslProtocols: SslProtocols.Tls11 | SslProtocols.Tls12, checkCertificateRevocation:false).ConfigureAwait(false);


注意: Azure IoT中心需要DigiCert巴尔的摩根证书来建立TLS连接.您可以引用"直接使用 MQTT协议直接"更多信息.

And note: Azure IoT Hub require DigiCert Baltimore Root Certificate to establish a TLS connection. You can reference "Using the MQTT protocol directly" more information.

Azure IoT中心SDK ,用于多种语言,可帮助将设备连接到Azure IoT中心服务.建议使用SDK与IoT中心进行通信.

There are Azure IoT Hub SDKs for a variety of languages for helping to connect devices to Azure IoT Hub services. It is suggested to use SDK to communicate with IoT Hub. 

如果不能直接使用SDK,则可以引用 github上的C#SDK ,并实现自己的.

If you can't use SDK directly, you can reference source code of C# SDK on github and implement your own.

最诚挚的问候,

Rita


这篇关于MQTT Azure IOT集线器连接C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆