Skip to content

Commit 08059dd

Browse files
Fix | Disable encryption when connecting to SQL Local DB (#1312)
1 parent 6af83fc commit 08059dd

File tree

3 files changed

+50
-3
lines changed

3 files changed

+50
-3
lines changed

src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,13 @@ internal void Connect(
396396
authType == SqlAuthenticationMethod.NotSpecified ? SqlAuthenticationMethod.SqlPassword.ToString() : authType.ToString());
397397
}
398398

399+
// Encryption is not supported on SQL Local DB - disable it for current session.
400+
if (connHandler.ConnectionOptions.LocalDBInstance != null && encrypt)
401+
{
402+
encrypt = false;
403+
SqlClientEventSource.Log.TryTraceEvent("<sc.TdsParser.Connect|SEC> Encryption will be disabled as target server is a SQL Local DB instance.");
404+
}
405+
399406
_sniSpnBuffer = null;
400407

401408
// AD Integrated behaves like Windows integrated when connecting to a non-fedAuth server

src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParser.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,15 @@ internal void Connect(ServerInfo serverInfo,
529529

530530
//Create LocalDB instance if necessary
531531
if (connHandler.ConnectionOptions.LocalDBInstance != null)
532+
{
532533
LocalDBAPI.CreateLocalDBInstance(connHandler.ConnectionOptions.LocalDBInstance);
534+
if (encrypt)
535+
{
536+
// Encryption is not supported on SQL Local DB - disable it for current session.
537+
encrypt = false;
538+
SqlClientEventSource.Log.TryTraceEvent("<sc.TdsParser.Connect|SEC> Encryption will be disabled as target server is a SQL Local DB instance.");
539+
}
540+
}
533541

534542
// AD Integrated behaves like Windows integrated when connecting to a non-fedAuth server
535543
if (integratedSecurity || authType == SqlAuthenticationMethod.ActiveDirectoryIntegrated)

src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/LocalDBTest/LocalDBTest.cs

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,15 @@ public static void SqlLocalDbConnectionTest()
2323
ConnectionTest(s_localDbConnectionString);
2424
}
2525

26+
[SkipOnTargetFramework(TargetFrameworkMonikers.Uap)] // No Registry support on UAP
27+
[ConditionalFact(nameof(IsLocalDBEnvironmentSet))]
28+
public static void LocalDBEncryptionNotSupportedTest()
29+
{
30+
// Encryption is not supported by SQL Local DB.
31+
// But connection should succeed as encryption is disabled by driver.
32+
ConnectionWithEncryptionTest(s_localDbConnectionString);
33+
}
34+
2635
[SkipOnTargetFramework(TargetFrameworkMonikers.Uap)] // No Registry support on UAP
2736
[ConditionalFact(nameof(IsLocalDBEnvironmentSet))]
2837
public static void LocalDBMarsTest()
@@ -40,6 +49,18 @@ public static void InvalidLocalDBTest()
4049
#endregion
4150

4251
#region SharedLocalDb tests
52+
[SkipOnTargetFramework(TargetFrameworkMonikers.Uap)] // No Registry support on UAP
53+
[ConditionalFact(nameof(IsLocalDbSharedInstanceSet))]
54+
public static void SharedLocalDbEncryptionTest()
55+
{
56+
foreach (string connectionString in s_sharedLocalDbInstances)
57+
{
58+
// Encryption is not supported by SQL Local DB.
59+
// But connection should succeed as encryption is disabled by driver.
60+
ConnectionWithEncryptionTest(connectionString);
61+
}
62+
}
63+
4364
[SkipOnTargetFramework(TargetFrameworkMonikers.Uap)] // No Registry support on UAP
4465
[ConditionalFact(nameof(IsLocalDbSharedInstanceSet))]
4566
public static void SharedLocalDbMarsTest()
@@ -67,18 +88,28 @@ private static void ConnectionWithMarsTest(string connectionString)
6788
{
6889
IntegratedSecurity = true,
6990
MultipleActiveResultSets = true,
91+
ConnectTimeout = 2
92+
};
93+
OpenConnection(builder.ConnectionString);
94+
}
95+
96+
private static void ConnectionWithEncryptionTest(string connectionString)
97+
{
98+
SqlConnectionStringBuilder builder = new(connectionString)
99+
{
100+
IntegratedSecurity = true,
70101
ConnectTimeout = 2,
71-
Encrypt = false
102+
Encrypt = true
72103
};
73104
OpenConnection(builder.ConnectionString);
74105
}
106+
75107
private static void ConnectionTest(string connectionString)
76108
{
77109
SqlConnectionStringBuilder builder = new(connectionString)
78110
{
79111
IntegratedSecurity = true,
80-
ConnectTimeout = 2,
81-
Encrypt = false
112+
ConnectTimeout = 2
82113
};
83114
OpenConnection(builder.ConnectionString);
84115
}
@@ -87,6 +118,7 @@ private static void OpenConnection(string connString)
87118
{
88119
using SqlConnection connection = new(connString);
89120
connection.Open();
121+
Assert.Equal(System.Data.ConnectionState.Open, connection.State);
90122
using SqlCommand command = new SqlCommand("SELECT @@SERVERNAME", connection);
91123
var result = command.ExecuteScalar();
92124
Assert.NotNull(result);

0 commit comments

Comments
 (0)