Skip to content

Commit

Permalink
Add null checks for connection properties (#2137)
Browse files Browse the repository at this point in the history
  • Loading branch information
cheenamalhotra committed Jul 12, 2023
1 parent 26c66b8 commit 63c43bd
Showing 1 changed file with 29 additions and 10 deletions.
39 changes: 29 additions & 10 deletions src/Microsoft.SqlTools.ServiceLayer/Connection/ConnectionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1410,31 +1410,50 @@ public static SqlConnectionStringBuilder CreateConnectionStringBuilder(Connectio
connectionBuilder.IntegratedSecurity = true;
break;
case SqlLogin:
connectionBuilder.UserID = connectionDetails.UserName;
connectionBuilder.Password = string.IsNullOrEmpty(connectionDetails.Password)
// Don't erase username from connection string.
if (string.IsNullOrEmpty(connectionBuilder.UserID))
{
connectionBuilder.UserID = connectionDetails.UserName;
}
// Don't erase password from connection string.
if (string.IsNullOrEmpty(connectionBuilder.Password))
{
connectionBuilder.Password = string.IsNullOrEmpty(connectionDetails.Password)
? string.Empty // Support empty password for accounts without password
: connectionDetails.Password;
}
connectionBuilder.Authentication = SqlAuthenticationMethod.SqlPassword;
break;
case AzureMFA:
if (Instance.EnableSqlAuthenticationProvider)
{
connectionBuilder.UserID = connectionDetails.UserName;
if (string.IsNullOrEmpty(connectionBuilder.UserID))
{
connectionBuilder.UserID = connectionDetails.UserName;
}
connectionDetails.AuthenticationType = ActiveDirectoryInteractive;
connectionBuilder.Authentication = SqlAuthenticationMethod.ActiveDirectoryInteractive;
}
else
{
connectionBuilder.UserID = "";
}
break;
case ActiveDirectoryInteractive:
connectionBuilder.UserID = connectionDetails.UserName;
// Don't erase username from connection string.
if (string.IsNullOrEmpty(connectionBuilder.UserID))
{
connectionBuilder.UserID = connectionDetails.UserName;
}
connectionBuilder.Authentication = SqlAuthenticationMethod.ActiveDirectoryInteractive;
break;
case ActiveDirectoryPassword:
connectionBuilder.UserID = connectionDetails.UserName;
connectionBuilder.Password = connectionDetails.Password;
// Don't erase username from connection string.
if (string.IsNullOrEmpty(connectionBuilder.UserID))
{
connectionBuilder.UserID = connectionDetails.UserName;
}
// Don't erase password from connection string.
if (string.IsNullOrEmpty(connectionBuilder.Password))
{
connectionBuilder.Password = connectionDetails.Password;
}
connectionBuilder.Authentication = SqlAuthenticationMethod.ActiveDirectoryPassword;
break;
default:
Expand Down

0 comments on commit 63c43bd

Please sign in to comment.