I want to check username and password of MQTT client in MQTT server then allow its connection. I implemented a server and send data from a device. I get the data but the problem is the authentication does not work properly because I need to get the client information from DB based on the topic that the client sent. What did I do so far is as below:
public async Task Received()
{
var options = new MqttServerOptions();
var mqttServer = new MqttFactory().CreateMqttServer();
mqttServer.ApplicationMessageReceived += (sender, eventArgs) =>
{
var path = eventArgs.ApplicationMessage.Topic;
var device= GetDevice(path);
options.ConnectionValidator = p =>
{
if (p.Username != device.username || p.Password != device.password)
{
p.ReturnCode = MqttConnectReturnCode.ConnectionRefusedBadUsernameOrPassword;
}
};
};
mqttServer.ClientConnected += (s, e) => { };
mqttServer.ClientDisconnected += (s, e) => { };
mqttServer.ClientSubscribedTopic += (s, e) => { };
mqttServer.ClientUnsubscribedTopic += (s, e) => { };
mqttServer.Started += (s, e) => { };
mqttServer.Stopped += (s, e) => { };
await mqttServer.StartAsync(options);
}
and this code is in my startup
app.UseMqttServer(server =>
{
server.Started += async (sender, args) => await myClass.Received();
});
I can get the requests in my method but I have difficulty to check the username and password.