Skip to content

Commit 0a6c5f1

Browse files
committed
Add authentication parameters
1 parent 7898cb2 commit 0a6c5f1

File tree

6 files changed

+35
-7
lines changed

6 files changed

+35
-7
lines changed

doc/snippets/Microsoft.Data.SqlClient/SSPIContextProvider.xml

+3
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,8 @@
1212
<param name="input">Received buffer, if any.</param>
1313
<returns>A memory owned type with the response of the client.</returns>
1414
</GenerateSspiClientContext>
15+
<AuthenticationParameters>
16+
<summary>Gets the authentication parameters for the SSPI context.</summary>
17+
</AuthenticationParameters>
1518
</members>
1619
</docs>

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

+3
Original file line numberDiff line numberDiff line change
@@ -957,6 +957,9 @@ public abstract class SSPIContextProvider
957957
/// <include file='../../../../doc/snippets/Microsoft.Data.SqlClient/SSPIContextProvider.xml' path='docs/members[@name="SSPIContextProvider"]/ServerNames/*' />
958958
public System.Collections.Generic.IReadOnlyList<string> ServerNames { get { throw null; } }
959959

960+
/// <include file='../../../../doc/snippets/Microsoft.Data.SqlClient/SSPIContextProvider.xml' path='docs/members[@name="SSPIContextProvider"]/AuthenticationParameters/*' />
961+
public SqlAuthenticationParameters AuthenticationParameters { get { throw null; } }
962+
960963
/// <include file='../../../../doc/snippets/Microsoft.Data.SqlClient/SSPIContextProvider.xml' path='docs/members[@name="SSPIContextProvider"]/GenerateSspiClientContext/*' />
961964
protected abstract System.Buffers.IMemoryOwner<byte> GenerateSspiClientContext(System.ReadOnlyMemory<byte> input);
962965
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ internal void Connect(
468468
hostNameInCertificate,
469469
serverCertificateFilename);
470470

471-
_authenticationProvider?.Initialize(serverInfo, _physicalStateObj, this, _sniSpn);
471+
_authenticationProvider?.Initialize(serverInfo, _physicalStateObj, this, _sniSpn, _connHandler);
472472

473473
if (TdsEnums.SNI_SUCCESS != _physicalStateObj.Status)
474474
{
@@ -574,7 +574,7 @@ internal void Connect(
574574
ThrowExceptionAndWarning(_physicalStateObj);
575575
}
576576

577-
_authenticationProvider?.Initialize(serverInfo, _physicalStateObj, this, _sniSpn);
577+
_authenticationProvider?.Initialize(serverInfo, _physicalStateObj, this, _sniSpn, _connHandler);
578578

579579
uint retCode = _physicalStateObj.SniGetConnectionId(ref _connHandler._clientConnectionId);
580580

src/Microsoft.Data.SqlClient/netfx/ref/Microsoft.Data.SqlClient.cs

+3
Original file line numberDiff line numberDiff line change
@@ -897,6 +897,9 @@ public abstract class SSPIContextProvider
897897
/// <include file='../../../../doc/snippets/Microsoft.Data.SqlClient/SSPIContextProvider.xml' path='docs/members[@name="SSPIContextProvider"]/ServerNames/*' />
898898
public System.Collections.Generic.IReadOnlyList<string> ServerNames { get { throw null; } }
899899

900+
/// <include file='../../../../doc/snippets/Microsoft.Data.SqlClient/SSPIContextProvider.xml' path='docs/members[@name="SSPIContextProvider"]/AuthenticationParameters/*' />
901+
public SqlAuthenticationParameters AuthenticationParameters { get { throw null; } }
902+
900903
/// <include file='../../../../doc/snippets/Microsoft.Data.SqlClient/SSPIContextProvider.xml' path='docs/members[@name="SSPIContextProvider"]/GenerateSspiClientContext/*' />
901904
protected abstract System.Buffers.IMemoryOwner<byte> GenerateSspiClientContext(System.ReadOnlyMemory<byte> input);
902905
}

src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParser.cs

+2-4
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,7 @@ internal void Connect(ServerInfo serverInfo,
649649
FQDNforDNSCache,
650650
hostNameInCertificate);
651651

652-
_authenticationProvider?.Initialize(serverInfo, _physicalStateObj, this, new[] { _sniSpn });
652+
_authenticationProvider?.Initialize(serverInfo, _physicalStateObj, this, new[] { _sniSpn }, _connHandler);
653653

654654
if (TdsEnums.SNI_SUCCESS != _physicalStateObj.Status)
655655
{
@@ -756,8 +756,6 @@ internal void Connect(ServerInfo serverInfo,
756756
serverInfo.ResolvedServerName,
757757
hostNameInCertificate);
758758

759-
_authenticationProvider?.Initialize(serverInfo, _physicalStateObj, this, new[] { _sniSpn });
760-
761759
if (TdsEnums.SNI_SUCCESS != _physicalStateObj.Status)
762760
{
763761
_physicalStateObj.AddError(ProcessSNIError(_physicalStateObj));
@@ -766,7 +764,7 @@ internal void Connect(ServerInfo serverInfo,
766764
ThrowExceptionAndWarning(_physicalStateObj);
767765
}
768766

769-
_authenticationProvider?.Initialize(serverInfo, _physicalStateObj, this, new[] { _sniSpn });
767+
_authenticationProvider?.Initialize(serverInfo, _physicalStateObj, this, new[] { _sniSpn }, _connHandler);
770768

771769
UInt32 retCode = SNINativeMethodWrapper.SniGetConnectionId(_physicalStateObj.Handle, ref _connHandler._clientConnectionId);
772770
Debug.Assert(retCode == TdsEnums.SNI_SUCCESS, "Unexpected failure state upon calling SniGetConnectionId");

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

+22-1
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,43 @@ public abstract class SSPIContextProvider
1616
private protected TdsParserStateObject _physicalStateObj = null!;
1717
private string[] _serverNames = Array.Empty<string>();
1818

19-
internal void Initialize(ServerInfo serverInfo, TdsParserStateObject physicalStateObj, TdsParser parser, string[] serverNames)
19+
internal void Initialize(ServerInfo serverInfo, TdsParserStateObject physicalStateObj, TdsParser parser, string[] serverNames, SqlInternalConnectionTds connection)
2020
{
2121
_parser = parser;
2222
_physicalStateObj = physicalStateObj;
2323
_serverInfo = serverInfo;
2424
_serverNames = serverNames;
25+
AuthenticationParameters = InitializeAuthenticationParameters(connection);
2526

2627
Debug.Assert(_serverNames.Length > 0);
2728

2829
Initialize();
2930
}
3031

32+
private SqlAuthenticationParameters InitializeAuthenticationParameters(SqlInternalConnectionTds connection)
33+
{
34+
var auth = new SqlAuthenticationParameters.Builder(connection.ConnectionOptions.Authentication, "resource", "auth", connection.ConnectionOptions.ObtainWorkstationId(), connection.ConnectionOptions.InitialCatalog);
35+
36+
if (connection.ConnectionOptions.UserID is { } userId)
37+
{
38+
auth.WithUserId(userId);
39+
}
40+
41+
if (connection.ConnectionOptions.Password is { } password)
42+
{
43+
auth.WithPassword(password);
44+
}
45+
46+
return auth;
47+
}
48+
3149
private protected virtual void Initialize()
3250
{
3351
}
3452

53+
/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient/SSPIContextProvider.xml' path='docs/members[@name="SSPIContextProvider"]/AuthenticationParameters/*' />
54+
public SqlAuthenticationParameters AuthenticationParameters { get; private set; } = null!;
55+
3556
/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient/SSPIContextProvider.xml' path='docs/members[@name="SSPIContextProvider"]/ServerNames/*' />
3657
public IReadOnlyList<string> ServerNames => _serverNames;
3758

0 commit comments

Comments
 (0)