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

By simon at 2018-02-28 • 0人收藏 • 125人看过

我正尝试使用m2mqtt c#客户端版本与蚊子代理进行连接 4.3.0库通过SSL / TLS。以下是我的代码试着

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的调用失败,请参阅内部异常。 内部例外说

收到的信息意外或格式不正确 有关信息,我可以成功地与经纪人无SSL / TLS连接。也 通过使用或不使用SSL / TLS使用Paho Java客户端我可以使用 经纪人。这个异常只发生在什么时候我正在尝试使用m2mqtt进行连接 C#客户端库,通过SSL / TLS。任何帮助或样本实施将是 appriciated。

3 个回复 | 最后更新于 2018-02-28
2018-02-28   #1

终于找到了解决办法。在Dot.Net框架内使用SSL证书 需要提供证书和相关证书找到私钥在一起。 为了达到这个目标,我们需要使用p12(.pfx)文件来结合这两个文件。在我的 项目,我用过se如果使用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);
    }

2018-02-28   #2

终于找到了解决办法。在Dot.Net框架内使用SSL证书 需要提供证书和相关证书找到私钥在一起。 为了达到这个目标,我们需要使用p12(.pfx)文件来结合这两个文件。在我的 项目,我用过se如果使用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);
    }

2018-02-28   #3

正如我所经历的关键点是在本地机器上安装证书 根证书!如果'ca.crt'文件安装了你你可以使用null值吗? 你的两个参数=> caCert,clientCert。这个 [链接](https://www.cpume.com/question/feenhfzz-gettinG- 的AuthenticationException - 当 - 连接 - m2mqtt-mqttclient到mosquitto-brok.html) 经过几个小时的困惑,我帮助了我!

 static void Main(string[] args){
// create client instance
MqttClient client = new MqttClient(IPAddress.Parse("127.0.0.1"), 8883,   true, null, null, 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);
}

登录后方可回帖

Loading...