1010
1111namespace Microsoft . Data . SqlClient
1212{
13-
1413 /// <summary>
1514 /// Authentication provider manager.
1615 /// </summary>
17- internal class SqlAuthenticationProviderManager
16+ internal partial class SqlAuthenticationProviderManager
1817 {
1918 private const string ActiveDirectoryPassword = "active directory password" ;
2019 private const string ActiveDirectoryIntegrated = "active directory integrated" ;
2120 private const string ActiveDirectoryInteractive = "active directory interactive" ;
2221
23- static SqlAuthenticationProviderManager ( )
24- {
25- var activeDirectoryAuthNativeProvider = new ActiveDirectoryNativeAuthenticationProvider ( ) ;
26- SqlAuthenticationProviderConfigurationSection configurationSection ;
27- try
28- {
29- configurationSection = ( SqlAuthenticationProviderConfigurationSection ) ConfigurationManager . GetSection ( SqlAuthenticationProviderConfigurationSection . Name ) ;
30- }
31- catch ( ConfigurationErrorsException e )
32- {
33- throw SQL . CannotGetAuthProviderConfig ( e ) ;
34- }
35- Instance = new SqlAuthenticationProviderManager ( configurationSection ) ;
36- Instance . SetProvider ( SqlAuthenticationMethod . ActiveDirectoryPassword , activeDirectoryAuthNativeProvider ) ;
37- }
38- public static readonly SqlAuthenticationProviderManager Instance ;
39-
4022 private readonly string _typeName ;
41- private readonly SqlAuthenticationInitializer _initializer ;
4223 private readonly IReadOnlyCollection < SqlAuthenticationMethod > _authenticationsWithAppSpecifiedProvider ;
4324 private readonly ConcurrentDictionary < SqlAuthenticationMethod , SqlAuthenticationProvider > _providers ;
4425 private readonly SqlClientLogger _sqlAuthLogger = new SqlClientLogger ( ) ;
4526
27+ public static readonly SqlAuthenticationProviderManager Instance ;
28+
4629 /// <summary>
4730 /// Constructor.
4831 /// </summary>
49- public SqlAuthenticationProviderManager ( SqlAuthenticationProviderConfigurationSection configSection )
32+ public SqlAuthenticationProviderManager ( )
5033 {
5134 _typeName = GetType ( ) . Name ;
52- var methodName = "Ctor" ;
5335 _providers = new ConcurrentDictionary < SqlAuthenticationMethod , SqlAuthenticationProvider > ( ) ;
54- var authenticationsWithAppSpecifiedProvider = new HashSet < SqlAuthenticationMethod > ( ) ;
55- _authenticationsWithAppSpecifiedProvider = authenticationsWithAppSpecifiedProvider ;
56-
57- if ( configSection == null )
58- {
59- _sqlAuthLogger . LogInfo ( _typeName , methodName , "No SqlAuthProviders configuration section found." ) ;
60- return ;
61- }
62-
63- // Create user-defined auth initializer, if any.
64- //
65- if ( ! string . IsNullOrEmpty ( configSection . InitializerType ) )
66- {
67- try
68- {
69- var initializerType = Type . GetType ( configSection . InitializerType , true ) ;
70- _initializer = ( SqlAuthenticationInitializer ) Activator . CreateInstance ( initializerType ) ;
71- _initializer . Initialize ( ) ;
72- }
73- catch ( Exception e )
74- {
75- throw SQL . CannotCreateSqlAuthInitializer ( configSection . InitializerType , e ) ;
76- }
77- _sqlAuthLogger . LogInfo ( _typeName , methodName , "Created user-defined SqlAuthenticationInitializer." ) ;
78- }
79- else
80- {
81- _sqlAuthLogger . LogInfo ( _typeName , methodName , "No user-defined SqlAuthenticationInitializer found." ) ;
82- }
83-
84- // add user-defined providers, if any.
85- //
86- if ( configSection . Providers != null && configSection . Providers . Count > 0 )
87- {
88- foreach ( ProviderSettings providerSettings in configSection . Providers )
89- {
90- SqlAuthenticationMethod authentication = AuthenticationEnumFromString ( providerSettings . Name ) ;
91- SqlAuthenticationProvider provider ;
92- try
93- {
94- var providerType = Type . GetType ( providerSettings . Type , true ) ;
95- provider = ( SqlAuthenticationProvider ) Activator . CreateInstance ( providerType ) ;
96- }
97- catch ( Exception e )
98- {
99- throw SQL . CannotCreateAuthProvider ( authentication . ToString ( ) , providerSettings . Type , e ) ;
100- }
101- if ( ! provider . IsSupported ( authentication ) )
102- throw SQL . UnsupportedAuthenticationByProvider ( authentication . ToString ( ) , providerSettings . Type ) ;
103-
104- _providers [ authentication ] = provider ;
105- authenticationsWithAppSpecifiedProvider . Add ( authentication ) ;
106- _sqlAuthLogger . LogInfo ( _typeName , methodName , string . Format ( "Added user-defined auth provider: {0} for authentication {1}." , providerSettings ? . Type , authentication ) ) ;
107- }
108- }
109- else
110- {
111- _sqlAuthLogger . LogInfo ( _typeName , methodName , "No user-defined auth providers." ) ;
112- }
36+ _authenticationsWithAppSpecifiedProvider = new HashSet < SqlAuthenticationMethod > ( ) ;
37+ _sqlAuthLogger . LogInfo ( _typeName , "Ctor" , "No SqlAuthProviders configuration section found." ) ;
11338 }
11439
11540 /// <summary>
@@ -156,17 +81,6 @@ public bool SetProvider(SqlAuthenticationMethod authenticationMethod, SqlAuthent
15681 return true ;
15782 }
15883
159- private static SqlAuthenticationMethod AuthenticationEnumFromString ( string authentication )
160- {
161- switch ( authentication . ToLowerInvariant ( ) )
162- {
163- case ActiveDirectoryPassword :
164- return SqlAuthenticationMethod . ActiveDirectoryPassword ;
165- default :
166- throw SQL . UnsupportedAuthentication ( authentication ) ;
167- }
168- }
169-
17084 private static string GetProviderType ( SqlAuthenticationProvider provider )
17185 {
17286 if ( provider == null )
@@ -175,26 +89,6 @@ private static string GetProviderType(SqlAuthenticationProvider provider)
17589 }
17690 }
17791
178- /// <summary>
179- /// The configuration section definition for reading app.config.
180- /// </summary>
181- internal class SqlAuthenticationProviderConfigurationSection : ConfigurationSection
182- {
183- public const string Name = "SqlAuthenticationProviders" ;
184-
185- /// <summary>
186- /// User-defined auth providers.
187- /// </summary>
188- [ ConfigurationProperty ( "providers" ) ]
189- public ProviderSettingsCollection Providers => ( ProviderSettingsCollection ) base [ "providers" ] ;
190-
191- /// <summary>
192- /// User-defined initializer.
193- /// </summary>
194- [ ConfigurationProperty ( "initializerType" ) ]
195- public string InitializerType => base [ "initializerType" ] as string ;
196- }
197-
19892 /// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlAuthenticationInitializer.xml' path='docs/members[@name="SqlAuthenticationInitializer"]/SqlAuthenticationInitializer/*'/>
19993 public abstract class SqlAuthenticationInitializer
20094 {
0 commit comments