diff --git a/Source/MQTTnet/Client/Options/MqttClientCaFileCertificateValidationHandler.cs b/Source/MQTTnet/Client/Options/MqttClientCaFileCertificateValidationHandler.cs index 60fce664b..0aad9d0a2 100644 --- a/Source/MQTTnet/Client/Options/MqttClientCaFileCertificateValidationHandler.cs +++ b/Source/MQTTnet/Client/Options/MqttClientCaFileCertificateValidationHandler.cs @@ -22,8 +22,8 @@ internal static bool Handle(MqttClientCertificateValidationEventArgs cvArgs) string caFile = cvArgs.ClientOptions.TlsOptions.CaFile; if (!string.IsNullOrEmpty(caFile)) { +#if NET6_0_OR_GREATER X509Certificate2Collection caCerts = new X509Certificate2Collection(); -#if NET6_0_OR_GREATER caCerts.ImportFromPemFile(caFile); cvArgs.Chain.ChainPolicy.TrustMode = X509ChainTrustMode.CustomRootTrust; cvArgs.Chain.ChainPolicy.CustomTrustStore.AddRange(caCerts); @@ -36,21 +36,14 @@ internal static bool Handle(MqttClientCertificateValidationEventArgs cvArgs) } return res; #else - return false; + throw new InvalidOperationException("WithCaFile requires .NET6 or greater"); #endif - } else { -#if NET6_0_OR_GREATER - Trace.TraceWarning($"CaFile '{caFile}' not found."); -#endif return false; } } -#if NET6_0_OR_GREATER - Trace.TraceWarning(cvArgs.SslPolicyErrors.ToString()); -#endif return false; } } diff --git a/Source/MQTTnet/Client/Options/MqttClientTlsOptionsBuilder.cs b/Source/MQTTnet/Client/Options/MqttClientTlsOptionsBuilder.cs index 8da97bbc2..5dc5d96ac 100644 --- a/Source/MQTTnet/Client/Options/MqttClientTlsOptionsBuilder.cs +++ b/Source/MQTTnet/Client/Options/MqttClientTlsOptionsBuilder.cs @@ -144,8 +144,8 @@ public MqttClientTlsOptionsBuilder WithCaFile(string caFile) { throw new InvalidOperationException("WithCaFile cannot be used with a custom Certificate Validation Handler"); } - _tlsOptions.CaFile = caFile; WithCertificateValidationHandler(MqttClientCaFileCertificateValidationHandler.Handle); + _tlsOptions.CaFile = caFile; return this; } #endif