Skip to content

Commit

Permalink
Update config Section name for Microsoft.Data.SqlClient (dotnet#702)
Browse files Browse the repository at this point in the history
  • Loading branch information
cheenamalhotra authored and TrayanZapryanov committed Aug 31, 2020
1 parent 5a46e3d commit be08e14
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 7 deletions.
Expand Up @@ -20,12 +20,18 @@ static SqlAuthenticationProviderManager()

try
{
configurationSection = (SqlAuthenticationProviderConfigurationSection)ConfigurationManager.GetSection(SqlAuthenticationProviderConfigurationSection.Name);
// New configuration section "SqlClientAuthenticationProviders" for Microsoft.Data.SqlClient accepted to avoid conflicts with older one.
configurationSection = FetchConfigurationSection<SqlClientAuthenticationProviderConfigurationSection>(SqlClientAuthenticationProviderConfigurationSection.Name);
if (null == configurationSection)
{
// If configuration 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 @@ -49,7 +55,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 @@ -105,6 +111,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 @@ -143,5 +167,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";
}
}
}
Expand Up @@ -27,12 +27,18 @@ static SqlAuthenticationProviderManager()
SqlAuthenticationProviderConfigurationSection configurationSection = null;
try
{
configurationSection = (SqlAuthenticationProviderConfigurationSection)ConfigurationManager.GetSection(SqlAuthenticationProviderConfigurationSection.Name);
// New configuration section "SqlClientAuthenticationProviders" for Microsoft.Data.SqlClient accepted to avoid conflicts with older one.
configurationSection = FetchConfigurationSection<SqlClientAuthenticationProviderConfigurationSection>(SqlClientAuthenticationProviderConfigurationSection.Name);
if (null == configurationSection)
{
// If configuration 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 @@ -211,11 +235,17 @@ 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
{

/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlAuthenticationInitializer.xml' path='docs/members[@name="SqlAuthenticationInitializer"]/Initialize/*'/>
public abstract void Initialize();
}
Expand Down

0 comments on commit be08e14

Please sign in to comment.