From 9f23105dcf8cd396eeb26596783c1e7ceae7d99e Mon Sep 17 00:00:00 2001 From: Cheena Malhotra Date: Fri, 21 Aug 2020 11:41:56 -0700 Subject: [PATCH] [Release/2.0] Fix configSection collision with System.Data.SqlClient type (#701) --- ...uthenticationProviderManager.NetCoreApp.cs | 38 +++++++++++++++++-- .../SqlAuthenticationProviderManager.cs | 37 ++++++++++++++++-- tools/props/Versions.props | 5 ++- 3 files changed, 73 insertions(+), 7 deletions(-) diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlAuthenticationProviderManager.NetCoreApp.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlAuthenticationProviderManager.NetCoreApp.cs index 3d6f135df1..e343a7ec39 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlAuthenticationProviderManager.NetCoreApp.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlAuthenticationProviderManager.NetCoreApp.cs @@ -20,12 +20,18 @@ static SqlAuthenticationProviderManager() try { - configurationSection = (SqlAuthenticationProviderConfigurationSection)ConfigurationManager.GetSection(SqlAuthenticationProviderConfigurationSection.Name); + // New configuration section for Microsoft.Data.SqlClient accepted to avoid conflicts with older one. + configurationSection = FetchConfigurationSection(SqlClientAuthenticationProviderConfigurationSection.Name); + if (null == configurationSection) + { + // If section is not yet found, try with old Configuration Section name for backwards compatibility + configurationSection = FetchConfigurationSection(SqlAuthenticationProviderConfigurationSection.Name); + } } catch (ConfigurationErrorsException e) { // Don't throw an error for invalid config files - SqlClientEventSource.Log.TraceEvent("Unable to load custom SqlAuthenticationProviders. ConfigurationManager failed to load due to configuration errors: {0}", e); + SqlClientEventSource.Log.TraceEvent("Unable to load custom SqlAuthenticationProviders or SqlClientAuthenticationProviders. ConfigurationManager failed to load due to configuration errors: {0}", e); } Instance = new SqlAuthenticationProviderManager(configurationSection); @@ -48,7 +54,7 @@ public SqlAuthenticationProviderManager(SqlAuthenticationProviderConfigurationSe if (configSection == null) { - _sqlAuthLogger.LogInfo(_typeName, methodName, "No SqlAuthProviders configuration section found."); + _sqlAuthLogger.LogInfo(_typeName, methodName, "Neither SqlClientAuthenticationProviders nor SqlAuthenticationProviders configuration section found."); return; } @@ -104,6 +110,24 @@ public SqlAuthenticationProviderManager(SqlAuthenticationProviderConfigurationSe } } + private static T FetchConfigurationSection(string name) + { + Type t = typeof(T); + object section = ConfigurationManager.GetSection(name); + if (null != section) + { + if (section is ConfigurationSection configSection && configSection.GetType() == t) + { + return (T)section; + } + else + { + SqlClientEventSource.Log.TraceEvent("Found a custom {0} configuration but it is not of type {1}.", name, t.FullName); + } + } + return default; + } + private static SqlAuthenticationMethod AuthenticationEnumFromString(string authentication) { switch (authentication.ToLowerInvariant()) @@ -140,5 +164,13 @@ internal class SqlAuthenticationProviderConfigurationSection : ConfigurationSect [ConfigurationProperty("initializerType")] public string InitializerType => base["initializerType"] as string; } + + /// + /// The configuration section definition for reading app.config. + /// + internal class SqlClientAuthenticationProviderConfigurationSection : SqlAuthenticationProviderConfigurationSection + { + public new const string Name = "SqlClientAuthenticationProviders"; + } } } diff --git a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlAuthenticationProviderManager.cs b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlAuthenticationProviderManager.cs index de50e441b8..869381e0bd 100644 --- a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlAuthenticationProviderManager.cs +++ b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlAuthenticationProviderManager.cs @@ -28,12 +28,18 @@ static SqlAuthenticationProviderManager() SqlAuthenticationProviderConfigurationSection configurationSection = null; try { - configurationSection = (SqlAuthenticationProviderConfigurationSection)ConfigurationManager.GetSection(SqlAuthenticationProviderConfigurationSection.Name); + // New configuration section for Microsoft.Data.SqlClient accepted to avoid conflicts with older one. + configurationSection = FetchConfigurationSection(SqlClientAuthenticationProviderConfigurationSection.Name); + if (null == configurationSection) + { + // If section is not yet found, try with old Configuration Section name for backwards compatibility + configurationSection = FetchConfigurationSection(SqlAuthenticationProviderConfigurationSection.Name); + } } catch (ConfigurationErrorsException e) { // Don't throw an error for invalid config files - SqlClientEventSource.Log.TraceEvent("Unable to load custom SqlAuthenticationProviders. ConfigurationManager failed to load due to configuration errors: {0}", e); + SqlClientEventSource.Log.TraceEvent("Unable to load custom SqlAuthenticationProviders or SqlClientAuthenticationProviders. ConfigurationManager failed to load due to configuration errors: {0}", e); } Instance = new SqlAuthenticationProviderManager(configurationSection); Instance.SetProvider(SqlAuthenticationMethod.ActiveDirectoryIntegrated, activeDirectoryAuthProvider); @@ -62,7 +68,7 @@ public SqlAuthenticationProviderManager(SqlAuthenticationProviderConfigurationSe if (configSection == null) { - _sqlAuthLogger.LogInfo(_typeName, methodName, "No SqlAuthProviders configuration section found."); + _sqlAuthLogger.LogInfo(_typeName, methodName, "Neither SqlClientAuthenticationProviders nor SqlAuthenticationProviders configuration section found."); return; } @@ -164,6 +170,24 @@ public bool SetProvider(SqlAuthenticationMethod authenticationMethod, SqlAuthent return true; } + private static T FetchConfigurationSection(string name) + { + Type t = typeof(T); + object section = ConfigurationManager.GetSection(name); + if (null != section) + { + if (section is ConfigurationSection configSection && configSection.GetType() == t) + { + return (T)section; + } + else + { + SqlClientEventSource.Log.TraceEvent("Found a custom {0} configuration but it is not of type {1}.", name, t.FullName); + } + } + return default; + } + private static SqlAuthenticationMethod AuthenticationEnumFromString(string authentication) { switch (authentication.ToLowerInvariant()) @@ -209,6 +233,13 @@ internal class SqlAuthenticationProviderConfigurationSection : ConfigurationSect public string InitializerType => base["initializerType"] as string; } + /// + /// The configuration section definition for reading app.config. + /// + internal class SqlClientAuthenticationProviderConfigurationSection : SqlAuthenticationProviderConfigurationSection + { + public new const string Name = "SqlClientAuthenticationProviders"; + } /// public abstract class SqlAuthenticationInitializer diff --git a/tools/props/Versions.props b/tools/props/Versions.props index 817a1dbc1a..086c8adb91 100644 --- a/tools/props/Versions.props +++ b/tools/props/Versions.props @@ -2,7 +2,10 @@ 1.0.0.0 - $(AssemblyFileVersion) + + + + 2.0.20168.4 1.0.0-beta.18578.2 2.0.0-dev $(NugetPackageVersion)