From 6f2e64d43a6b14ceac905d3af3e445c4782c2736 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Luthi?= Date: Mon, 1 Jul 2024 22:04:25 +0200 Subject: [PATCH] Avoid "early" access to SqlAuthenticationProviderManager.Instance Accessing `SqlAuthenticationProviderManager.Instance` inside the `SqlInternalConnectionTds` constructor forces to load `Microsoft.Identity.Client.dll` since the `ActiveDirectoryAuthenticationProvider` constructor references the `DeviceCodeResult` type (which is part of `Microsoft.Identity.Client`). ``` at Microsoft.Data.SqlClient.ActiveDirectoryAuthenticationProvider..ctor(String applicationClientId) at Microsoft.Data.SqlClient.SqlAuthenticationProviderManager.SetDefaultAuthProviders(SqlAuthenticationProviderManager instance) at Microsoft.Data.SqlClient.SqlAuthenticationProviderManager..cctor() at Microsoft.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, Boolean applyTransientFaultHandling, String accessToken, DbConnectionPool pool, Func`3 accessTokenCallback) ``` Accessing `SqlAuthenticationProviderManager.Instance` later (inside the `GetFedAuthToken` method) makes it possible to remove the `Microsoft.Identity.Client` dependency when it's not needed, for example with [Chisel][1]. [1]: https://github.com/0xced/Chisel/ --- .../src/Microsoft/Data/SqlClient/SqlInternalConnectionTds.cs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlInternalConnectionTds.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlInternalConnectionTds.cs index 5ef3d781ea..5d6a1d30e0 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlInternalConnectionTds.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlInternalConnectionTds.cs @@ -133,7 +133,6 @@ internal sealed class SqlInternalConnectionTds : SqlInternalConnection, IDisposa internal readonly Func> _accessTokenCallback; private readonly ActiveDirectoryAuthenticationTimeoutRetryHelper _activeDirectoryAuthTimeoutRetryHelper; - private readonly SqlAuthenticationProviderManager _sqlAuthenticationProviderManager; internal bool _cleanSQLDNSCaching = false; private bool _serverSupportsDNSCaching = false; @@ -484,7 +483,6 @@ internal SqlInternalConnectionTds( _accessTokenCallback = accessTokenCallback; _activeDirectoryAuthTimeoutRetryHelper = new ActiveDirectoryAuthenticationTimeoutRetryHelper(); - _sqlAuthenticationProviderManager = SqlAuthenticationProviderManager.Instance; _identity = identity; Debug.Assert(newSecurePassword != null || newPassword != null, "cannot have both new secure change password and string based change password to be null"); @@ -2362,7 +2360,7 @@ internal SqlFedAuthToken GetFedAuthToken(SqlFedAuthInfo fedAuthInfo) // Username to use in error messages. string username = null; - var authProvider = _sqlAuthenticationProviderManager.GetProvider(ConnectionOptions.Authentication); + var authProvider = SqlAuthenticationProvider.GetProvider(ConnectionOptions.Authentication); if (authProvider == null && _accessTokenCallback == null) throw SQL.CannotFindAuthProvider(ConnectionOptions.Authentication.ToString());