private MqttConfiguration GetConfigurationViaAttributeValues(MqttBaseAttribute mqttAttribute)
        {
            var name                 = mqttAttribute.ConnectionString ?? DefaultAppsettingsKeyForConnectionString;
            var connectionString     = _nameResolver.Resolve(mqttAttribute.ConnectionString) ?? _nameResolver.Resolve(DefaultAppsettingsKeyForConnectionString);
            var mqttConnectionString = new MqttConnectionString(connectionString);

            var mqttClientOptions = GetMqttClientOptions(mqttConnectionString);

            var managedMqttClientOptions = new ManagedMqttClientOptionsBuilder()
                                           .WithAutoReconnectDelay(_defaultReconnectTime)
                                           .WithClientOptions(mqttClientOptions)
                                           .Build();

            return(new MqttConfiguration(name, managedMqttClientOptions));
        }
Exemplo n.º 2
0
        private IMqttClientOptions GetMqttClientOptions(MqttConnectionString connectionString)
        {
            var mqttClientOptionsBuilder = new MqttClientOptionsBuilder()
                                           .WithClientId(connectionString.ClientId)
                                           .WithTcpServer(connectionString.Server, connectionString.Port);

            if (!string.IsNullOrEmpty(connectionString.Username) || !string.IsNullOrEmpty(connectionString.Password))
            {
                _logger.LogTrace($"Using authentication, username: '******'");
                mqttClientOptionsBuilder = mqttClientOptionsBuilder.WithCredentials(connectionString.Username, connectionString.Password);
            }

            if (connectionString.Tls)
            {
                var certificates = new List <byte[]>();
                if (connectionString.Certificate != null)
                {
                    using (var cert = new X509Certificate(connectionString.Certificate))
                    {
                        var serializedServerCertificate = cert.Export(X509ContentType.Cert);
                        certificates.Add(serializedServerCertificate);
                    }
                }

                mqttClientOptionsBuilder = mqttClientOptionsBuilder.WithTls(new MqttClientOptionsBuilderTlsParameters
                {
                    UseTls       = true,
                    Certificates = certificates,
#if DEBUG
                    CertificateValidationCallback = (X509Certificate x, X509Chain y, SslPolicyErrors z, IMqttClientOptions o) =>
                    {
                        return(true);
                    },
#endif
                    AllowUntrustedCertificates        = false,
                    IgnoreCertificateChainErrors      = false,
                    IgnoreCertificateRevocationErrors = false
                });
            }

            return(mqttClientOptionsBuilder.Build());
        }
        private IMqttClientOptions GetMqttClientOptions(MqttConnectionString connectionString)
        {
            var mqttClientOptionsBuilder = new MqttClientOptionsBuilder()
                                           .WithClientId(connectionString.ClientId)
                                           .WithTcpServer(connectionString.Server, connectionString.Port);

            if (!string.IsNullOrEmpty(connectionString.Username) || !string.IsNullOrEmpty(connectionString.Password))
            {
                _logger.LogTrace($"Using authentication, username: '******'");
                mqttClientOptionsBuilder = mqttClientOptionsBuilder.WithCredentials(connectionString.Username, connectionString.Password);
            }

            if (connectionString.Tls)
            {
                // Need to implement TLS verification sometime
                mqttClientOptionsBuilder = mqttClientOptionsBuilder.WithTls(true, false, false);
            }

            return(mqttClientOptionsBuilder.Build());
        }
        private IMqttClientOptions GetMqttClientOptions(MqttConnectionString connectionString)
        {
            var mqttClientOptionsBuilder = new MqttClientOptionsBuilder()
                                           .WithClientId(connectionString.ClientId)
                                           .WithTcpServer(connectionString.Server, connectionString.Port);

            if (!string.IsNullOrEmpty(connectionString.Username) || !string.IsNullOrEmpty(connectionString.Password))
            {
                _logger.LogTrace($"Using authentication, username: '******'");
                mqttClientOptionsBuilder = mqttClientOptionsBuilder.WithCredentials(connectionString.Username, connectionString.Password);
            }

            if (connectionString.Tls)
            {
                var certificates = new List <X509Certificate>();
                if (connectionString.Certificate != null)
                {
                    using (var cert = new X509Certificate(connectionString.Certificate))
                    {
                        certificates.Add(cert);
                    }
                }

                mqttClientOptionsBuilder = mqttClientOptionsBuilder.WithTls(new MqttClientOptionsBuilderTlsParameters
                {
                    UseTls       = true,
                    Certificates = certificates,

                    AllowUntrustedCertificates        = false,
                    IgnoreCertificateChainErrors      = false,
                    IgnoreCertificateRevocationErrors = false
                });
            }

            return(mqttClientOptionsBuilder.Build());
        }