From d0fdcba28fa3f6ea13e8c4f655485af16e4fd4f6 Mon Sep 17 00:00:00 2001 From: Cheena Malhotra Date: Wed, 17 Sep 2025 13:38:09 -0700 Subject: [PATCH 01/12] Fix SetProvider to return immediately if user-defined provider found --- .../Data/SqlClient/SqlAuthenticationProviderManager.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlAuthenticationProviderManager.cs b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlAuthenticationProviderManager.cs index e29746dc6c..e360309e11 100644 --- a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlAuthenticationProviderManager.cs +++ b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlAuthenticationProviderManager.cs @@ -186,7 +186,7 @@ public bool SetProvider(SqlAuthenticationMethod authenticationMethod, SqlAuthent if (candidateMethod == authenticationMethod) { _sqlAuthLogger.LogError(nameof(SqlAuthenticationProviderManager), methodName, $"Failed to add provider {GetProviderType(provider)} because a user-defined provider with type {GetProviderType(_providers[authenticationMethod])} already existed for authentication {authenticationMethod}."); - break; + return false; // return here to avoid replacing user-defined provider } } } From 762d785621f50492f505f5a7aecb3558a1499811 Mon Sep 17 00:00:00 2001 From: Cheena Malhotra Date: Tue, 23 Sep 2025 12:32:28 -0700 Subject: [PATCH 02/12] Include test --- .../FunctionalTests/AADAuthenticationTests.cs | 14 +++++++++- .../tests/FunctionalTests/App.config | 11 ++++++++ .../DummySqlAuthenticationProvider.cs | 27 +++++++++++++++++++ ...soft.Data.SqlClient.FunctionalTests.csproj | 1 + 4 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 src/Microsoft.Data.SqlClient/tests/FunctionalTests/App.config create mode 100644 src/Microsoft.Data.SqlClient/tests/FunctionalTests/DataCommon/DummySqlAuthenticationProvider.cs diff --git a/src/Microsoft.Data.SqlClient/tests/FunctionalTests/AADAuthenticationTests.cs b/src/Microsoft.Data.SqlClient/tests/FunctionalTests/AADAuthenticationTests.cs index e97b251613..51d63b984c 100644 --- a/src/Microsoft.Data.SqlClient/tests/FunctionalTests/AADAuthenticationTests.cs +++ b/src/Microsoft.Data.SqlClient/tests/FunctionalTests/AADAuthenticationTests.cs @@ -5,7 +5,7 @@ using System; using System.Security; using System.Threading.Tasks; -using Microsoft.Identity.Client; +using Microsoft.Data.SqlClient.FunctionalTests.DataCommon; using Xunit; namespace Microsoft.Data.SqlClient.Tests @@ -50,6 +50,18 @@ private void InvalidCombinationCheck(SqlCredential credential) } } + [Fact] + public async Task IsDummySqlAuthenticationProviderSetByDefault() + { + var provider = SqlAuthenticationProvider.GetProvider(SqlAuthenticationMethod.ActiveDirectoryInteractive); + + Assert.NotNull(provider); + Assert.Equal(typeof(DummySqlAuthenticationProvider), provider.GetType()); + + var token = await provider.AcquireTokenAsync(null); + Assert.Equal(token.AccessToken, DummySqlAuthenticationProvider.DUMMY_TOKEN_STR); + } + [Fact] public void CustomActiveDirectoryProviderTest() { diff --git a/src/Microsoft.Data.SqlClient/tests/FunctionalTests/App.config b/src/Microsoft.Data.SqlClient/tests/FunctionalTests/App.config new file mode 100644 index 0000000000..a4f3cf2b37 --- /dev/null +++ b/src/Microsoft.Data.SqlClient/tests/FunctionalTests/App.config @@ -0,0 +1,11 @@ + + + +
+ + + + + + + diff --git a/src/Microsoft.Data.SqlClient/tests/FunctionalTests/DataCommon/DummySqlAuthenticationProvider.cs b/src/Microsoft.Data.SqlClient/tests/FunctionalTests/DataCommon/DummySqlAuthenticationProvider.cs new file mode 100644 index 0000000000..bb5e2e2e52 --- /dev/null +++ b/src/Microsoft.Data.SqlClient/tests/FunctionalTests/DataCommon/DummySqlAuthenticationProvider.cs @@ -0,0 +1,27 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Threading.Tasks; + +namespace Microsoft.Data.SqlClient.FunctionalTests.DataCommon +{ + /// + /// Dummy class to override default Sql Authentication provider in functional tests. + /// This type returns a dummy access token and is only used for registration test from app.config file. + /// Since no actual connections are intended to be made in Functional tests, + /// this type is added by default to validate config file registration scenario. + /// + public class DummySqlAuthenticationProvider : SqlAuthenticationProvider + { + public static string DUMMY_TOKEN_STR = "dummy_access_token"; + + public override Task AcquireTokenAsync(SqlAuthenticationParameters parameters) + => Task.FromResult(new SqlAuthenticationToken(DUMMY_TOKEN_STR, new DateTimeOffset(DateTime.Now.AddHours(2)))); + + // Supported authentication modes don't matter for dummy test, but added to demonstrate config file usage. + public override bool IsSupported(SqlAuthenticationMethod authenticationMethod) + => authenticationMethod == SqlAuthenticationMethod.ActiveDirectoryInteractive; + } +} diff --git a/src/Microsoft.Data.SqlClient/tests/FunctionalTests/Microsoft.Data.SqlClient.FunctionalTests.csproj b/src/Microsoft.Data.SqlClient/tests/FunctionalTests/Microsoft.Data.SqlClient.FunctionalTests.csproj index 91a5a505b9..2946025aec 100644 --- a/src/Microsoft.Data.SqlClient/tests/FunctionalTests/Microsoft.Data.SqlClient.FunctionalTests.csproj +++ b/src/Microsoft.Data.SqlClient/tests/FunctionalTests/Microsoft.Data.SqlClient.FunctionalTests.csproj @@ -24,6 +24,7 @@ + From 08265d24bab33cb06ae6d5f89d0038f38b5528a4 Mon Sep 17 00:00:00 2001 From: Cheena Malhotra Date: Tue, 23 Sep 2025 16:15:13 -0700 Subject: [PATCH 03/12] Fix tests --- .../Data/SqlClient/SqlAuthenticationProviderManager.cs | 9 +++++++++ .../tests/FunctionalTests/AADAuthenticationTests.cs | 8 +++++++- .../tests/FunctionalTests/App.config | 2 ++ .../Microsoft.Data.SqlClient.FunctionalTests.csproj | 10 ++++++++++ 4 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlAuthenticationProviderManager.cs b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlAuthenticationProviderManager.cs index e360309e11..1fe587720c 100644 --- a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlAuthenticationProviderManager.cs +++ b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlAuthenticationProviderManager.cs @@ -206,9 +206,18 @@ public bool SetProvider(SqlAuthenticationMethod authenticationMethod, SqlAuthent return true; } + /// + /// Fetches provided configuration section from app.config file. + /// Does not support reading from appsettings.json yet. + /// + /// + /// + /// private static T FetchConfigurationSection(string name) { Type t = typeof(T); + + // TODO: Support reading configuration from appsettings.json for .NET runtime applications. object section = ConfigurationManager.GetSection(name); if (section != null) { diff --git a/src/Microsoft.Data.SqlClient/tests/FunctionalTests/AADAuthenticationTests.cs b/src/Microsoft.Data.SqlClient/tests/FunctionalTests/AADAuthenticationTests.cs index 51d63b984c..eebb61ad9d 100644 --- a/src/Microsoft.Data.SqlClient/tests/FunctionalTests/AADAuthenticationTests.cs +++ b/src/Microsoft.Data.SqlClient/tests/FunctionalTests/AADAuthenticationTests.cs @@ -50,7 +50,13 @@ private void InvalidCombinationCheck(SqlCredential credential) } } - [Fact] + /// + /// Tests whether SQL Auth provider is overridden using app.config file. + /// This use case is only supported for .NET Framework applications, as driver doesn't support reading configuration from appsettings.json file. + /// In future if need be, appsettings.json support can be added. + /// + /// + [ConditionalFact(typeof(TestUtility), nameof(TestUtility.IsFullFramework))] public async Task IsDummySqlAuthenticationProviderSetByDefault() { var provider = SqlAuthenticationProvider.GetProvider(SqlAuthenticationMethod.ActiveDirectoryInteractive); diff --git a/src/Microsoft.Data.SqlClient/tests/FunctionalTests/App.config b/src/Microsoft.Data.SqlClient/tests/FunctionalTests/App.config index a4f3cf2b37..fb7f63f65f 100644 --- a/src/Microsoft.Data.SqlClient/tests/FunctionalTests/App.config +++ b/src/Microsoft.Data.SqlClient/tests/FunctionalTests/App.config @@ -1,8 +1,10 @@  +
+ diff --git a/src/Microsoft.Data.SqlClient/tests/FunctionalTests/Microsoft.Data.SqlClient.FunctionalTests.csproj b/src/Microsoft.Data.SqlClient/tests/FunctionalTests/Microsoft.Data.SqlClient.FunctionalTests.csproj index 2946025aec..fad3acd761 100644 --- a/src/Microsoft.Data.SqlClient/tests/FunctionalTests/Microsoft.Data.SqlClient.FunctionalTests.csproj +++ b/src/Microsoft.Data.SqlClient/tests/FunctionalTests/Microsoft.Data.SqlClient.FunctionalTests.csproj @@ -72,6 +72,11 @@ + + + PreserveNewest + + @@ -92,6 +97,7 @@ + @@ -138,10 +144,14 @@ %(Filename)%(Extension) + + + PreserveNewest xunit.runner.json + From f926b15a3f4a2003cb54a396db411ab13c6ab9df Mon Sep 17 00:00:00 2001 From: Cheena Malhotra Date: Tue, 23 Sep 2025 16:18:50 -0700 Subject: [PATCH 04/12] Remove unwanted changes --- .../Microsoft.Data.SqlClient.FunctionalTests.csproj | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/Microsoft.Data.SqlClient/tests/FunctionalTests/Microsoft.Data.SqlClient.FunctionalTests.csproj b/src/Microsoft.Data.SqlClient/tests/FunctionalTests/Microsoft.Data.SqlClient.FunctionalTests.csproj index fad3acd761..a2d11d1b63 100644 --- a/src/Microsoft.Data.SqlClient/tests/FunctionalTests/Microsoft.Data.SqlClient.FunctionalTests.csproj +++ b/src/Microsoft.Data.SqlClient/tests/FunctionalTests/Microsoft.Data.SqlClient.FunctionalTests.csproj @@ -144,14 +144,10 @@ %(Filename)%(Extension) - - - PreserveNewest xunit.runner.json - From 50b14b17d165ff91dad3632965adbe0d3a6ebbe4 Mon Sep 17 00:00:00 2001 From: Cheena Malhotra Date: Mon, 29 Sep 2025 09:32:15 -0700 Subject: [PATCH 05/12] Update config file name --- .../FunctionalTests/{App.config => FunctionalTests.dll.config} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/Microsoft.Data.SqlClient/tests/FunctionalTests/{App.config => FunctionalTests.dll.config} (100%) diff --git a/src/Microsoft.Data.SqlClient/tests/FunctionalTests/App.config b/src/Microsoft.Data.SqlClient/tests/FunctionalTests/FunctionalTests.dll.config similarity index 100% rename from src/Microsoft.Data.SqlClient/tests/FunctionalTests/App.config rename to src/Microsoft.Data.SqlClient/tests/FunctionalTests/FunctionalTests.dll.config From ab38d89da60f2d3df2f072114dc30a2fce6dccf9 Mon Sep 17 00:00:00 2001 From: Cheena Malhotra Date: Wed, 1 Oct 2025 14:23:54 -0700 Subject: [PATCH 06/12] Rename file back to app.config --- .../FunctionalTests/{FunctionalTests.dll.config => app.config} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/Microsoft.Data.SqlClient/tests/FunctionalTests/{FunctionalTests.dll.config => app.config} (100%) diff --git a/src/Microsoft.Data.SqlClient/tests/FunctionalTests/FunctionalTests.dll.config b/src/Microsoft.Data.SqlClient/tests/FunctionalTests/app.config similarity index 100% rename from src/Microsoft.Data.SqlClient/tests/FunctionalTests/FunctionalTests.dll.config rename to src/Microsoft.Data.SqlClient/tests/FunctionalTests/app.config From 75eb377a63f92c89ad366cccdd528561d81e4f7b Mon Sep 17 00:00:00 2001 From: Cheena Malhotra Date: Wed, 1 Oct 2025 14:28:05 -0700 Subject: [PATCH 07/12] Copy always --- .../Microsoft.Data.SqlClient.FunctionalTests.csproj | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Microsoft.Data.SqlClient/tests/FunctionalTests/Microsoft.Data.SqlClient.FunctionalTests.csproj b/src/Microsoft.Data.SqlClient/tests/FunctionalTests/Microsoft.Data.SqlClient.FunctionalTests.csproj index a2d11d1b63..0539e2d6d1 100644 --- a/src/Microsoft.Data.SqlClient/tests/FunctionalTests/Microsoft.Data.SqlClient.FunctionalTests.csproj +++ b/src/Microsoft.Data.SqlClient/tests/FunctionalTests/Microsoft.Data.SqlClient.FunctionalTests.csproj @@ -150,4 +150,9 @@ xunit.runner.json + + + Always + + From d4db85f3a02ae0d4439ff31917cc677679abb85c Mon Sep 17 00:00:00 2001 From: Cheena Malhotra Date: Wed, 1 Oct 2025 15:46:10 -0700 Subject: [PATCH 08/12] Disable tests for now. --- .../tests/FunctionalTests/AADAuthenticationTests.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.Data.SqlClient/tests/FunctionalTests/AADAuthenticationTests.cs b/src/Microsoft.Data.SqlClient/tests/FunctionalTests/AADAuthenticationTests.cs index eebb61ad9d..671bae0dba 100644 --- a/src/Microsoft.Data.SqlClient/tests/FunctionalTests/AADAuthenticationTests.cs +++ b/src/Microsoft.Data.SqlClient/tests/FunctionalTests/AADAuthenticationTests.cs @@ -57,13 +57,14 @@ private void InvalidCombinationCheck(SqlCredential credential) /// /// [ConditionalFact(typeof(TestUtility), nameof(TestUtility.IsFullFramework))] + [ActiveIssue("3648")] // Cannot enable on CI/CD pipeline due to limitation of loading app.config file for tests. public async Task IsDummySqlAuthenticationProviderSetByDefault() { var provider = SqlAuthenticationProvider.GetProvider(SqlAuthenticationMethod.ActiveDirectoryInteractive); - + Assert.NotNull(provider); Assert.Equal(typeof(DummySqlAuthenticationProvider), provider.GetType()); - + var token = await provider.AcquireTokenAsync(null); Assert.Equal(token.AccessToken, DummySqlAuthenticationProvider.DUMMY_TOKEN_STR); } From 8117bf06af58ff532d65f7e6678c39c80524329c Mon Sep 17 00:00:00 2001 From: Cheena Malhotra Date: Wed, 1 Oct 2025 21:24:52 -0700 Subject: [PATCH 09/12] Fix framework inclusion --- .../tests/FunctionalTests/AADAuthenticationTests.cs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.Data.SqlClient/tests/FunctionalTests/AADAuthenticationTests.cs b/src/Microsoft.Data.SqlClient/tests/FunctionalTests/AADAuthenticationTests.cs index 671bae0dba..7a3b425f7d 100644 --- a/src/Microsoft.Data.SqlClient/tests/FunctionalTests/AADAuthenticationTests.cs +++ b/src/Microsoft.Data.SqlClient/tests/FunctionalTests/AADAuthenticationTests.cs @@ -49,14 +49,16 @@ private void InvalidCombinationCheck(SqlCredential credential) Assert.Throws(() => connection.AccessToken = "SampleAccessToken"); } } + +#if NETFRAMEWORK + // This test is only valid for .NET Framework /// /// Tests whether SQL Auth provider is overridden using app.config file. /// This use case is only supported for .NET Framework applications, as driver doesn't support reading configuration from appsettings.json file. /// In future if need be, appsettings.json support can be added. /// - /// - [ConditionalFact(typeof(TestUtility), nameof(TestUtility.IsFullFramework))] + [Fact] [ActiveIssue("3648")] // Cannot enable on CI/CD pipeline due to limitation of loading app.config file for tests. public async Task IsDummySqlAuthenticationProviderSetByDefault() { @@ -68,6 +70,7 @@ public async Task IsDummySqlAuthenticationProviderSetByDefault() var token = await provider.AcquireTokenAsync(null); Assert.Equal(token.AccessToken, DummySqlAuthenticationProvider.DUMMY_TOKEN_STR); } +#endif [Fact] public void CustomActiveDirectoryProviderTest() From bd37fa360fd3d606f5bdf82964c5d6e7d1693a37 Mon Sep 17 00:00:00 2001 From: Cheena Malhotra Date: Wed, 1 Oct 2025 21:50:00 -0700 Subject: [PATCH 10/12] Fix test failures --- .../FunctionalTests/AADAuthenticationTests.cs | 5 ++--- .../SqlAuthenticationProviderTest.cs | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.Data.SqlClient/tests/FunctionalTests/AADAuthenticationTests.cs b/src/Microsoft.Data.SqlClient/tests/FunctionalTests/AADAuthenticationTests.cs index 7a3b425f7d..f354e6f806 100644 --- a/src/Microsoft.Data.SqlClient/tests/FunctionalTests/AADAuthenticationTests.cs +++ b/src/Microsoft.Data.SqlClient/tests/FunctionalTests/AADAuthenticationTests.cs @@ -50,7 +50,7 @@ private void InvalidCombinationCheck(SqlCredential credential) } } -#if NETFRAMEWORK + #if NETFRAMEWORK // This test is only valid for .NET Framework /// @@ -59,7 +59,6 @@ private void InvalidCombinationCheck(SqlCredential credential) /// In future if need be, appsettings.json support can be added. /// [Fact] - [ActiveIssue("3648")] // Cannot enable on CI/CD pipeline due to limitation of loading app.config file for tests. public async Task IsDummySqlAuthenticationProviderSetByDefault() { var provider = SqlAuthenticationProvider.GetProvider(SqlAuthenticationMethod.ActiveDirectoryInteractive); @@ -70,7 +69,7 @@ public async Task IsDummySqlAuthenticationProviderSetByDefault() var token = await provider.AcquireTokenAsync(null); Assert.Equal(token.AccessToken, DummySqlAuthenticationProvider.DUMMY_TOKEN_STR); } -#endif + #endif [Fact] public void CustomActiveDirectoryProviderTest() diff --git a/src/Microsoft.Data.SqlClient/tests/FunctionalTests/SqlAuthenticationProviderTest.cs b/src/Microsoft.Data.SqlClient/tests/FunctionalTests/SqlAuthenticationProviderTest.cs index d41f4b40d1..9001cb993a 100644 --- a/src/Microsoft.Data.SqlClient/tests/FunctionalTests/SqlAuthenticationProviderTest.cs +++ b/src/Microsoft.Data.SqlClient/tests/FunctionalTests/SqlAuthenticationProviderTest.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +using Microsoft.Data.SqlClient.FunctionalTests.DataCommon; using Xunit; namespace Microsoft.Data.SqlClient.Tests @@ -22,5 +23,18 @@ public void DefaultAuthenticationProviders(SqlAuthenticationMethod method) { Assert.IsType(SqlAuthenticationProvider.GetProvider(method)); } + + #if NETFRAMEWORK + // This test is only valid for .NET Framework + + // Overridden by app.config in this project + [Theory] + [InlineData(SqlAuthenticationMethod.ActiveDirectoryInteractive)] + public void DefaultAuthenticationProviders_Interactive(SqlAuthenticationMethod method) + { + Assert.IsType(SqlAuthenticationProvider.GetProvider(method)); + } + + #endif } } From a6871b0212db5f06868ea7166f62a5fc39e21182 Mon Sep 17 00:00:00 2001 From: Cheena Malhotra Date: Wed, 1 Oct 2025 21:53:10 -0700 Subject: [PATCH 11/12] Touch ups --- .../Microsoft.Data.SqlClient.FunctionalTests.csproj | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/Microsoft.Data.SqlClient/tests/FunctionalTests/Microsoft.Data.SqlClient.FunctionalTests.csproj b/src/Microsoft.Data.SqlClient/tests/FunctionalTests/Microsoft.Data.SqlClient.FunctionalTests.csproj index 0539e2d6d1..acf619a281 100644 --- a/src/Microsoft.Data.SqlClient/tests/FunctionalTests/Microsoft.Data.SqlClient.FunctionalTests.csproj +++ b/src/Microsoft.Data.SqlClient/tests/FunctionalTests/Microsoft.Data.SqlClient.FunctionalTests.csproj @@ -73,8 +73,8 @@ - - PreserveNewest + + Always @@ -150,9 +150,4 @@ xunit.runner.json - - - Always - - From a792b5144d2ed015fe03b5bb3ecced63fe831425 Mon Sep 17 00:00:00 2001 From: Cheena Malhotra Date: Wed, 1 Oct 2025 22:18:16 -0700 Subject: [PATCH 12/12] Fix test (continued) --- .../tests/FunctionalTests/SqlAuthenticationProviderTest.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Microsoft.Data.SqlClient/tests/FunctionalTests/SqlAuthenticationProviderTest.cs b/src/Microsoft.Data.SqlClient/tests/FunctionalTests/SqlAuthenticationProviderTest.cs index 9001cb993a..c15f1a9300 100644 --- a/src/Microsoft.Data.SqlClient/tests/FunctionalTests/SqlAuthenticationProviderTest.cs +++ b/src/Microsoft.Data.SqlClient/tests/FunctionalTests/SqlAuthenticationProviderTest.cs @@ -12,7 +12,6 @@ public class SqlAuthenticationProviderTest [Theory] [InlineData(SqlAuthenticationMethod.ActiveDirectoryIntegrated)] [InlineData(SqlAuthenticationMethod.ActiveDirectoryPassword)] - [InlineData(SqlAuthenticationMethod.ActiveDirectoryInteractive)] [InlineData(SqlAuthenticationMethod.ActiveDirectoryServicePrincipal)] [InlineData(SqlAuthenticationMethod.ActiveDirectoryDeviceCodeFlow)] [InlineData(SqlAuthenticationMethod.ActiveDirectoryManagedIdentity)]