@@ -684,29 +684,30 @@ private ConnectionString BuildResolvedConnectionString(ConnectionStringScheme re
684684 . AllKeys
685685 . SelectMany ( key => resolvedOptions
686686 . GetValues ( key )
687- . Select ( value => $ "{ key } ={ Uri . EscapeDataString ( value ) } ") ) ) ;
687+ . Select ( value => $ "{ key } ={ EscapeOptionValue ( key , value ) } ") ) ) ;
688688
689689 if ( mergedOptions . Count > 0 )
690690 {
691691 connectionString += "?" + string . Join ( "&" , mergedOptions ) ;
692692 }
693693
694694 return new ConnectionString ( connectionString , isResolved : true ) ;
695+
696+ string EscapeOptionValue ( string key , string value )
697+ => string . Equals ( key , "authmechanismproperties" , StringComparison . OrdinalIgnoreCase ) ? value : Uri . EscapeDataString ( value ) ;
698+
695699 }
696700
697701 private void ExtractScheme ( Match match )
698702 {
699703 var schemeGroup = match . Groups [ "scheme" ] ;
700- if ( schemeGroup . Success )
704+ if ( schemeGroup . Success && schemeGroup . Value == "mongodb+srv" )
701705 {
702- if ( schemeGroup . Value == "mongodb+srv" )
706+ _scheme = ConnectionStringScheme . MongoDBPlusSrv ;
707+ if ( ! _tls . HasValue )
703708 {
704- _scheme = ConnectionStringScheme . MongoDBPlusSrv ;
705- if ( ! _tls . HasValue )
706- {
707- _tls = true ;
708- _allOptions . Add ( "tls" , "true" ) ;
709- }
709+ _tls = true ;
710+ _allOptions . Add ( "tls" , "true" ) ;
710711 }
711712 }
712713 }
@@ -761,7 +762,13 @@ private void ExtractOptions(Match match)
761762 var parts = option . Value . Split ( '=' ) ;
762763 var name = parts [ 0 ] . Trim ( ) ;
763764 var value = parts [ 1 ] . Trim ( ) ;
764- _allOptions . Add ( name , Uri . UnescapeDataString ( value ) ) ;
765+ // Should not decode authmechanismproperties before splitting by separator.
766+ if ( ! string . Equals ( name , "authmechanismproperties" , StringComparison . OrdinalIgnoreCase ) )
767+ {
768+ value = Uri . UnescapeDataString ( value ) ;
769+ }
770+
771+ _allOptions . Add ( name , value ) ;
765772 ParseOption ( name , value ) ;
766773 }
767774 }
@@ -905,12 +912,6 @@ string ProtectConnectionString(string connectionString)
905912
906913 private void ParseOption ( string name , string value )
907914 {
908- // Should not decode authmechanismproperties before splitting by separator.
909- if ( ! string . Equals ( name , "authmechanismproperties" , StringComparison . OrdinalIgnoreCase ) )
910- {
911- value = Uri . UnescapeDataString ( value ) ;
912- }
913-
914915 switch ( name . ToLowerInvariant ( ) )
915916 {
916917 case "appname" :
0 commit comments