Skip to content

Commit 3697d08

Browse files
authored
Merge ca7841a into f7ab115
2 parents f7ab115 + ca7841a commit 3697d08

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

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

+17-3
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,27 @@ 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+
string spnName = Encoding.Unicode.GetString(_sniSpnBuffer[i]);
22+
_negotiateAuth ??= new(new NegotiateAuthenticationClientOptions { Package = "Negotiate", TargetName = spnName });
23+
sendBuff = _negotiateAuth.GetOutgoingBlob(received.Span, out statusCode)!;
24+
// Log session id, status code and the actual SPN used in the negotiation
25+
SqlClientEventSource.Log.TryTraceEvent($"TdsParserStateObjectManaged.GenerateSspiClientContext | Info | Session Id {_physicalStateObj.SessionId}, StatusCode={statusCode}, SPN={_negotiateAuth.TargetName}");
26+
27+
if (statusCode == NegotiateAuthenticationStatusCode.Completed || statusCode == NegotiateAuthenticationStatusCode.ContinueNeeded)
28+
break; // Successful case, exit the loop with current SPN.
29+
else
30+
_negotiateAuth = null; // Reset _negotiateAuth to be generated again for next SPN.
31+
}
32+
2033
if (statusCode is not NegotiateAuthenticationStatusCode.Completed and not NegotiateAuthenticationStatusCode.ContinueNeeded)
2134
{
2235
throw new InvalidOperationException(SQLMessage.SSPIGenerateError() + Environment.NewLine + statusCode);
2336
}
37+
2438
sendLength = (uint)(sendBuff != null ? sendBuff.Length : 0);
2539
}
2640
}

0 commit comments

Comments
 (0)