diff --git a/doc/snippets/Microsoft.Data.SqlClient/SqlConnection.xml b/doc/snippets/Microsoft.Data.SqlClient/SqlConnection.xml index 37f61835aa..890a89b9be 100644 --- a/doc/snippets/Microsoft.Data.SqlClient/SqlConnection.xml +++ b/doc/snippets/Microsoft.Data.SqlClient/SqlConnection.xml @@ -262,10 +262,11 @@ The following example creates a and a This property is mutually exclusive with the - + , + , and - properties, among others. Setting this property when + properties. Setting this property when is already set throws , because SSPI is an diff --git a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/Common/AdapterUtil.cs b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/Common/AdapterUtil.cs index 3b8e6cda5d..0f85aa0792 100644 --- a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/Common/AdapterUtil.cs +++ b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/Common/AdapterUtil.cs @@ -1452,11 +1452,8 @@ internal static Exception InvalidMixedUsageOfAccessTokenCallbackAndAuthenticatio internal static Exception InvalidMixedUsageOfAccessTokenCallbackAndIntegratedSecurity() => InvalidOperation(StringsHelper.GetString(Strings.ADP_InvalidMixedUsageOfAccessTokenCallbackAndIntegratedSecurity)); - internal static Exception InvalidMixedUsageOfAccessTokenAndSspiContextProvider() - => InvalidOperation(StringsHelper.GetString(Strings.ADP_InvalidMixedUsageOfAccessTokenAndSspiContextProvider)); - - internal static Exception InvalidMixedUsageOfSspiContextProviderAndAccessToken() - => InvalidOperation(StringsHelper.GetString(Strings.ADP_InvalidMixedUsageOfSspiContextProviderAndAccessToken)); + internal static Exception InvalidMixedUsageOfAccessTokenProperties() + => InvalidOperation(StringsHelper.GetString(Strings.ADP_InvalidMixedUsageOfAccessTokenProperties)); #endregion internal static readonly IntPtr s_ptrZero = IntPtr.Zero; diff --git a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlConnection.cs b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlConnection.cs index 740470b5bd..11e4c02324 100644 --- a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlConnection.cs +++ b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlConnection.cs @@ -1209,7 +1209,7 @@ private void CheckAndThrowOnInvalidCombinationOfConnectionOptionAndAccessToken(S if (_sspiContextProvider != null) { - throw ADP.InvalidMixedUsageOfAccessTokenAndSspiContextProvider(); + throw ADP.InvalidMixedUsageOfAccessTokenProperties(); } } @@ -1236,7 +1236,7 @@ private void CheckAndThrowOnInvalidCombinationOfConnectionOptionAndAccessTokenCa if (_sspiContextProvider != null) { - throw ADP.InvalidMixedUsageOfAccessTokenAndSspiContextProvider(); + throw ADP.InvalidMixedUsageOfAccessTokenProperties(); } } @@ -1248,7 +1248,7 @@ private void CheckAndThrowOnInvalidCombinationOfConnectionOptionAndSspiContextPr { if (_accessToken != null || _accessTokenCallback != null) { - throw ADP.InvalidMixedUsageOfSspiContextProviderAndAccessToken(); + throw ADP.InvalidMixedUsageOfAccessTokenProperties(); } } diff --git a/src/Microsoft.Data.SqlClient/src/Resources/Strings.Designer.cs b/src/Microsoft.Data.SqlClient/src/Resources/Strings.Designer.cs index e2fc5988bd..243cb77563 100644 --- a/src/Microsoft.Data.SqlClient/src/Resources/Strings.Designer.cs +++ b/src/Microsoft.Data.SqlClient/src/Resources/Strings.Designer.cs @@ -916,20 +916,11 @@ internal static string ADP_InvalidMixedUsageOfAccessTokenCallbackAndIntegratedSe } /// - /// Looks up a localized string similar to Cannot set the AccessToken or AccessTokenCallback property if the SspiContextProvider property has been set.. + /// Looks up a localized string similar to Cannot set more than one of the properties AccessToken, AccessTokenCallback, or SspiContextProvider.. /// - internal static string ADP_InvalidMixedUsageOfAccessTokenAndSspiContextProvider { + internal static string ADP_InvalidMixedUsageOfAccessTokenProperties { get { - return ResourceManager.GetString("ADP_InvalidMixedUsageOfAccessTokenAndSspiContextProvider", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Cannot set the SspiContextProvider property if the AccessToken or AccessTokenCallback property has been set.. - /// - internal static string ADP_InvalidMixedUsageOfSspiContextProviderAndAccessToken { - get { - return ResourceManager.GetString("ADP_InvalidMixedUsageOfSspiContextProviderAndAccessToken", resourceCulture); + return ResourceManager.GetString("ADP_InvalidMixedUsageOfAccessTokenProperties", resourceCulture); } } diff --git a/src/Microsoft.Data.SqlClient/src/Resources/Strings.resx b/src/Microsoft.Data.SqlClient/src/Resources/Strings.resx index 94af5aed6b..3ea4d5eb09 100644 --- a/src/Microsoft.Data.SqlClient/src/Resources/Strings.resx +++ b/src/Microsoft.Data.SqlClient/src/Resources/Strings.resx @@ -4566,11 +4566,8 @@ Cannot set the AccessTokenCallback property if the 'Integrated Security' connection string keyword has been set to 'true' or 'SSPI'. - - Cannot set the AccessToken or AccessTokenCallback property if the SspiContextProvider property has been set. - - - Cannot set the SspiContextProvider property if the AccessToken or AccessTokenCallback property has been set. + + Cannot set more than one of the properties AccessToken, AccessTokenCallback, or SspiContextProvider. Cannot set the AccessTokenCallback property if 'Authentication=Active Directory Default' has been specified in the connection string. diff --git a/src/Microsoft.Data.SqlClient/tests/UnitTests/SimulatedServerTests/ConnectionTests.cs b/src/Microsoft.Data.SqlClient/tests/UnitTests/SimulatedServerTests/ConnectionTests.cs index 0aa916c7ff..8960dc43cb 100644 --- a/src/Microsoft.Data.SqlClient/tests/UnitTests/SimulatedServerTests/ConnectionTests.cs +++ b/src/Microsoft.Data.SqlClient/tests/UnitTests/SimulatedServerTests/ConnectionTests.cs @@ -810,33 +810,41 @@ public void SspiContextProviderAndAccessTokenStateAreMutuallyExclusive() { Func> callback = (ctx, token) => Task.FromResult(new SqlAuthenticationToken("invalid", DateTimeOffset.MaxValue)); + string expectedMessage = global::Microsoft.Data.StringsHelper.GetString( + global::System.Strings.ADP_InvalidMixedUsageOfAccessTokenProperties); // Token first, then provider. using (SqlConnection conn = new("Data Source=localhost")) { conn.AccessToken = "token"; - Assert.Throws( + InvalidOperationException exception = Assert.Throws( () => conn.SspiContextProvider = new TestSspiContextProvider()); + Assert.Equal(expectedMessage, exception.Message); } using (SqlConnection conn = new("Data Source=localhost")) { conn.AccessTokenCallback = callback; - Assert.Throws( + InvalidOperationException exception = Assert.Throws( () => conn.SspiContextProvider = new TestSspiContextProvider()); + Assert.Equal(expectedMessage, exception.Message); } // Provider first, then token. using (SqlConnection conn = new("Data Source=localhost")) { conn.SspiContextProvider = new TestSspiContextProvider(); - Assert.Throws(() => conn.AccessToken = "token"); + InvalidOperationException exception = Assert.Throws( + () => conn.AccessToken = "token"); + Assert.Equal(expectedMessage, exception.Message); } using (SqlConnection conn = new("Data Source=localhost")) { conn.SspiContextProvider = new TestSspiContextProvider(); - Assert.Throws(() => conn.AccessTokenCallback = callback); + InvalidOperationException exception = Assert.Throws( + () => conn.AccessTokenCallback = callback); + Assert.Equal(expectedMessage, exception.Message); } }