Comments (2)
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.
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)
- Connection timeout error due to high latency on the network
- Invert conditions in aspnet package csproj
- Cannot Connect Server on Android/MAUI Blazor Project
- Maximum message / s throughput for a single subscriber can't stay above 10000 m/s HOT 5
- Flaky exceptions for oversized messages HOT 1
- Intercepting Publish on server does not return the correct status code to the client HOT 3
- Retained Messages limited to 250 HOT 1
- MqttServer.StartAsync Unable to actually start the listening port HOT 5
- MqttClient Publish a message when you are inside the `InterceptingPublishAsync` result to an Error when the Client Publish a QoS 1
- MqttServer messages send get lost while client is subscribing HOT 2
- Unable to use certificate in Ubuntu (TLS1.2) HOT 1
- MqttClient with TLS and client certificate works on Linux but not on windows using the latest and following samples HOT 18
- No valid replacement for the obsolete MqttClientOptionsBuilder.WithConnectionUri HOT 1
- MQTTnet.AspTestApp Fails to start HOT 1
- MQTTnet.Extensions.WebSocket4Net uses several outdated and possibly vulnerable dependencies HOT 4
- MQTT-over-Websocket connection reconnects every 30 seconds (MAUI regression) HOT 1
- Can't connect to broker using CA Cert ClientCert and Key HOT 2
- Unable to connect with Unix Socket HOT 4
- Last Will not working? HOT 2
- [FR] Protobuf HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mqttnet.