Code Monkey home page Code Monkey logo

Comments (2)

rido-min avatar rido-min commented on May 28, 2024 1

Hi @neetabisht,

We have samples for Azure Event Grid in https://github.com/Azure-Samples/MqttApplicationSamples, I'd recommend moving this issue there.

From a quick glance to the sample provided, you dont need to call .WithTrustChain(certificates) as the TLS connection is protected with a chain that can be resolved from public certs, in Windows from the Trusted Certificates Store.

from mqttnet.

neetabisht avatar neetabisht commented on May 28, 2024

Hi @rido-min
After adding the Intermediate Crt file in Windows trusted certificates store and updating the code(removed .withTrustChain, added WithClientCertificates) as below, I'm able to connect with azure event grid server.

using MQTTnet.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace MQTTnet.Samples.Client;

public static class Client_Connection_pub
{
    public static async Task connect_with_pub()
    {
        string hostname = "publishjsonmqtteventgridns.westeurope-1.ts.eventgrid.azure.net";
        string clientId = "client1-session1";  //client ID can be the session identifier.  A client can have multiple sessions using username and clientId. 
        string x509_pem = @"C:\Users\INNEBIS1/certificate/client1-authn-ID.pem";  //Provide your client certificate .cer.pem file path
        string x509_key = @"C:\Users\INNEBIS1/certificate/client1-authn-ID.key";  //Provide your client certificate .key.pem file path

        // Load certificate and private key from PEM files
        var certificate = new X509Certificate2(X509Certificate2.CreateFromPemFile(x509_pem, x509_key).Export(X509ContentType.Pkcs12));

        // Add the loaded certificate to a certificate collection
        X509Certificate2Collection certificates = new X509Certificate2Collection
        {
            certificate
        };

        var mqttClient = new MqttFactory().CreateMqttClient();
        var connAck = await mqttClient!.ConnectAsync(new MqttClientOptionsBuilder()
            .WithTcpServer(hostname, 8883)
            .WithClientId(clientId)
            .WithCredentials("client1-authn-ID", "")  //use client authentication name in the username
            .WithTlsOptions(new MqttClientTlsOptionsBuilder()
                .WithSslProtocols(System.Security.Authentication.SslProtocols.Tls12 | System.Security.Authentication.SslProtocols.Tls13)
                **.WithClientCertificates(certificates) // Missed to update the client certificate provider details**
                .Build())
            .Build());

        Console.WriteLine($"Client Connected: {mqttClient.IsConnected} with CONNACK: {connAck.ResultCode}");

        mqttClient.ApplicationMessageReceivedAsync += async m => await Console.Out.WriteAsync($"Received message on topic: '{m.ApplicationMessage.Topic}' with content: '{m.ApplicationMessage.ConvertPayloadToString()}'\n\n");

        var suback = await mqttClient.SubscribeAsync("contosotopics/topic1");
        suback.Items.ToList().ForEach(s => Console.WriteLine($"subscribed to '{s.TopicFilter.Topic}' with '{s.ResultCode}'"));

        while (true)
        {
            var puback = await mqttClient.PublishStringAsync("contosotopics/topic1", "hello world!");
            Console.WriteLine(puback.ReasonString);
            await Task.Delay(1000);
        }
    }
}

Thanks a lot 👍

from mqttnet.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.