调用SSPI失败,请参阅内部异常paho m2mqtt Dot.Net(c#)客户端SSL/TLS连接 [英] A call to SSPI failed, see inner exception paho m2mqtt Dot.Net(c#) client SSL/TLS connection

查看:331
本文介绍了调用SSPI失败,请参阅内部异常paho m2mqtt Dot.Net(c#)客户端SSL/TLS连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to connect with mosquitto broker using m2mqtt c# client version 4.3.0 library via SSL/TLS. Below is the code I have tried

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);
    }

但我遇到了异常

对SSPI的调用失败,请参阅内部异常.

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#客户端库进行连接时,才会发生此异常.任何帮助或示例实现都将适用.

For information I can successfully connect with broker without SSL/TLS. Also using Paho Java client via both with or without SSL/TLS I can connect with the broker. This exception is happen only when I am trying to connect using m2mqtt C# client library via SSL/TLS. Any help or sample implementation will be appriciated.

推荐答案

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

Finally found the solution. To use SSL certificate inside Dot.Net framework we need to provide both certificate and its corresponding private key together. To achieve this we need to use p12(.pfx) file which combined this two. In my project, I have used self-signed certificate using OpenSSL so I used below command to combine certificate and private key

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

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

which will create p12(.pfx) file for each certificate. Then I have used them into my code like below

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);
    }

这篇关于调用SSPI失败,请参阅内部异常paho m2mqtt Dot.Net(c#)客户端SSL/TLS连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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