Skip to content

Commit 8401a77

Browse files
arelleguemdaigle
authored andcommitted
Fix | Fixed GenerateSspiClientContext to retry negotiation with default port (#2559)
1 parent 6c48d05 commit 8401a77

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

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

+15-3
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,25 @@ internal sealed class NegotiateSSPIContextProvider : SSPIContextProvider
1414

1515
internal override void GenerateSspiClientContext(ReadOnlyMemory<byte> received, ref byte[] sendBuff, ref uint sendLength, byte[][] _sniSpnBuffer)
1616
{
17-
_negotiateAuth ??= new(new NegotiateAuthenticationClientOptions { Package = "Negotiate", TargetName = Encoding.Unicode.GetString(_sniSpnBuffer[0]) });
18-
sendBuff = _negotiateAuth.GetOutgoingBlob(received.Span, out NegotiateAuthenticationStatusCode statusCode)!;
19-
SqlClientEventSource.Log.TryTraceEvent("TdsParserStateObjectManaged.GenerateSspiClientContext | Info | Session Id {0}, StatusCode={1}", _physicalStateObj.SessionId, statusCode);
17+
NegotiateAuthenticationStatusCode statusCode = NegotiateAuthenticationStatusCode.UnknownCredentials;
18+
19+
for (int i = 0; i < _sniSpnBuffer.Length; i++)
20+
{
21+
_negotiateAuth ??= new(new NegotiateAuthenticationClientOptions { Package = "Negotiate", TargetName = Encoding.Unicode.GetString(_sniSpnBuffer[i]) });
22+
sendBuff = _negotiateAuth.GetOutgoingBlob(received.Span, out statusCode)!;
23+
// Log session id, status code and the actual SPN used in the negotiation
24+
SqlClientEventSource.Log.TryTraceEvent("TdsParserStateObjectManaged.GenerateSspiClientContext | Info | Session Id {0}, StatusCode={1}, SPN={2}", _physicalStateObj.SessionId, statusCode, _negotiateAuth.TargetName);
25+
if (statusCode == NegotiateAuthenticationStatusCode.Completed || statusCode == NegotiateAuthenticationStatusCode.ContinueNeeded)
26+
break; // Successful case, exit the loop with current SPN.
27+
else
28+
_negotiateAuth = null; // Reset _negotiateAuth to be generated again for next SPN.
29+
}
30+
2031
if (statusCode is not NegotiateAuthenticationStatusCode.Completed and not NegotiateAuthenticationStatusCode.ContinueNeeded)
2132
{
2233
throw new InvalidOperationException(SQLMessage.SSPIGenerateError() + Environment.NewLine + statusCode);
2334
}
35+
2436
sendLength = (uint)(sendBuff != null ? sendBuff.Length : 0);
2537
}
2638
}

0 commit comments

Comments
 (0)