Skip to content

Commit 120662f

Browse files
committed
Expose SqlAuthenticationParameters on SSPIContextProvider
1 parent 89d85fb commit 120662f

File tree

4 files changed

+41
-13
lines changed

4 files changed

+41
-13
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@ internal sealed class ManagedSSPIContextProvider : SSPIContextProvider
1212
{
1313
private SspiClientContextStatus? _sspiClientContextStatus;
1414

15-
protected override void GenerateSspiClientContext(ReadOnlySpan<byte> incomingBlob, IBufferWriter<byte> outgoingBlobWriter, string[] serverNames)
15+
protected override bool GenerateSspiClientContext(ReadOnlySpan<byte> incomingBlob, IBufferWriter<byte> outgoingBlobWriter, SqlAuthenticationParameters authParams, ReadOnlySpan<string> serverNames)
1616
{
1717
_sspiClientContextStatus ??= new SspiClientContextStatus();
18-
SNIProxy.GenSspiClientContext(_sspiClientContextStatus, incomingBlob, outgoingBlobWriter, serverNames);
18+
SNIProxy.GenSspiClientContext(_sspiClientContextStatus, incomingBlob, outgoingBlobWriter, serverNames.ToArray());
1919
SqlClientEventSource.Log.TryTraceEvent("{0}.{1} | Info | Session Id {2}", nameof(ManagedSSPIContextProvider), nameof(GenerateSspiClientContext), _physicalStateObj.SessionId);
20+
return true;
2021
}
2122
}
2223
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ private void LoadSSPILibrary()
4949
}
5050
}
5151

52-
protected override void GenerateSspiClientContext(ReadOnlySpan<byte> incomingBlob, IBufferWriter<byte> outgoingBlobWriter, string[] _sniSpnBuffer)
52+
protected override bool GenerateSspiClientContext(ReadOnlySpan<byte> incomingBlob, IBufferWriter<byte> outgoingBlobWriter, SqlAuthenticationParameters authParams, ReadOnlySpan<string> serverNames)
5353
{
5454
#if NETFRAMEWORK
5555
SNIHandle handle = _physicalStateObj.Handle;
@@ -60,7 +60,7 @@ protected override void GenerateSspiClientContext(ReadOnlySpan<byte> incomingBlo
6060

6161
var outBuff = outgoingBlobWriter.GetSpan((int)s_maxSSPILength);
6262

63-
if (0 != SNINativeMethodWrapper.SNISecGenClientContext(handle, incomingBlob, outBuff, out var sendLength, _sniSpnBuffer[0]))
63+
if (0 != SNINativeMethodWrapper.SNISecGenClientContext(handle, incomingBlob, outBuff, out var sendLength, serverNames[0]))
6464
{
6565
throw new InvalidOperationException(SQLMessage.SSPIGenerateError());
6666
}
@@ -71,6 +71,8 @@ protected override void GenerateSspiClientContext(ReadOnlySpan<byte> incomingBlo
7171
}
7272

7373
outgoingBlobWriter.Advance((int)sendLength);
74+
75+
return true;
7476
}
7577
}
7678
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
#if NET8_0_OR_GREATER
22

33
using System;
4-
using System.Text;
5-
using System.Net.Security;
64
using System.Buffers;
5+
using System.Net.Security;
6+
using System.Text;
77

88
#nullable enable
99

1010
namespace Microsoft.Data.SqlClient
1111
{
1212
internal sealed class NegotiateSSPIContextProvider : SSPIContextProvider
1313
{
14-
protected override void GenerateSspiClientContext(ReadOnlySpan<byte> incomingBlob, IBufferWriter<byte> outgoingBlobWriter, string[] serverNames)
14+
protected override bool GenerateSspiClientContext(ReadOnlySpan<byte> incomingBlob, IBufferWriter<byte> outgoingBlobWriter, SqlAuthenticationParameters authParams, ReadOnlySpan<string> serverNames)
1515
{
1616
NegotiateAuthenticationStatusCode statusCode = default;
1717

@@ -27,7 +27,7 @@ protected override void GenerateSspiClientContext(ReadOnlySpan<byte> incomingBlo
2727
if (statusCode == NegotiateAuthenticationStatusCode.Completed || statusCode == NegotiateAuthenticationStatusCode.ContinueNeeded)
2828
{
2929
outgoingBlobWriter.Write(result);
30-
return;
30+
return true;
3131
}
3232
}
3333

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

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,21 @@ private protected virtual void Initialize()
2626
{
2727
}
2828

29-
protected abstract void GenerateSspiClientContext(ReadOnlySpan<byte> incomingBlob, IBufferWriter<byte> outgoingBlobWriter, string[] _sniSpnBuffer);
29+
protected abstract bool GenerateSspiClientContext(ReadOnlySpan<byte> incomingBlob, IBufferWriter<byte> outgoingBlobWriter, SqlAuthenticationParameters authParams, ReadOnlySpan<string> serverNames);
3030

31-
internal void SSPIData(ReadOnlySpan<byte> receivedBuff, IBufferWriter<byte> outgoingBlobWriter, string sniSpnBuffer)
32-
=> SSPIData(receivedBuff, outgoingBlobWriter, new[] { sniSpnBuffer });
31+
internal void SSPIData(ReadOnlySpan<byte> receivedBuff, IBufferWriter<byte> outgoingBlobWriter, string serverNames)
32+
=> SSPIData(receivedBuff, outgoingBlobWriter, new[] { serverNames });
3333

34-
internal void SSPIData(ReadOnlySpan<byte> receivedBuff, IBufferWriter<byte> outgoingBlobWriter, string[] sniSpnBuffer)
34+
internal void SSPIData(ReadOnlySpan<byte> receivedBuff, IBufferWriter<byte> outgoingBlobWriter, string[] serverNames)
3535
{
3636
using (TrySNIEventScope.Create(nameof(SSPIContextProvider)))
3737
{
3838
try
3939
{
40-
GenerateSspiClientContext(receivedBuff, outgoingBlobWriter, sniSpnBuffer);
40+
if (GenerateSspiClientContext(receivedBuff, outgoingBlobWriter, CreateSqlAuthParams(_parser.Connection, serverNames[0]), serverNames))
41+
{
42+
return;
43+
}
4144
}
4245
catch (Exception e)
4346
{
@@ -46,6 +49,28 @@ internal void SSPIData(ReadOnlySpan<byte> receivedBuff, IBufferWriter<byte> outg
4649
}
4750
}
4851

52+
private static SqlAuthenticationParameters CreateSqlAuthParams(SqlInternalConnectionTds connection, string serverName)
53+
{
54+
var auth = new SqlAuthenticationParameters.Builder(
55+
authenticationMethod: connection.ConnectionOptions.Authentication,
56+
resource: null,
57+
authority: null,
58+
serverName: serverName,
59+
connection.ConnectionOptions.InitialCatalog);
60+
61+
if (connection.ConnectionOptions.UserID is { } userId)
62+
{
63+
auth.WithUserId(userId);
64+
}
65+
66+
if (connection.ConnectionOptions.Password is { } password)
67+
{
68+
auth.WithPassword(password);
69+
}
70+
71+
return auth;
72+
}
73+
4974
protected void SSPIError(string error, string procedure)
5075
{
5176
Debug.Assert(!ADP.IsEmpty(procedure), "TdsParser.SSPIError called with an empty or null procedure string");

0 commit comments

Comments
 (0)