Skip to content

Commit dfeb360

Browse files
committed
Port netcore _serverSpn array to netfx
1 parent 3b8f850 commit dfeb360

File tree

3 files changed

+26
-28
lines changed

3 files changed

+26
-28
lines changed

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

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ internal sealed partial class TdsParser
121121

122122
private bool _is2022 = false;
123123

124-
private string _serverSpn = null;
124+
private string[] _serverSpn = null;
125125

126126
// SqlStatistics
127127
private SqlStatistics _statistics = null;
@@ -395,6 +395,12 @@ internal void Connect(ServerInfo serverInfo,
395395
ThrowExceptionAndWarning(_physicalStateObj);
396396
Debug.Fail("SNI returned status != success, but no error thrown?");
397397
}
398+
else
399+
{
400+
_serverSpn = null;
401+
SqlClientEventSource.Log.TryTraceEvent("TdsParser.Connect | SEC | Connection Object Id {0}, Authentication Mode: {1}", _connHandler.ObjectID,
402+
authType == SqlAuthenticationMethod.NotSpecified ? SqlAuthenticationMethod.SqlPassword.ToString() : authType.ToString());
403+
}
398404

399405
//Create LocalDB instance if necessary
400406
if (connHandler.ConnectionOptions.LocalDBInstance != null)
@@ -408,28 +414,17 @@ internal void Connect(ServerInfo serverInfo,
408414
}
409415
}
410416

417+
_serverSpn = null;
418+
411419
// AD Integrated behaves like Windows integrated when connecting to a non-fedAuth server
412420
if (integratedSecurity || authType == SqlAuthenticationMethod.ActiveDirectoryIntegrated)
413421
{
414422
_authenticationProvider = _physicalStateObj.CreateSspiContextProvider();
415-
416-
if (!string.IsNullOrEmpty(serverInfo.ServerSPN))
417-
{
418-
_serverSpn = serverInfo.ServerSPN;
419-
SqlClientEventSource.Log.TryTraceEvent("<sc.TdsParser.Connect|SEC> Server SPN `{0}` from the connection string is used.", serverInfo.ServerSPN);
420-
}
421-
else
422-
{
423-
// Empty signifies to interop layer that SPN needs to be generated
424-
_serverSpn = string.Empty;
425-
}
426-
427423
SqlClientEventSource.Log.TryTraceEvent("<sc.TdsParser.Connect|SEC> SSPI or Active Directory Authentication Library for SQL Server based integrated authentication");
428424
}
429425
else
430426
{
431427
_authenticationProvider = null;
432-
_serverSpn = null;
433428

434429
switch (authType)
435430
{
@@ -614,7 +609,7 @@ internal void Connect(ServerInfo serverInfo,
614609
transparentNetworkResolutionState,
615610
totalTimeout,
616611
_connHandler.ConnectionOptions.IPAddressPreference,
617-
serverInfo.ResolvedServerName,
612+
FQDNforDNSCache,
618613
ref _connHandler.pendingSQLDNSObject,
619614
serverInfo.ServerSPN,
620615
integratedSecurity,

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ internal void CreatePhysicalSNIHandle(
108108
string serverName,
109109
TimeoutTimer timeout,
110110
out byte[] instanceName,
111-
ref string spn,
111+
ref string[] spns,
112112
bool flushCache,
113113
bool async,
114114
bool fParallel,
@@ -123,16 +123,30 @@ internal void CreatePhysicalSNIHandle(
123123
string hostNameInCertificate = "",
124124
string serverCertificateFilename = "")
125125
{
126+
if (isIntegratedSecurity)
127+
{
128+
if (!string.IsNullOrEmpty(serverSPN))
129+
{
130+
SqlClientEventSource.Log.TryTraceEvent("<sc.TdsParser.Connect|SEC> Server SPN `{0}` from the connection string is used.", serverSPN);
131+
}
132+
else
133+
{
134+
// Empty signifies to interop layer that SPN needs to be generated
135+
serverSPN = string.Empty;
136+
}
137+
}
138+
126139
ConsumerInfo myInfo = CreateConsumerInfo(async);
127140

128141
// serverName : serverInfo.ExtendedServerName
129142
// may not use this serverName as key
130143

131144
_ = SQLFallbackDNSCache.Instance.GetDNSInfo(cachedFQDN, out SQLDNSInfo cachedDNSInfo);
132145

133-
_sessionHandle = new SNIHandle(myInfo, serverName, ref spn, timeout.MillisecondsRemainingInt,
146+
_sessionHandle = new SNIHandle(myInfo, serverName, ref serverSPN, timeout.MillisecondsRemainingInt,
134147
out instanceName, flushCache, !async, fParallel, transparentNetworkResolutionState, totalTimeout,
135148
iPAddressPreference, cachedDNSInfo, hostNameInCertificate);
149+
spns = new[] { serverSPN.TrimEnd() };
136150
}
137151

138152
internal uint CheckConnection() => SniNativeWrapper.SniCheckConnection(Handle);

src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SSPI/SspiContextProvider.cs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,6 @@ private protected virtual void Initialize()
2727

2828
protected abstract bool GenerateSspiClientContext(ReadOnlySpan<byte> incomingBlob, IBufferWriter<byte> outgoingBlobWriter, SspiAuthenticationParameters authParams);
2929

30-
internal void SSPIData(ReadOnlySpan<byte> receivedBuff, IBufferWriter<byte> outgoingBlobWriter, string serverSpn)
31-
{
32-
using var _ = TrySNIEventScope.Create(nameof(SspiContextProvider));
33-
34-
if (!RunGenerateSspiClientContext(receivedBuff, outgoingBlobWriter, serverSpn))
35-
{
36-
// If we've hit here, the SSPI context provider implementation failed to generate the SSPI context.
37-
SSPIError(SQLMessage.SSPIGenerateError(), TdsEnums.GEN_CLIENT_CONTEXT);
38-
}
39-
}
40-
4130
internal void SSPIData(ReadOnlySpan<byte> receivedBuff, IBufferWriter<byte> outgoingBlobWriter, ReadOnlySpan<string> serverSpns)
4231
{
4332
using var _ = TrySNIEventScope.Create(nameof(SspiContextProvider));

0 commit comments

Comments
 (0)