c# - 对 SSPI 的调用失败,请参阅内部异常 paho m2mqtt Dot.Net(c#) 客户端 SSL/TLS 连接

标签 c# ssl mqtt mosquitto paho

我正在尝试通过 SSL/TLS 使用 m2mqtt c# 客户端版本 4.3.0 库与 mosquitto 代理连接。下面是我试过的代码

static void Main(string[] args)
    {

        // create client instance
        MqttClient client = new MqttClient(IPAddress.Parse("127.0.0.1"), 8883, true, 
                                new X509Certificate2("C:\\Users\\hp\\Desktop\\certificate\\ca.crt"), 
                                new X509Certificate2("C:\\Users\\hp\\Desktop\\certificate\\client.crt"), 
                                MqttSslProtocols.TLSv1_2);

        // register to message received
        client.MqttMsgPublishReceived += client_MqttMsgPublishReceived;

        string clientId = "pahoSubscriber2";
        client.Connect(clientId);

        // subscribe to the topic "hello" with QoS 0
        client.Subscribe(new string[] { "hello" }, new byte[] { MqttMsgBase.QOS_LEVEL_AT_MOST_ONCE });

    }

static void client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e)
    {
        // handle message received
        Console.WriteLine(e.Message);
    }

但是我遇到了异常

A call to SSPI failed, see inner exception.

内部异常说

the message received was unexpected or badly formatted

有关信息,我可以在没有 SSL/TLS 的情况下成功连接代理。也可以通过带或不带 SSL/TLS 的方式使用 Paho Java 客户端,我可以连接到代理。仅当我尝试通过 SSL/TLS 使用 m2mqtt C# 客户端库进行连接时才会发生此异常。任何帮助或示例实现都将得到应用。

最佳答案

终于找到解决办法了。要在 Dot.Net 框架内使用 SSL 证书,我们需要同时提供证书及其相应的私钥。为此,我们需要使用结合了这两者的 p12(.pfx) 文件。在我的项目中,我使用了使用 OpenSSL 的自签名证书,所以我使用下面的命令来组合证书和私钥

pkcs12 -export -out ca.pfx -inkey ca.key -in ca.crt
pkcs12 -export -out client.pfx -inkey client.key -in client.crt

这将为每个证书创建 p12(.pfx) 文件。然后我将它们用到我的代码中,如下所示

static void Main(string[] args)
    {

        // create client instance
        MqttClient client = new MqttClient(IPAddress.Parse("127.0.0.1"), 8883, true, 
                                new X509Certificate2("C:\\Users\\hp\\Desktop\\certificate\\ca.pfx"), 
                                new X509Certificate2("C:\\Users\\hp\\Desktop\\certificate\\client.pfx"), 
                                MqttSslProtocols.TLSv1_2);

        // register to message received
        client.MqttMsgPublishReceived += client_MqttMsgPublishReceived;

        string clientId = "pahoSubscriber2";
        client.Connect(clientId);

        // subscribe to the topic "hello" with QoS 0
        client.Subscribe(new string[] { "hello" }, new byte[] { MqttMsgBase.QOS_LEVEL_AT_MOST_ONCE });

    }

static void client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e)
    {
        // handle message received
        Console.WriteLine(e.Message);
    }

关于c# - 对 SSPI 的调用失败,请参阅内部异常 paho m2mqtt Dot.Net(c#) 客户端 SSL/TLS 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43993106/

相关文章:

android - Kotlin Android 中的 OKhttp 自签名证书

regex - 将 http 重定向到 https(仅前端而不是管理面板)

c++ - mbed 中的 Hello World MQTT 程序

c - Mosquitto - 订阅一个经纪人并使用 C 推送到另一个经纪人

.net - 你能给我一些关于 .NET 和 TLS 1.2 的一般性建议吗

c# - Func<Owned<T>> 与 Func<T> 依赖关系

c# - 如何在 OpenCvSharp 中获取一条线上的所有点?

c# - NPOI - 获取 excel 行数以检查它是否为空

javascript - 如何通过 paho mqtt js 客户端强制进行非安全连接?

c# - 打开 InputBox 时 Excel 无响应,c#