From dca5f8f6b538b684e97c412175732f3407cfb2a4 Mon Sep 17 00:00:00 2001 From: Cheena Malhotra Date: Wed, 23 Sep 2020 02:15:58 -0700 Subject: [PATCH 1/4] Add support for user-defined ApplicationClientId --- ...tionClientIdAzureAuthenticationProvider.cs | 26 +++++++++++++++ .../ActiveDirectoryAuthenticationProvider.xml | 23 +++++++++++-- .../netcore/ref/Microsoft.Data.SqlClient.cs | 4 ++- ...uthenticationProviderManager.NetCoreApp.cs | 30 +++++++++++++++-- ...thenticationProviderManager.NetStandard.cs | 2 +- .../SqlAuthenticationProviderManager.cs | 1 + .../src/Microsoft/Data/SqlClient/SqlUtil.cs | 5 +++ .../netcore/src/Resources/Strings.Designer.cs | 9 +++++ .../netcore/src/Resources/Strings.resx | 5 ++- .../netfx/ref/Microsoft.Data.SqlClient.cs | 4 ++- .../SqlAuthenticationProviderManager.cs | 33 ++++++++++++++++--- .../src/Microsoft/Data/SqlClient/SqlUtil.cs | 9 +++-- .../netfx/src/Resources/Strings.Designer.cs | 9 +++++ .../netfx/src/Resources/Strings.resx | 5 ++- .../ActiveDirectoryAuthenticationProvider.cs | 16 ++++++--- 15 files changed, 161 insertions(+), 20 deletions(-) create mode 100644 doc/samples/ApplicationClientIdAzureAuthenticationProvider.cs diff --git a/doc/samples/ApplicationClientIdAzureAuthenticationProvider.cs b/doc/samples/ApplicationClientIdAzureAuthenticationProvider.cs new file mode 100644 index 0000000000..f206a1c29e --- /dev/null +++ b/doc/samples/ApplicationClientIdAzureAuthenticationProvider.cs @@ -0,0 +1,26 @@ +// +using System; +using Microsoft.Data.SqlClient; + +namespace CustomAuthenticationProviderExamples +{ + public class Program + { + public static void Main() + { + // Supported for all authentication modes supported by ActiveDirectoryAuthenticationProvider + ActiveDirectoryAuthenticationProvider provider = new ActiveDirectoryAuthenticationProvider(""); + if (provider.IsSupported(SqlAuthenticationMethod.ActiveDirectoryInteractive)) + { + SqlAuthenticationProvider.SetProvider(SqlAuthenticationMethod.ActiveDirectoryInteractive, provider); + } + + using (SqlConnection sqlConnection = new SqlConnection("Server=.database.windows.net;Authentication=Active Directory Interactive;Database=;")) + { + sqlConnection.Open(); + Console.WriteLine("Connected successfully!"); + } + } + } +} +// diff --git a/doc/snippets/Microsoft.Data.SqlClient/ActiveDirectoryAuthenticationProvider.xml b/doc/snippets/Microsoft.Data.SqlClient/ActiveDirectoryAuthenticationProvider.xml index 49a2e5616a..e136306055 100644 --- a/doc/snippets/Microsoft.Data.SqlClient/ActiveDirectoryAuthenticationProvider.xml +++ b/doc/snippets/Microsoft.Data.SqlClient/ActiveDirectoryAuthenticationProvider.xml @@ -11,11 +11,30 @@ - The callback method to be used when performing 'Active Directory Device Code Flow' authentication. + Client Application Id to be used for acquiring access token for federated authentication. The driver uses it's own application client id by default. - Initializes the class with the provided device code flow callback method. + Initializes the class with the provided application client id. + + + + + + + The callback method to be used when performing 'Active Directory Device Code Flow' authentication. + (Optional) Client Application Id to be used for acquiring access token for federated authentication. The driver uses it's own application client id by default. + + Initializes the class with the provided device code flow callback method and application client id. + + The Active Directory authentication parameters passed to authentication providers. Acquires a security token from the authority. diff --git a/src/Microsoft.Data.SqlClient/netcore/ref/Microsoft.Data.SqlClient.cs b/src/Microsoft.Data.SqlClient/netcore/ref/Microsoft.Data.SqlClient.cs index 3a75ca8d09..f08e1835ed 100644 --- a/src/Microsoft.Data.SqlClient/netcore/ref/Microsoft.Data.SqlClient.cs +++ b/src/Microsoft.Data.SqlClient/netcore/ref/Microsoft.Data.SqlClient.cs @@ -36,7 +36,9 @@ public sealed partial class ActiveDirectoryAuthenticationProvider : SqlAuthentic /// public ActiveDirectoryAuthenticationProvider() { } /// - public ActiveDirectoryAuthenticationProvider(System.Func deviceCodeFlowCallbackMethod) { } + public ActiveDirectoryAuthenticationProvider(string applicationClientId) { } + /// + public ActiveDirectoryAuthenticationProvider(System.Func deviceCodeFlowCallbackMethod, string applicationClientId = null) { } /// public override System.Threading.Tasks.Task AcquireTokenAsync(SqlAuthenticationParameters parameters) { throw null; } /// 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 bf7c1632d1..684f9210be 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 @@ -15,7 +15,6 @@ internal partial class SqlAuthenticationProviderManager static SqlAuthenticationProviderManager() { - var activeDirectoryAuthProvider = new ActiveDirectoryAuthenticationProvider(); SqlAuthenticationProviderConfigurationSection configurationSection = null; try @@ -35,6 +34,7 @@ static SqlAuthenticationProviderManager() } Instance = new SqlAuthenticationProviderManager(configurationSection); + var activeDirectoryAuthProvider = new ActiveDirectoryAuthenticationProvider(Instance._applicationClientId); Instance.SetProvider(SqlAuthenticationMethod.ActiveDirectoryIntegrated, activeDirectoryAuthProvider); Instance.SetProvider(SqlAuthenticationMethod.ActiveDirectoryPassword, activeDirectoryAuthProvider); Instance.SetProvider(SqlAuthenticationMethod.ActiveDirectoryInteractive, activeDirectoryAuthProvider); @@ -59,6 +59,24 @@ public SqlAuthenticationProviderManager(SqlAuthenticationProviderConfigurationSe return; } + if (!string.IsNullOrEmpty(configSection.ApplicationClientId)) + { + try + { + _applicationClientId = configSection.ApplicationClientId; + } + catch (Exception e) + { + throw SQL.CannotFetchApplicationClientId(configSection.ApplicationClientId, e); + } + + _sqlAuthLogger.LogInfo(_typeName, methodName, "Received user-defined Application Client Id"); + } + else + { + _sqlAuthLogger.LogInfo(_typeName, methodName, "No user-defined Application Client Id found."); + } + // Create user-defined auth initializer, if any. if (!string.IsNullOrEmpty(configSection.InitializerType)) { @@ -159,13 +177,19 @@ internal class SqlAuthenticationProviderConfigurationSection : ConfigurationSect /// User-defined auth providers. /// [ConfigurationProperty("providers")] - public ProviderSettingsCollection Providers => (ProviderSettingsCollection)base["providers"]; + public ProviderSettingsCollection Providers => (ProviderSettingsCollection)this["providers"]; /// /// User-defined initializer. /// [ConfigurationProperty("initializerType")] - public string InitializerType => base["initializerType"] as string; + public string InitializerType => this["initializerType"] as string; + + /// + /// Application Client Id + /// + [ConfigurationProperty("applicationClientId", IsRequired = false)] + public string ApplicationClientId => this["applicationClientId"] as string; } /// diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlAuthenticationProviderManager.NetStandard.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlAuthenticationProviderManager.NetStandard.cs index e3d5fd5e11..d283f58400 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlAuthenticationProviderManager.NetStandard.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlAuthenticationProviderManager.NetStandard.cs @@ -8,8 +8,8 @@ internal partial class SqlAuthenticationProviderManager { static SqlAuthenticationProviderManager() { - var activeDirectoryAuthProvider = new ActiveDirectoryAuthenticationProvider(); Instance = new SqlAuthenticationProviderManager(); + var activeDirectoryAuthProvider = new ActiveDirectoryAuthenticationProvider(Instance._applicationClientId); Instance.SetProvider(SqlAuthenticationMethod.ActiveDirectoryPassword, activeDirectoryAuthProvider); Instance.SetProvider(SqlAuthenticationMethod.ActiveDirectoryIntegrated, activeDirectoryAuthProvider); Instance.SetProvider(SqlAuthenticationMethod.ActiveDirectoryInteractive, activeDirectoryAuthProvider); diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlAuthenticationProviderManager.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlAuthenticationProviderManager.cs index 8d85802486..512f3d4b04 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlAuthenticationProviderManager.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlAuthenticationProviderManager.cs @@ -24,6 +24,7 @@ internal partial class SqlAuthenticationProviderManager private readonly IReadOnlyCollection _authenticationsWithAppSpecifiedProvider; private readonly ConcurrentDictionary _providers; private readonly SqlClientLogger _sqlAuthLogger = new SqlClientLogger(); + private readonly string _applicationClientId = ActiveDirectoryAuthentication.AdoClientId; public static readonly SqlAuthenticationProviderManager Instance; diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlUtil.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlUtil.cs index ebfe96b769..611fd5a3a5 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlUtil.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlUtil.cs @@ -432,6 +432,11 @@ internal static Exception CannotCreateSqlAuthInitializer(string type, Exception { return ADP.Argument(System.StringsHelper.GetString(Strings.SQL_CannotCreateAuthInitializer, type), e); } + + static internal Exception CannotFetchApplicationClientId(string type, Exception e) + { + return ADP.Argument(StringsHelper.GetString(Strings.SQL_CannotFetchApplicationClientId, type), e); + } internal static Exception CannotInitializeAuthProvider(string type, Exception e) { diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Resources/Strings.Designer.cs b/src/Microsoft.Data.SqlClient/netcore/src/Resources/Strings.Designer.cs index 438423fa71..d3a6f1ea52 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Resources/Strings.Designer.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Resources/Strings.Designer.cs @@ -2373,6 +2373,15 @@ internal static string SQL_CannotCreateNormalizer { } } + /// + /// Looks up a localized string similar to Failed to fetch application client id with type '{0}'.. + /// + internal static string SQL_CannotFetchApplicationClientId { + get { + return ResourceManager.GetString("SQL_CannotFetchApplicationClientId", resourceCulture); + } + } + /// /// Looks up a localized string similar to Cannot find an authentication provider for '{0}'.. /// diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Resources/Strings.resx b/src/Microsoft.Data.SqlClient/netcore/src/Resources/Strings.resx index b94391deac..34c22d3029 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Resources/Strings.resx +++ b/src/Microsoft.Data.SqlClient/netcore/src/Resources/Strings.resx @@ -1905,4 +1905,7 @@ Cannot use 'Authentication=Active Directory Device Code Flow', if the Credential property has been set. - + + Failed to fetch application client id with type '{0}'. + + \ No newline at end of file diff --git a/src/Microsoft.Data.SqlClient/netfx/ref/Microsoft.Data.SqlClient.cs b/src/Microsoft.Data.SqlClient/netfx/ref/Microsoft.Data.SqlClient.cs index 6f29fe813c..8fb8e1fed5 100644 --- a/src/Microsoft.Data.SqlClient/netfx/ref/Microsoft.Data.SqlClient.cs +++ b/src/Microsoft.Data.SqlClient/netfx/ref/Microsoft.Data.SqlClient.cs @@ -41,7 +41,9 @@ public sealed class ActiveDirectoryAuthenticationProvider : SqlAuthenticationPro /// public ActiveDirectoryAuthenticationProvider() { } /// - public ActiveDirectoryAuthenticationProvider(System.Func deviceCodeFlowCallbackMethod) { } + public ActiveDirectoryAuthenticationProvider(string applicationClientId) { } + /// + public ActiveDirectoryAuthenticationProvider(System.Func deviceCodeFlowCallbackMethod, string applicationClientId = null) { } /// public override System.Threading.Tasks.Task AcquireTokenAsync(SqlAuthenticationParameters parameters) { throw null; } /// 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 06bdc0bb6b..c0d9ac7d0d 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 @@ -23,7 +23,6 @@ internal class SqlAuthenticationProviderManager static SqlAuthenticationProviderManager() { - var activeDirectoryAuthProvider = new ActiveDirectoryAuthenticationProvider(); SqlAuthenticationProviderConfigurationSection configurationSection = null; try { @@ -41,6 +40,8 @@ static SqlAuthenticationProviderManager() SqlClientEventSource.Log.TryTraceEvent("Unable to load custom SqlAuthenticationProviders or SqlClientAuthenticationProviders. ConfigurationManager failed to load due to configuration errors: {0}", e); } Instance = new SqlAuthenticationProviderManager(configurationSection); + + var activeDirectoryAuthProvider = new ActiveDirectoryAuthenticationProvider(Instance._applicationClientId); Instance.SetProvider(SqlAuthenticationMethod.ActiveDirectoryIntegrated, activeDirectoryAuthProvider); Instance.SetProvider(SqlAuthenticationMethod.ActiveDirectoryPassword, activeDirectoryAuthProvider); Instance.SetProvider(SqlAuthenticationMethod.ActiveDirectoryInteractive, activeDirectoryAuthProvider); @@ -54,6 +55,7 @@ static SqlAuthenticationProviderManager() private readonly IReadOnlyCollection _authenticationsWithAppSpecifiedProvider; private readonly ConcurrentDictionary _providers; private readonly SqlClientLogger _sqlAuthLogger = new SqlClientLogger(); + private readonly string _applicationClientId = ActiveDirectoryAuthentication.AdoClientId; /// /// Constructor. @@ -72,8 +74,25 @@ public SqlAuthenticationProviderManager(SqlAuthenticationProviderConfigurationSe return; } + if (!string.IsNullOrEmpty(configSection.ApplicationClientId)) + { + try + { + _applicationClientId = configSection.ApplicationClientId; + } + catch (Exception e) + { + throw SQL.CannotFetchApplicationClientId(configSection.ApplicationClientId, e); + } + + _sqlAuthLogger.LogInfo(_typeName, methodName, "Received user-defined Application Client Id"); + } + else + { + _sqlAuthLogger.LogInfo(_typeName, methodName, "No user-defined Application Client Id found."); + } + // Create user-defined auth initializer, if any. - // if (!string.IsNullOrEmpty(configSection.InitializerType)) { try @@ -226,13 +245,19 @@ internal class SqlAuthenticationProviderConfigurationSection : ConfigurationSect /// User-defined auth providers. /// [ConfigurationProperty("providers")] - public ProviderSettingsCollection Providers => (ProviderSettingsCollection)base["providers"]; + public ProviderSettingsCollection Providers => (ProviderSettingsCollection)this["providers"]; /// /// User-defined initializer. /// [ConfigurationProperty("initializerType")] - public string InitializerType => base["initializerType"] as string; + public string InitializerType => this["initializerType"] as string; + + /// + /// Application Client Id + /// + [ConfigurationProperty("applicationClientId", IsRequired = false)] + public string ApplicationClientId => this["applicationClientId"] as string; } /// diff --git a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlUtil.cs b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlUtil.cs index 6318a1491f..ac29ff4f95 100644 --- a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlUtil.cs +++ b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlUtil.cs @@ -462,6 +462,11 @@ static internal Exception CannotCreateSqlAuthInitializer(string type, Exception return ADP.Argument(StringsHelper.GetString(Strings.SQL_CannotCreateAuthInitializer, type), e); } + static internal Exception CannotFetchApplicationClientId(string type, Exception e) + { + return ADP.Argument(StringsHelper.GetString(Strings.SQL_CannotFetchApplicationClientId, type), e); + } + static internal Exception CannotInitializeAuthProvider(string type, Exception e) { return ADP.InvalidOperation(StringsHelper.GetString(Strings.SQL_CannotInitializeAuthProvider, type), e); @@ -772,7 +777,7 @@ static internal Exception UDTUnexpectedResult(string exceptionText) static internal Exception CannotCompleteDelegatedTransactionWithOpenResults(SqlInternalConnectionTds internalConnection, bool marsOn) { SqlErrorCollection errors = new SqlErrorCollection(); - errors.Add(new SqlError(TdsEnums.TIMEOUT_EXPIRED, (byte)0x00, TdsEnums.MIN_ERROR_CLASS, null, (StringsHelper.GetString(Strings.ADP_OpenReaderExists, marsOn? ADP.Command : ADP.Connection)), "", 0, TdsEnums.SNI_WAIT_TIMEOUT)); + errors.Add(new SqlError(TdsEnums.TIMEOUT_EXPIRED, (byte)0x00, TdsEnums.MIN_ERROR_CLASS, null, (StringsHelper.GetString(Strings.ADP_OpenReaderExists, marsOn ? ADP.Command : ADP.Connection)), "", 0, TdsEnums.SNI_WAIT_TIMEOUT)); return SqlException.CreateException(errors, null, internalConnection); } static internal SysTx.TransactionPromotionException PromotionFailed(Exception inner) @@ -858,7 +863,7 @@ static internal Exception UDTInvalidSqlType(string typeName) { return ADP.Argument(StringsHelper.GetString(Strings.SQLUDT_InvalidSqlType, typeName)); } - + static internal Exception UDTInvalidSize(int maxSize, int maxSupportedSize) { throw ADP.ArgumentOutOfRange(StringsHelper.GetString(Strings.SQLUDT_InvalidSize, maxSize, maxSupportedSize)); diff --git a/src/Microsoft.Data.SqlClient/netfx/src/Resources/Strings.Designer.cs b/src/Microsoft.Data.SqlClient/netfx/src/Resources/Strings.Designer.cs index 0501c22c3e..2471648af3 100644 --- a/src/Microsoft.Data.SqlClient/netfx/src/Resources/Strings.Designer.cs +++ b/src/Microsoft.Data.SqlClient/netfx/src/Resources/Strings.Designer.cs @@ -9090,6 +9090,15 @@ internal static string SQL_CannotCreateAuthProvider { } } + /// + /// Looks up a localized string similar to Failed to fetch application client id with type '{0}'.. + /// + internal static string SQL_CannotFetchApplicationClientId { + get { + return ResourceManager.GetString("SQL_CannotFetchApplicationClientId", resourceCulture); + } + } + /// /// Looks up a localized string similar to Cannot find an authentication provider for '{0}'.. /// diff --git a/src/Microsoft.Data.SqlClient/netfx/src/Resources/Strings.resx b/src/Microsoft.Data.SqlClient/netfx/src/Resources/Strings.resx index c74e4526ef..127d0fff82 100644 --- a/src/Microsoft.Data.SqlClient/netfx/src/Resources/Strings.resx +++ b/src/Microsoft.Data.SqlClient/netfx/src/Resources/Strings.resx @@ -4575,4 +4575,7 @@ Cannot use 'Authentication=Active Directory Device Code Flow', if the Credential property has been set. - + + Failed to fetch application client id with type '{0}'. + + \ No newline at end of file diff --git a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/ActiveDirectoryAuthenticationProvider.cs b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/ActiveDirectoryAuthenticationProvider.cs index fe3317f17b..fd3ddfe932 100644 --- a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/ActiveDirectoryAuthenticationProvider.cs +++ b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/ActiveDirectoryAuthenticationProvider.cs @@ -20,13 +20,21 @@ public sealed class ActiveDirectoryAuthenticationProvider : SqlAuthenticationPro private readonly SqlClientLogger _logger = new SqlClientLogger(); private Func _deviceCodeFlowCallback; private ICustomWebUi _customWebUI = null; + private readonly string _applicationClientId = ActiveDirectoryAuthentication.AdoClientId; /// public ActiveDirectoryAuthenticationProvider() => new ActiveDirectoryAuthenticationProvider(DefaultDeviceFlowCallback); /// - public ActiveDirectoryAuthenticationProvider(Func deviceCodeFlowCallbackMethod) + public ActiveDirectoryAuthenticationProvider(string applicationClientId) => new ActiveDirectoryAuthenticationProvider(DefaultDeviceFlowCallback, applicationClientId); + + /// + public ActiveDirectoryAuthenticationProvider(Func deviceCodeFlowCallbackMethod, string applicationClientId = null) { + if (applicationClientId != null) + { + _applicationClientId = applicationClientId; + } SetDeviceCodeFlowCallback(deviceCodeFlowCallbackMethod); } @@ -112,7 +120,7 @@ public override Task AcquireTokenAsync(SqlAuthentication #if netstandard if (parentActivityOrWindowFunc != null) { - app = PublicClientApplicationBuilder.Create(ActiveDirectoryAuthentication.AdoClientId) + app = PublicClientApplicationBuilder.Create(_applicationClientId) .WithAuthority(parameters.Authority) .WithClientName(Common.DbConnectionStringDefaults.ApplicationName) .WithClientVersion(Common.ADP.GetAssemblyVersion().ToString()) @@ -124,7 +132,7 @@ public override Task AcquireTokenAsync(SqlAuthentication #if netfx if (_iWin32WindowFunc != null) { - app = PublicClientApplicationBuilder.Create(ActiveDirectoryAuthentication.AdoClientId) + app = PublicClientApplicationBuilder.Create(_applicationClientId) .WithAuthority(parameters.Authority) .WithClientName(Common.DbConnectionStringDefaults.ApplicationName) .WithClientVersion(Common.ADP.GetAssemblyVersion().ToString()) @@ -137,7 +145,7 @@ public override Task AcquireTokenAsync(SqlAuthentication else #endif { - app = PublicClientApplicationBuilder.Create(ActiveDirectoryAuthentication.AdoClientId) + app = PublicClientApplicationBuilder.Create(_applicationClientId) .WithAuthority(parameters.Authority) .WithClientName(Common.DbConnectionStringDefaults.ApplicationName) .WithClientVersion(Common.ADP.GetAssemblyVersion().ToString()) From 33f047e7686aa12b2c764c90ad1e15911ed70df3 Mon Sep 17 00:00:00 2001 From: Cheena Malhotra Date: Wed, 23 Sep 2020 16:30:54 -0700 Subject: [PATCH 2/4] Address review comments --- .../ActiveDirectoryAuthenticationProvider.xml | 4 ++-- .../SqlAuthenticationProviderManager.NetCoreApp.cs | 10 +--------- .../netcore/src/Microsoft/Data/SqlClient/SqlUtil.cs | 5 ----- .../netcore/src/Resources/Strings.Designer.cs | 9 --------- .../netcore/src/Resources/Strings.resx | 3 --- .../Data/SqlClient/SqlAuthenticationProviderManager.cs | 10 +--------- .../netfx/src/Microsoft/Data/SqlClient/SqlUtil.cs | 5 ----- .../netfx/src/Resources/Strings.Designer.cs | 9 --------- .../netfx/src/Resources/Strings.resx | 3 --- 9 files changed, 4 insertions(+), 54 deletions(-) diff --git a/doc/snippets/Microsoft.Data.SqlClient/ActiveDirectoryAuthenticationProvider.xml b/doc/snippets/Microsoft.Data.SqlClient/ActiveDirectoryAuthenticationProvider.xml index e136306055..001a8b5d52 100644 --- a/doc/snippets/Microsoft.Data.SqlClient/ActiveDirectoryAuthenticationProvider.xml +++ b/doc/snippets/Microsoft.Data.SqlClient/ActiveDirectoryAuthenticationProvider.xml @@ -11,7 +11,7 @@ - Client Application Id to be used for acquiring access token for federated authentication. The driver uses it's own application client id by default. + Client Application Id to be used for acquiring access token for federated authentication. The driver uses its own application client id by default. Initializes the class with the provided application client id. @@ -30,7 +30,7 @@ The callback method to be used when performing 'Active Directory Device Code Flow' authentication. - (Optional) Client Application Id to be used for acquiring access token for federated authentication. The driver uses it's own application client id by default. + (Optional) Client Application Id to be used for acquiring access token for federated authentication. The driver uses its own application client id by default. Initializes the class with the provided device code flow callback method and application client id. 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 684f9210be..80ed6024e3 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 @@ -61,15 +61,7 @@ public SqlAuthenticationProviderManager(SqlAuthenticationProviderConfigurationSe if (!string.IsNullOrEmpty(configSection.ApplicationClientId)) { - try - { - _applicationClientId = configSection.ApplicationClientId; - } - catch (Exception e) - { - throw SQL.CannotFetchApplicationClientId(configSection.ApplicationClientId, e); - } - + _applicationClientId = configSection.ApplicationClientId; _sqlAuthLogger.LogInfo(_typeName, methodName, "Received user-defined Application Client Id"); } else diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlUtil.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlUtil.cs index 611fd5a3a5..5ae4a85d2f 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlUtil.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlUtil.cs @@ -433,11 +433,6 @@ internal static Exception CannotCreateSqlAuthInitializer(string type, Exception return ADP.Argument(System.StringsHelper.GetString(Strings.SQL_CannotCreateAuthInitializer, type), e); } - static internal Exception CannotFetchApplicationClientId(string type, Exception e) - { - return ADP.Argument(StringsHelper.GetString(Strings.SQL_CannotFetchApplicationClientId, type), e); - } - internal static Exception CannotInitializeAuthProvider(string type, Exception e) { return ADP.InvalidOperation(System.StringsHelper.GetString(Strings.SQL_CannotInitializeAuthProvider, type), e); diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Resources/Strings.Designer.cs b/src/Microsoft.Data.SqlClient/netcore/src/Resources/Strings.Designer.cs index d3a6f1ea52..438423fa71 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Resources/Strings.Designer.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Resources/Strings.Designer.cs @@ -2373,15 +2373,6 @@ internal static string SQL_CannotCreateNormalizer { } } - /// - /// Looks up a localized string similar to Failed to fetch application client id with type '{0}'.. - /// - internal static string SQL_CannotFetchApplicationClientId { - get { - return ResourceManager.GetString("SQL_CannotFetchApplicationClientId", resourceCulture); - } - } - /// /// Looks up a localized string similar to Cannot find an authentication provider for '{0}'.. /// diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Resources/Strings.resx b/src/Microsoft.Data.SqlClient/netcore/src/Resources/Strings.resx index 34c22d3029..56515de02b 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Resources/Strings.resx +++ b/src/Microsoft.Data.SqlClient/netcore/src/Resources/Strings.resx @@ -1905,7 +1905,4 @@ Cannot use 'Authentication=Active Directory Device Code Flow', if the Credential property has been set. - - Failed to fetch application client id with type '{0}'. - \ No newline at end of file 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 c0d9ac7d0d..a865dc8fa1 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 @@ -76,15 +76,7 @@ public SqlAuthenticationProviderManager(SqlAuthenticationProviderConfigurationSe if (!string.IsNullOrEmpty(configSection.ApplicationClientId)) { - try - { - _applicationClientId = configSection.ApplicationClientId; - } - catch (Exception e) - { - throw SQL.CannotFetchApplicationClientId(configSection.ApplicationClientId, e); - } - + _applicationClientId = configSection.ApplicationClientId; _sqlAuthLogger.LogInfo(_typeName, methodName, "Received user-defined Application Client Id"); } else diff --git a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlUtil.cs b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlUtil.cs index ac29ff4f95..fee169e994 100644 --- a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlUtil.cs +++ b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlUtil.cs @@ -462,11 +462,6 @@ static internal Exception CannotCreateSqlAuthInitializer(string type, Exception return ADP.Argument(StringsHelper.GetString(Strings.SQL_CannotCreateAuthInitializer, type), e); } - static internal Exception CannotFetchApplicationClientId(string type, Exception e) - { - return ADP.Argument(StringsHelper.GetString(Strings.SQL_CannotFetchApplicationClientId, type), e); - } - static internal Exception CannotInitializeAuthProvider(string type, Exception e) { return ADP.InvalidOperation(StringsHelper.GetString(Strings.SQL_CannotInitializeAuthProvider, type), e); diff --git a/src/Microsoft.Data.SqlClient/netfx/src/Resources/Strings.Designer.cs b/src/Microsoft.Data.SqlClient/netfx/src/Resources/Strings.Designer.cs index 2471648af3..0501c22c3e 100644 --- a/src/Microsoft.Data.SqlClient/netfx/src/Resources/Strings.Designer.cs +++ b/src/Microsoft.Data.SqlClient/netfx/src/Resources/Strings.Designer.cs @@ -9090,15 +9090,6 @@ internal static string SQL_CannotCreateAuthProvider { } } - /// - /// Looks up a localized string similar to Failed to fetch application client id with type '{0}'.. - /// - internal static string SQL_CannotFetchApplicationClientId { - get { - return ResourceManager.GetString("SQL_CannotFetchApplicationClientId", resourceCulture); - } - } - /// /// Looks up a localized string similar to Cannot find an authentication provider for '{0}'.. /// diff --git a/src/Microsoft.Data.SqlClient/netfx/src/Resources/Strings.resx b/src/Microsoft.Data.SqlClient/netfx/src/Resources/Strings.resx index 127d0fff82..c6fbca99b1 100644 --- a/src/Microsoft.Data.SqlClient/netfx/src/Resources/Strings.resx +++ b/src/Microsoft.Data.SqlClient/netfx/src/Resources/Strings.resx @@ -4575,7 +4575,4 @@ Cannot use 'Authentication=Active Directory Device Code Flow', if the Credential property has been set. - - Failed to fetch application client id with type '{0}'. - \ No newline at end of file From c8d7a6527e3b1376a733ef992e28ac87080f2140 Mon Sep 17 00:00:00 2001 From: Cheena Malhotra Date: Thu, 24 Sep 2020 16:31:26 -0700 Subject: [PATCH 3/4] Code Review applied --- .../ActiveDirectoryAuthenticationProvider.xml | 12 ++++++------ .../netcore/src/Microsoft/Data/SqlClient/SqlUtil.cs | 6 +++--- .../netcore/src/Resources/Strings.resx | 2 +- .../netfx/src/Resources/Strings.resx | 2 +- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/doc/snippets/Microsoft.Data.SqlClient/ActiveDirectoryAuthenticationProvider.xml b/doc/snippets/Microsoft.Data.SqlClient/ActiveDirectoryAuthenticationProvider.xml index 001a8b5d52..052cd0cc30 100644 --- a/doc/snippets/Microsoft.Data.SqlClient/ActiveDirectoryAuthenticationProvider.xml +++ b/doc/snippets/Microsoft.Data.SqlClient/ActiveDirectoryAuthenticationProvider.xml @@ -11,7 +11,7 @@ - Client Application Id to be used for acquiring access token for federated authentication. The driver uses its own application client id by default. + Client Application Id to be used for acquiring an access token for federated authentication. The driver uses its own application client id by default. Initializes the class with the provided application client id. @@ -20,7 +20,7 @@ - The callback method to be used when performing 'Active Directory Device Code Flow' authentication. - (Optional) Client Application Id to be used for acquiring access token for federated authentication. The driver uses its own application client id by default. + The callback method to be used with 'Active Directory Device Code Flow' authentication. + (Optional) Client Application Id to be used for acquiring an access token for federated authentication. The driver uses its own application client id by default. Initializes the class with the provided device code flow callback method and application client id. @@ -41,8 +41,8 @@ Represents an asynchronous operation that returns the authentication token. - The callback method to be used when performing 'Active Directory Device Code Flow' authentication. - Sets the callback method, overriding the default implementation that processes the result when performing 'Active Directory Device Code Flow' authentication. + The callback method to be used with 'Active Directory Device Code Flow' authentication. + Sets the callback method, overriding the default implementation that processes the result for 'Active Directory Device Code Flow' authentication. The parent as an object, in order to be used from shared .NET Standard assemblies. diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlUtil.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlUtil.cs index 5ae4a85d2f..744ab20402 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlUtil.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlUtil.cs @@ -432,7 +432,7 @@ internal static Exception CannotCreateSqlAuthInitializer(string type, Exception { return ADP.Argument(System.StringsHelper.GetString(Strings.SQL_CannotCreateAuthInitializer, type), e); } - + internal static Exception CannotInitializeAuthProvider(string type, Exception e) { return ADP.InvalidOperation(System.StringsHelper.GetString(Strings.SQL_CannotInitializeAuthProvider, type), e); @@ -903,11 +903,11 @@ internal static Exception BulkLoadUnspecifiedSortOrder() internal static Exception BulkLoadInvalidOrderHint() { return ADP.Argument(System.StringsHelper.GetString(Strings.SQL_BulkLoadInvalidOrderHint)); - } + } internal static Exception BulkLoadOrderHintInvalidColumn(string columnName) { return ADP.InvalidOperation(string.Format(System.StringsHelper.GetString(Strings.SQL_BulkLoadOrderHintInvalidColumn), columnName)); - } + } internal static Exception BulkLoadOrderHintDuplicateColumn(string columnName) { return ADP.InvalidOperation(string.Format(System.StringsHelper.GetString(Strings.SQL_BulkLoadOrderHintDuplicateColumn), columnName)); diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Resources/Strings.resx b/src/Microsoft.Data.SqlClient/netcore/src/Resources/Strings.resx index 56515de02b..b94391deac 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Resources/Strings.resx +++ b/src/Microsoft.Data.SqlClient/netcore/src/Resources/Strings.resx @@ -1905,4 +1905,4 @@ Cannot use 'Authentication=Active Directory Device Code Flow', if the Credential property has been set. - \ No newline at end of file + diff --git a/src/Microsoft.Data.SqlClient/netfx/src/Resources/Strings.resx b/src/Microsoft.Data.SqlClient/netfx/src/Resources/Strings.resx index c6fbca99b1..c74e4526ef 100644 --- a/src/Microsoft.Data.SqlClient/netfx/src/Resources/Strings.resx +++ b/src/Microsoft.Data.SqlClient/netfx/src/Resources/Strings.resx @@ -4575,4 +4575,4 @@ Cannot use 'Authentication=Active Directory Device Code Flow', if the Credential property has been set. - \ No newline at end of file + From ef8cf3a56325c0a637348d13d1e90a2a66161fa0 Mon Sep 17 00:00:00 2001 From: Cheena Malhotra Date: Fri, 25 Sep 2020 10:41:12 -0700 Subject: [PATCH 4/4] Update trace logs --- .../SqlClient/SqlAuthenticationProviderManager.NetCoreApp.cs | 2 +- .../Data/SqlClient/SqlAuthenticationProviderManager.cs | 2 +- 2 files changed, 2 insertions(+), 2 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 80ed6024e3..06d9c786ce 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 @@ -30,7 +30,7 @@ static SqlAuthenticationProviderManager() catch (ConfigurationErrorsException e) { // Don't throw an error for invalid config files - SqlClientEventSource.Log.TryTraceEvent("Unable to load custom SqlAuthenticationProviders or SqlClientAuthenticationProviders. ConfigurationManager failed to load due to configuration errors: {0}", e); + SqlClientEventSource.Log.TryTraceEvent("static SqlAuthenticationProviderManager: Unable to load custom SqlAuthenticationProviders or SqlClientAuthenticationProviders. ConfigurationManager failed to load due to configuration errors: {0}", e); } Instance = new SqlAuthenticationProviderManager(configurationSection); 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 a865dc8fa1..b875c0160d 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 @@ -37,7 +37,7 @@ static SqlAuthenticationProviderManager() catch (ConfigurationErrorsException e) { // Don't throw an error for invalid config files - SqlClientEventSource.Log.TryTraceEvent("Unable to load custom SqlAuthenticationProviders or SqlClientAuthenticationProviders. ConfigurationManager failed to load due to configuration errors: {0}", e); + SqlClientEventSource.Log.TryTraceEvent("static SqlAuthenticationProviderManager: Unable to load custom SqlAuthenticationProviders or SqlClientAuthenticationProviders. ConfigurationManager failed to load due to configuration errors: {0}", e); } Instance = new SqlAuthenticationProviderManager(configurationSection);