具有 TLS 未经授权结果的 Xamarin.Android Mqtt 客户端

发布于 2025-01-21 03:50:51 字数 2082 浏览 3 评论 0原文

我基本上是移动开发的新手,并且坚持了几个星期,因为 我无法将Xamarin Android应用与TLS上的MQTTT连接。
也许我正在使用错误的库,还是Xamarin错误?
我的证书是M21.cloudmqtt.com的.crt文件。

https://crt.sh/?id=5253106089

首先是使用System.net mqtt,但它们是他们目前无法在TLS上工作。
因此,我目前正在使用MQTTNET,(目前)我已将其存储在资产文件夹中的M21.Cloud.com的默认证书。
我用有没有证书的证书进行了本地测试,其工作正常。 带有本地文件夹证书的MQTTNET客户端是这样的,它应该像:

var caCert = new X509Certificate2("C:/pathtocert.crt");
            var source = new CancellationTokenSource().Token;
            var token = source;

            var factory = new MqttFactory();
            var mqttClient = factory.CreateMqttClient();

            var mqttOptions = new MqttClientOptionsBuilder()
                .WithTcpServer(server, port)
                .WithClientId(clientId)
                .WithCredentials(username, pswd)
                .WithTls(new MqttClientOptionsBuilderTlsParameters
                {
                    UseTls = true,
                    Certificates = new List<X509Certificate> { caCert }
                })
                .Build();

            mqttClient.ConnectAsync(mqttOptions, token).Wait(token);

要从Android Assets文件夹中获得认证,我使用了与上述相同的客户端代码,以及我使用的证书:

using (var assetStream = await Xamarin.Essentials.FileSystem.OpenAppPackageFileAsync("filename.crt"))
                using (var memStream = new MemoryStream())
                {
                    assetStream.CopyTo(memStream);
                    caCert = new X509Certificate(memStream.ToArray());
                }

我不明白为什么它不起作用,现在,如果未使用证书,但需要使用TLS,也可以。但是尝试过,但我仍然会遇到未经授权的错误。

    var mqttOptions = new MqttClientOptionsBuilder()
                .WithTcpServer(server, port)
                .WithClientId(clientId)
                .WithCredentials(username, pswd)
                .WithTls(new MqttClientOptionsBuilderTlsParameters
                {
                    UseTls = true,
                })
                .Build();

感谢Adnvance。

I'm basically new to mobile development and stuck for a few weeks because,
I am unable to connect XAMARIN Android app with MQTTT over TLS.
Maybe i am using the wrong library or is it an Xamarin error?
My certificate is a .crt file from m21.cloudmqtt.com.

https://crt.sh/?id=5253106089

First was using System.Net MQTT but they are currently unable to work over TLS.
So i currently i am using MQTTNet, with (for the moment) the default certificate from m21.cloud.com which i have stored in Assets folder.
I tested this local with and without a certificate and its working fine.
The MQTTNet Client with cert from local folder is like this, and works like it should:

var caCert = new X509Certificate2("C:/pathtocert.crt");
            var source = new CancellationTokenSource().Token;
            var token = source;

            var factory = new MqttFactory();
            var mqttClient = factory.CreateMqttClient();

            var mqttOptions = new MqttClientOptionsBuilder()
                .WithTcpServer(server, port)
                .WithClientId(clientId)
                .WithCredentials(username, pswd)
                .WithTls(new MqttClientOptionsBuilderTlsParameters
                {
                    UseTls = true,
                    Certificates = new List<X509Certificate> { caCert }
                })
                .Build();

            mqttClient.ConnectAsync(mqttOptions, token).Wait(token);

To get the Certifcate from Android Assets Folder i used the same client code as above and t et the certificate i used:

using (var assetStream = await Xamarin.Essentials.FileSystem.OpenAppPackageFileAsync("filename.crt"))
                using (var memStream = new MemoryStream())
                {
                    assetStream.CopyTo(memStream);
                    caCert = new X509Certificate(memStream.ToArray());
                }

I dont understand why its not working, for now its also okay if the certificate isn't used but it needs to use TLS. But tried, and i still get unauthorized error.

    var mqttOptions = new MqttClientOptionsBuilder()
                .WithTcpServer(server, port)
                .WithClientId(clientId)
                .WithCredentials(username, pswd)
                .WithTls(new MqttClientOptionsBuilderTlsParameters
                {
                    UseTls = true,
                })
                .Build();

Thanks in adnvance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文