mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-28 17:22:37 +08:00
Password Provider for dynamic password
This commit is contained in:
@@ -10,5 +10,7 @@ namespace MQTTnet.Core.ManagedClient
|
|||||||
TimeSpan AutoReconnectDelay { get; }
|
TimeSpan AutoReconnectDelay { get; }
|
||||||
|
|
||||||
IManagedMqttClientStorage Storage { get; }
|
IManagedMqttClientStorage Storage { get; }
|
||||||
|
|
||||||
|
Func<IManagedMqttClientOptions, string> PasswordProvider { get; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -277,6 +277,7 @@ namespace MQTTnet.Core.ManagedClient
|
|||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
_options.PasswordProvider?.Invoke(_options);
|
||||||
await _mqttClient.ConnectAsync(_options.ClientOptions).ConfigureAwait(false);
|
await _mqttClient.ConnectAsync(_options.ClientOptions).ConfigureAwait(false);
|
||||||
return ReconnectionResult.Reconnected;
|
return ReconnectionResult.Reconnected;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,5 +10,8 @@ namespace MQTTnet.Core.ManagedClient
|
|||||||
public TimeSpan AutoReconnectDelay { get; set; } = TimeSpan.FromSeconds(5);
|
public TimeSpan AutoReconnectDelay { get; set; } = TimeSpan.FromSeconds(5);
|
||||||
|
|
||||||
public IManagedMqttClientStorage Storage { get; set; }
|
public IManagedMqttClientStorage Storage { get; set; }
|
||||||
|
|
||||||
|
public Func<IManagedMqttClientOptions, string> PasswordProvider { get; set; }
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,9 @@ using MQTTnet.Core.Packets;
|
|||||||
using MQTTnet.Core.Protocol;
|
using MQTTnet.Core.Protocol;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
|
using System.IO;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
namespace MQTTnet.TestApp.NetCore
|
namespace MQTTnet.TestApp.NetCore
|
||||||
{
|
{
|
||||||
@@ -22,18 +25,32 @@ namespace MQTTnet.TestApp.NetCore
|
|||||||
services.GetService<ILoggerFactory>()
|
services.GetService<ILoggerFactory>()
|
||||||
.AddConsole();
|
.AddConsole();
|
||||||
|
|
||||||
|
ClientRetainedMessageHandler ms = new ClientRetainedMessageHandler();
|
||||||
|
Func<ManagedMqttClientOptions, string> func = delegate (ManagedMqttClientOptions managedMqttClientOptions)
|
||||||
|
{
|
||||||
|
return "password";
|
||||||
|
};
|
||||||
|
|
||||||
var options = new ManagedMqttClientOptions
|
var options = new ManagedMqttClientOptions
|
||||||
{
|
{
|
||||||
ClientOptions = new MqttClientTcpOptions
|
ClientOptions = new MqttClientTcpOptions
|
||||||
{
|
{
|
||||||
Server = "broker.hivemq.com",
|
Server = "broker.hivemq.com",
|
||||||
ClientId = "MQTTnetManagedClientTest"
|
ClientId = "MQTTnetManagedClientTest",
|
||||||
|
Password = "pippo",
|
||||||
},
|
},
|
||||||
|
|
||||||
AutoReconnectDelay = TimeSpan.FromSeconds(1)
|
AutoReconnectDelay = TimeSpan.FromSeconds(1),
|
||||||
|
Storage = ms,
|
||||||
|
PasswordProvider = o =>
|
||||||
|
{
|
||||||
|
//o.ClientOptions.Password = GetPassword();
|
||||||
|
return o.ClientOptions.Password;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var managedClient = services.GetRequiredService<ManagedMqttClient>();
|
var managedClient = services.GetRequiredService<ManagedMqttClient>();
|
||||||
@@ -59,5 +76,39 @@ namespace MQTTnet.TestApp.NetCore
|
|||||||
Console.WriteLine(e);
|
Console.WriteLine(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static string GetPassword()
|
||||||
|
{
|
||||||
|
return "password";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public class ClientRetainedMessageHandler : IManagedMqttClientStorage
|
||||||
|
{
|
||||||
|
private const string Filename = @"RetainedMessages.json";
|
||||||
|
|
||||||
|
public Task SaveQueuedMessagesAsync(IList<MqttApplicationMessage> messages)
|
||||||
|
{
|
||||||
|
File.WriteAllText(Filename, JsonConvert.SerializeObject(messages));
|
||||||
|
return Task.FromResult(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Task<IList<MqttApplicationMessage>> LoadQueuedMessagesAsync()
|
||||||
|
{
|
||||||
|
IList<MqttApplicationMessage> retainedMessages;
|
||||||
|
if (File.Exists(Filename))
|
||||||
|
{
|
||||||
|
var json = File.ReadAllText(Filename);
|
||||||
|
retainedMessages = JsonConvert.DeserializeObject<List<MqttApplicationMessage>>(json);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
retainedMessages = new List<MqttApplicationMessage>();
|
||||||
|
}
|
||||||
|
|
||||||
|
return Task.FromResult(retainedMessages);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Binary file not shown.
Reference in New Issue
Block a user