Skip to content

Commit 09db047

Browse files
committed
revert
1 parent 7e4d15f commit 09db047

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

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

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,34 @@ namespace Microsoft.Data.SqlClient
1111
{
1212
internal sealed class NegotiateSSPIContextProvider : SSPIContextProvider
1313
{
14+
private NegotiateAuthentication? _negotiateAuth = null;
15+
1416
protected override void GenerateSspiClientContext(ReadOnlySpan<byte> incomingBlob, IBufferWriter<byte> outgoingBlobWriter, byte[][] _sniSpnBuffer)
1517
{
16-
NegotiateAuthenticationStatusCode statusCode = default;
18+
NegotiateAuthenticationStatusCode statusCode = NegotiateAuthenticationStatusCode.UnknownCredentials;
1719

1820
for (int i = 0; i < _sniSpnBuffer.Length; i++)
1921
{
20-
var negotiateAuth = new NegotiateAuthentication(new NegotiateAuthenticationClientOptions { Package = "Negotiate", TargetName = Encoding.Unicode.GetString(_sniSpnBuffer[i]) });
21-
var result = negotiateAuth.GetOutgoingBlob(incomingBlob, out statusCode);
22-
22+
_negotiateAuth ??= new(new NegotiateAuthenticationClientOptions { Package = "Negotiate", TargetName = Encoding.Unicode.GetString(_sniSpnBuffer[i]) });
23+
var sendBuff = _negotiateAuth.GetOutgoingBlob(incomingBlob, out statusCode)!;
2324
// Log session id, status code and the actual SPN used in the negotiation
2425
SqlClientEventSource.Log.TryTraceEvent("{0}.{1} | Info | Session Id {2}, StatusCode={3}, SPN={4}", nameof(NegotiateSSPIContextProvider),
25-
nameof(GenerateSspiClientContext), _physicalStateObj.SessionId, statusCode, negotiateAuth.TargetName);
26-
26+
nameof(GenerateSspiClientContext), _physicalStateObj.SessionId, statusCode, _negotiateAuth.TargetName);
2727
if (statusCode == NegotiateAuthenticationStatusCode.Completed || statusCode == NegotiateAuthenticationStatusCode.ContinueNeeded)
2828
{
29-
outgoingBlobWriter.Write(result);
30-
return;
29+
outgoingBlobWriter.Write(sendBuff);
30+
break; // Successful case, exit the loop with current SPN.
31+
}
32+
else
33+
{
34+
_negotiateAuth = null; // Reset _negotiateAuth to be generated again for next SPN.
3135
}
3236
}
3337

34-
throw new InvalidOperationException(SQLMessage.SSPIGenerateError() + Environment.NewLine + statusCode);
38+
if (statusCode is not NegotiateAuthenticationStatusCode.Completed and not NegotiateAuthenticationStatusCode.ContinueNeeded)
39+
{
40+
throw new InvalidOperationException(SQLMessage.SSPIGenerateError() + Environment.NewLine + statusCode);
41+
}
3542
}
3643
}
3744
}

0 commit comments

Comments
 (0)