From 5a8687219d1ed678c988499fb3f1dbc93c1a46ad Mon Sep 17 00:00:00 2001 From: Aasim Khan Date: Tue, 3 Oct 2023 20:25:22 -0700 Subject: [PATCH] Only opening connections when access token is provided (#2262) * Only opening connections when connection string is provided * undoing change * Adding comment for context * Adding comment for context * Formatting file * Fixing comment --- .../ObjectExplorer/StatelessObjectExplorer.cs | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/Microsoft.SqlTools.SqlCore/ObjectExplorer/StatelessObjectExplorer.cs b/src/Microsoft.SqlTools.SqlCore/ObjectExplorer/StatelessObjectExplorer.cs index 0cc9e324..af3ad706 100644 --- a/src/Microsoft.SqlTools.SqlCore/ObjectExplorer/StatelessObjectExplorer.cs +++ b/src/Microsoft.SqlTools.SqlCore/ObjectExplorer/StatelessObjectExplorer.cs @@ -38,10 +38,18 @@ namespace Microsoft.SqlTools.SqlCore.ObjectExplorer { using (SqlConnection conn = new SqlConnection(connectionString)) { - conn.AccessToken = accessToken?.Token; - conn.Open(); + // In case of access token based connections, we need to open the sql connection first before creating the server connection + // Not doing so will result in an exception when trying to call 'Connect' on the server connection + if (accessToken != null) + { + conn.AccessToken = accessToken.Token; + await conn.OpenAsync(); + } ServerConnection connection = new ServerConnection(conn); - connection.AccessToken = accessToken as IRenewableToken; + if (accessToken != null) + { + connection.AccessToken = accessToken as IRenewableToken; + } return await Expand(connection, accessToken, nodePath, serverInfo, options, filters); } } @@ -98,7 +106,7 @@ namespace Microsoft.SqlTools.SqlCore.ObjectExplorer } } - + /// /// Expands the given node and returns the child nodes. /// @@ -120,10 +128,11 @@ namespace Microsoft.SqlTools.SqlCore.ObjectExplorer { SmoQueryContext nodeContext = node.GetContextAs() ?? throw new ArgumentException("Node does not have a valid context"); - if(options.GroupBySchemaFlagGetter != null) + if (options.GroupBySchemaFlagGetter != null) { nodeContext.GroupBySchemaFlag = options.GroupBySchemaFlagGetter; } + if (!nodeContext.Server.ConnectionContext.IsOpen && securityToken != null) { var underlyingSqlConnection = nodeContext.Server.ConnectionContext.SqlConnectionObject;