MerossService MQTT working !

This commit is contained in:
Thomas Fransolet 2020-01-19 03:16:54 +01:00
parent 1cd7a56d1b
commit b9599ec357
3 changed files with 53 additions and 8 deletions

View File

@ -197,21 +197,17 @@ namespace MyCore.Services
_client = new MqttFactory().CreateMqttClient(); _client = new MqttFactory().CreateMqttClient();
string stringForMD5 = resultToken.userid + resultToken.key; string stringForMD5 = resultToken.userid + resultToken.key;
string hashed_password = CreateMD5(stringForMD5); string hashed_password = CreateMD5(stringForMD5).ToLower();
MqttClientOptionsBuilderTlsParameters tlsParameters = new MqttClientOptionsBuilderTlsParameters();
tlsParameters.UseTls = true;
tlsParameters.SslProtocol = System.Security.Authentication.SslProtocols.Tls;
_options = new MqttClientOptionsBuilder() _options = new MqttClientOptionsBuilder()
.WithClientId("app: 08d4c9f99da40203ebc798a79512ec14") .WithClientId("app:ddddd9eb14407b666f559af5af9c1840")
.WithTcpServer(domain, port) .WithTcpServer(domain, port)
.WithCredentials(resultToken.userid, hashed_password) .WithCredentials(resultToken.userid, hashed_password)
.WithTls(tlsParameters) .WithCleanSession()
.WithTls()
.WithProtocolVersion(MqttProtocolVersion.V311) .WithProtocolVersion(MqttProtocolVersion.V311)
.Build(); .Build();
_client.ConnectAsync(_options, CancellationToken.None).ContinueWith(res => { _client.ConnectAsync(_options, CancellationToken.None).ContinueWith(res => {
if (res.Status == TaskStatus.RanToCompletion) if (res.Status == TaskStatus.RanToCompletion)
{ {
@ -265,5 +261,54 @@ namespace MyCore.Services
} }
} }
private static bool CertificateValidationCallBack(
object sender,
System.Security.Cryptography.X509Certificates.X509Certificate certificate,
System.Security.Cryptography.X509Certificates.X509Chain chain,
System.Net.Security.SslPolicyErrors sslPolicyErrors)
{
// If the certificate is a valid, signed certificate, return true.
if (sslPolicyErrors == System.Net.Security.SslPolicyErrors.None)
{
return true;
}
// If there are errors in the certificate chain, look at each error to determine the cause.
if ((sslPolicyErrors & System.Net.Security.SslPolicyErrors.RemoteCertificateChainErrors) != 0)
{
if (chain != null && chain.ChainStatus != null)
{
foreach (System.Security.Cryptography.X509Certificates.X509ChainStatus status in chain.ChainStatus)
{
if ((certificate.Subject == certificate.Issuer) &&
(status.Status == System.Security.Cryptography.X509Certificates.X509ChainStatusFlags.UntrustedRoot))
{
// Self-signed certificates with an untrusted root are valid.
continue;
}
else
{
if (status.Status != System.Security.Cryptography.X509Certificates.X509ChainStatusFlags.NoError)
{
// If there are any other errors in the certificate chain, the certificate is invalid,
// so the method returns false.
return false;
}
}
}
}
// When processing reaches this line, the only errors in the certificate chain are
// untrusted root errors for self-signed certificates. These certificates are valid
// for default Exchange server installations, so return true.
return true;
}
else
{
// In all other cases, return false.
return false;
}
}
} }
} }