Skip to content

Commit

Permalink
[Release/2.0] Fix configSection collision with System.Data.SqlClient …
Browse files Browse the repository at this point in the history
…type (#701)
  • Loading branch information
cheenamalhotra authored Aug 21, 2020
1 parent 9ce5e66 commit 9f23105
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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>(SqlClientAuthenticationProviderConfigurationSection.Name);
if (null == configurationSection)
{
// If section is not yet found, try with old Configuration Section name for backwards compatibility
configurationSection = FetchConfigurationSection<SqlAuthenticationProviderConfigurationSection>(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);
Expand All @@ -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;
}

Expand Down Expand Up @@ -104,6 +110,24 @@ public SqlAuthenticationProviderManager(SqlAuthenticationProviderConfigurationSe
}
}

private static T FetchConfigurationSection<T>(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())
Expand Down Expand Up @@ -140,5 +164,13 @@ internal class SqlAuthenticationProviderConfigurationSection : ConfigurationSect
[ConfigurationProperty("initializerType")]
public string InitializerType => base["initializerType"] as string;
}

/// <summary>
/// The configuration section definition for reading app.config.
/// </summary>
internal class SqlClientAuthenticationProviderConfigurationSection : SqlAuthenticationProviderConfigurationSection
{
public new const string Name = "SqlClientAuthenticationProviders";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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>(SqlClientAuthenticationProviderConfigurationSection.Name);
if (null == configurationSection)
{
// If section is not yet found, try with old Configuration Section name for backwards compatibility
configurationSection = FetchConfigurationSection<SqlAuthenticationProviderConfigurationSection>(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);
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -164,6 +170,24 @@ public bool SetProvider(SqlAuthenticationMethod authenticationMethod, SqlAuthent
return true;
}

private static T FetchConfigurationSection<T>(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())
Expand Down Expand Up @@ -209,6 +233,13 @@ internal class SqlAuthenticationProviderConfigurationSection : ConfigurationSect
public string InitializerType => base["initializerType"] as string;
}

/// <summary>
/// The configuration section definition for reading app.config.
/// </summary>
internal class SqlClientAuthenticationProviderConfigurationSection : SqlAuthenticationProviderConfigurationSection
{
public new const string Name = "SqlClientAuthenticationProviders";
}

/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlAuthenticationInitializer.xml' path='docs/members[@name="SqlAuthenticationInitializer"]/SqlAuthenticationInitializer/*'/>
public abstract class SqlAuthenticationInitializer
Expand Down
5 changes: 4 additions & 1 deletion tools/props/Versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
<Project ToolsVersion="Latest" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<AssemblyFileVersion Condition="'$(AssemblyFileVersion)' == ''">1.0.0.0</AssemblyFileVersion>
<AssemblyVersion>$(AssemblyFileVersion)</AssemblyVersion>
<!-- This Assembly version corresponds to version of Microsoft.Data.SqlClient v2.0.0 Assembly. -->
<!-- Should only be changed in future when a non-backwards compatible driver is released. -->
<!-- Future Assembly Version values shall be Major.0.0.0; e.g. 3.0.0.0 -->
<AssemblyVersion>2.0.20168.4</AssemblyVersion>
<MicrosoftDotNetGenAPIPackageVersion>1.0.0-beta.18578.2</MicrosoftDotNetGenAPIPackageVersion>
<NugetPackageVersion Condition="'$(NugetPackageVersion)' == ''">2.0.0-dev</NugetPackageVersion>
<Version>$(NugetPackageVersion)</Version>
Expand Down

0 comments on commit 9f23105

Please sign in to comment.