Skip to content

Commit b5edb42

Browse files
authored
[5.2] | Fix GenerateSspiClientContext to retry negotiation with default port (#2815)
1 parent da57fa9 commit b5edb42

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -396,9 +396,20 @@ internal override uint GenerateSspiClientContext(byte[] receivedBuff,
396396
byte[][] _sniSpnBuffer)
397397
{
398398
#if NET7_0_OR_GREATER
399-
_negotiateAuth ??= new(new NegotiateAuthenticationClientOptions { Package = "Negotiate", TargetName = Encoding.Unicode.GetString(_sniSpnBuffer[0]) });
400-
sendBuff = _negotiateAuth.GetOutgoingBlob(receivedBuff, out NegotiateAuthenticationStatusCode statusCode)!;
401-
SqlClientEventSource.Log.TryTraceEvent("TdsParserStateObjectManaged.GenerateSspiClientContext | Info | Session Id {0}, StatusCode={1}", _sessionHandle?.ConnectionId, statusCode);
399+
NegotiateAuthenticationStatusCode statusCode = NegotiateAuthenticationStatusCode.UnknownCredentials;
400+
401+
for (int i = 0; i < _sniSpnBuffer.Length; i++)
402+
{
403+
_negotiateAuth ??= new(new NegotiateAuthenticationClientOptions { Package = "Negotiate", TargetName = Encoding.Unicode.GetString(_sniSpnBuffer[i]) });
404+
sendBuff = _negotiateAuth.GetOutgoingBlob(receivedBuff, out statusCode)!;
405+
// Log session id, status code and the actual SPN used in the negotiation
406+
SqlClientEventSource.Log.TryTraceEvent("TdsParserStateObjectManaged.GenerateSspiClientContext | Info | Session Id {0}, StatusCode={1}, SPN={2}", _sessionHandle?.ConnectionId, statusCode, _negotiateAuth.TargetName);
407+
if (statusCode == NegotiateAuthenticationStatusCode.Completed || statusCode == NegotiateAuthenticationStatusCode.ContinueNeeded)
408+
break; // Successful case, exit the loop with current SPN.
409+
else
410+
_negotiateAuth = null; // Reset _negotiateAuth to be generated again for next SPN.
411+
}
412+
402413
if (statusCode is not NegotiateAuthenticationStatusCode.Completed and not NegotiateAuthenticationStatusCode.ContinueNeeded)
403414
{
404415
throw new InvalidOperationException(SQLMessage.SSPIGenerateError() + Environment.NewLine + statusCode);

0 commit comments

Comments
 (0)