@@ -783,9 +783,9 @@ async Task RawOpen(SslMode sslMode, NpgsqlTimeout timeout, bool async, Cancellat
783783
784784 IsSecure = false ;
785785
786- if ( Settings . SslNegotiation == SslNegotiation . Direct )
786+ if ( GetSslNegotiation ( Settings ) == SslNegotiation . Direct )
787787 {
788- // We already check that in NpgsqlConnectionStringBuilder.PostProcessAndValidate, but just on the off case
788+ // We already check that in NpgsqlConnectionStringBuilder.PostProcessAndValidate, but since we also allow environment variables...
789789 if ( Settings . SslMode is not SslMode . Require and not SslMode . VerifyCA and not SslMode . VerifyFull )
790790 throw new ArgumentException( "SSL Mode has to be Require or higher to be used with direct SSL Negotiation" ) ;
791791 await DataSource. TransportSecurityHandler . NegotiateEncryption ( async, this , sslMode , timeout , cancellationToken ) . ConfigureAwait ( false ) ;
@@ -836,6 +836,20 @@ async Task RawOpen(SslMode sslMode, NpgsqlTimeout timeout, bool async, Cancellat
836836 }
837837 }
838838
839+ static SslNegotiation GetSslNegotiation( NpgsqlConnectionStringBuilder settings )
840+ {
841+ if ( settings . UserProvidedSslNegotiation is { } userProvidedSslNegotiation )
842+ return userProvidedSslNegotiation;
843+
844+ if ( PostgresEnvironment . SslNegotiation is { } sslNegotiationEnv )
845+ {
846+ if ( Enum . TryParse < SslNegotiation > ( sslNegotiationEnv , ignoreCase : true , out var sslNegotiation ) )
847+ return sslNegotiation;
848+ }
849+
850+ return SslNegotiation. Postgres ;
851+ }
852+
839853 internal async Task NegotiateEncryption( SslMode sslMode , NpgsqlTimeout timeout , bool async , CancellationToken cancellationToken )
840854 {
841855 var clientCertificates = new X509Certificate2Collection( ) ;
0 commit comments