Add support for chaning passwords

This commit is contained in:
Christian Kratky
2017-10-25 19:50:37 +02:00
parent c257e5cb77
commit a9b394401b
15 changed files with 52 additions and 46 deletions
@@ -3,7 +3,6 @@ using System.Threading.Tasks;
using MQTTnet.Core;
using MQTTnet.Core.Client;
using MQTTnet.Core.ManagedClient;
using MQTTnet.Core.Packets;
using MQTTnet.Core.Protocol;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
@@ -25,32 +24,21 @@ namespace MQTTnet.TestApp.NetCore
services.GetService<ILoggerFactory>()
.AddConsole();
ClientRetainedMessageHandler ms = new ClientRetainedMessageHandler();
Func<ManagedMqttClientOptions, string> func = delegate (ManagedMqttClientOptions managedMqttClientOptions)
{
return "password";
};
var ms = new ClientRetainedMessageHandler();
var options = new ManagedMqttClientOptions
{
ClientOptions = new MqttClientTcpOptions
{
Server = "broker.hivemq.com",
ClientId = "MQTTnetManagedClientTest",
Password = "pippo",
Credentials = new RandomPassword()
},
AutoReconnectDelay = TimeSpan.FromSeconds(1),
Storage = ms,
PasswordProvider = o =>
{
//o.ClientOptions.Password = GetPassword();
return o.ClientOptions.Password;
}
Storage = ms
};
try
{
var managedClient = services.GetRequiredService<ManagedMqttClient>();
@@ -78,11 +66,18 @@ namespace MQTTnet.TestApp.NetCore
}
public static string GetPassword()
public class RandomPassword : IMqttClientCredentials
{
return "password";
}
public string Password
{
get
{
return Guid.NewGuid().ToString(); // The random password.
}
}
public string Username => "the_static_user";
}
public class ClientRetainedMessageHandler : IManagedMqttClientStorage
{
@@ -42,7 +42,7 @@ namespace MQTTnet.TestApp.UniversalWindows
private async void Connect(object sender, RoutedEventArgs e)
{
BaseMqttClientOptions options = null;
MqttClientOptions options = null;
if (UseTcp.IsChecked == true)
{
options = new MqttClientTcpOptions
@@ -64,8 +64,12 @@ namespace MQTTnet.TestApp.UniversalWindows
throw new InvalidOperationException();
}
options.UserName = User.Text;
options.Password = Password.Text;
options.Credentials = new MqttClientCredentials
{
Username = User.Text,
Password = Password.Text
};
options.ClientId = ClientId.Text;
options.TlsOptions.UseTls = UseTls.IsChecked == true;
options.TlsOptions.IgnoreCertificateChainErrors = true;