Skip to content

Commit 396cd0b

Browse files
net fx draft
1 parent 865ac03 commit 396cd0b

File tree

5 files changed

+115
-22
lines changed

5 files changed

+115
-22
lines changed

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

+15-11
Original file line numberDiff line numberDiff line change
@@ -1877,7 +1877,7 @@ private void LoginNoFailover(ServerInfo serverInfo, string newPassword, SecureSt
18771877
throw SQL.ROR_TimeoutAfterRoutingInfo(this);
18781878
}
18791879

1880-
serverInfo = new ServerInfo(ConnectionOptions, _routingInfo, serverInfo.ResolvedServerName);
1880+
serverInfo = new ServerInfo(ConnectionOptions, _routingInfo, serverInfo.ResolvedServerName, serverInfo.ServerSPN);
18811881
timeoutErrorInternal.SetInternalSourceType(SqlConnectionInternalSourceType.RoutingDestination);
18821882
_originalClientConnectionId = _clientConnectionId;
18831883
_routingDestination = serverInfo.UserServerName;
@@ -2047,7 +2047,7 @@ TimeoutTimer timeout
20472047
long timeoutUnitInterval;
20482048

20492049
string protocol = ConnectionOptions.NetworkLibrary;
2050-
ServerInfo failoverServerInfo = new ServerInfo(connectionOptions, failoverHost);
2050+
ServerInfo failoverServerInfo = new ServerInfo(connectionOptions, failoverHost, connectionOptions.FailoverPartnerSPN);
20512051

20522052
ResolveExtendedServerName(primaryServerInfo, !redirectedUserInstance, connectionOptions);
20532053
if (null == ServerProvidedFailOverPartner)
@@ -2150,7 +2150,7 @@ TimeoutTimer timeout
21502150
_parser = new TdsParser(ConnectionOptions.MARS, ConnectionOptions.Asynchronous);
21512151
Debug.Assert(SniContext.Undefined == Parser._physicalStateObj.SniContext, $"SniContext should be Undefined; actual Value: {Parser._physicalStateObj.SniContext}");
21522152

2153-
currentServerInfo = new ServerInfo(ConnectionOptions, _routingInfo, currentServerInfo.ResolvedServerName);
2153+
currentServerInfo = new ServerInfo(ConnectionOptions, _routingInfo, currentServerInfo.ResolvedServerName, currentServerInfo.ServerSPN);
21542154
timeoutErrorInternal.SetInternalSourceType(SqlConnectionInternalSourceType.RoutingDestination);
21552155
_originalClientConnectionId = _clientConnectionId;
21562156
_routingDestination = currentServerInfo.UserServerName;
@@ -2296,13 +2296,9 @@ private void AttemptOneLogin(ServerInfo serverInfo, string newPassword, SecureSt
22962296
this,
22972297
ignoreSniOpenTimeout,
22982298
timeout.LegacyTimerExpire,
2299-
ConnectionOptions.Encrypt,
2300-
ConnectionOptions.TrustServerCertificate,
2301-
ConnectionOptions.IntegratedSecurity,
2299+
ConnectionOptions,
23022300
withFailover,
23032301
isFirstTransparentAttempt,
2304-
ConnectionOptions.Authentication,
2305-
ConnectionOptions.Certificate,
23062302
_serverCallback,
23072303
_clientCallback,
23082304
_originalNetworkAddressInfo != null,
@@ -3244,6 +3240,7 @@ internal sealed class ServerInfo
32443240
internal string ResolvedServerName { get; private set; } // the resolved servername only
32453241
internal string ResolvedDatabaseName { get; private set; } // name of target database after resolution
32463242
internal string UserProtocol { get; private set; } // the user specified protocol
3243+
internal string ServerSPN { get; private set; } // the server SPN
32473244

32483245
// The original user-supplied server name from the connection string.
32493246
// If connection string has no Data Source, the value is set to string.Empty.
@@ -3264,10 +3261,16 @@ private set
32643261
internal readonly string PreRoutingServerName;
32653262

32663263
// Initialize server info from connection options,
3267-
internal ServerInfo(SqlConnectionString userOptions) : this(userOptions, userOptions.DataSource) { }
3264+
internal ServerInfo(SqlConnectionString userOptions) : this(userOptions, userOptions.DataSource, userOptions.ServerSPN) { }
3265+
3266+
// Initialize server info from connection options, but override DataSource and ServerSPN with given server name and server SPN
3267+
internal ServerInfo(SqlConnectionString userOptions, string serverName, string serverSPN) : this(userOptions, serverName)
3268+
{
3269+
ServerSPN = serverSPN;
3270+
}
32683271

32693272
// Initialize server info from connection options, but override DataSource with given server name
3270-
internal ServerInfo(SqlConnectionString userOptions, string serverName)
3273+
private ServerInfo(SqlConnectionString userOptions, string serverName)
32713274
{
32723275
//-----------------
32733276
// Preconditions
@@ -3286,7 +3289,7 @@ internal ServerInfo(SqlConnectionString userOptions, string serverName)
32863289

32873290

32883291
// Initialize server info from connection options, but override DataSource with given server name
3289-
internal ServerInfo(SqlConnectionString userOptions, RoutingInfo routing, string preRoutingServerName)
3292+
internal ServerInfo(SqlConnectionString userOptions, RoutingInfo routing, string preRoutingServerName, string serverSPN)
32903293
{
32913294
//-----------------
32923295
// Preconditions
@@ -3307,6 +3310,7 @@ internal ServerInfo(SqlConnectionString userOptions, RoutingInfo routing, string
33073310
UserProtocol = TdsEnums.TCP;
33083311
SetDerivedNames(UserProtocol, UserServerName);
33093312
ResolvedDatabaseName = userOptions.InitialCatalog;
3313+
ServerSPN = serverSPN;
33103314
}
33113315

33123316
internal void SetDerivedNames(string protocol, string serverName)

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

+10-5
Original file line numberDiff line numberDiff line change
@@ -493,18 +493,20 @@ internal void Connect(ServerInfo serverInfo,
493493
SqlInternalConnectionTds connHandler,
494494
bool ignoreSniOpenTimeout,
495495
long timerExpire,
496-
bool encrypt,
497-
bool trustServerCert,
498-
bool integratedSecurity,
496+
SqlConnectionString connectionOptions,
499497
bool withFailover,
500498
bool isFirstTransparentAttempt,
501-
SqlAuthenticationMethod authType,
502-
string certificate,
503499
ServerCertificateValidationCallback serverCallback,
504500
ClientCertificateRetrievalCallback clientCallback,
505501
bool useOriginalAddressInfo,
506502
bool disableTnir)
507503
{
504+
bool encrypt = connectionOptions.Encrypt;
505+
bool trustServerCert = connectionOptions.TrustServerCertificate;
506+
bool integratedSecurity = connectionOptions.IntegratedSecurity;
507+
SqlAuthenticationMethod authType = connectionOptions.Authentication;
508+
string certificate = connectionOptions.Certificate;
509+
508510
if (_state != TdsParserState.Closed)
509511
{
510512
Debug.Fail("TdsParser.Connect called on non-closed connection!");
@@ -544,6 +546,9 @@ internal void Connect(ServerInfo serverInfo,
544546
LoadSSPILibrary();
545547
// now allocate proper length of buffer
546548
_sniSpnBuffer = new byte[SNINativeMethodWrapper.SniMaxComposedSpnLength];
549+
byte[] srvSPN = Encoding.Unicode.GetBytes(serverInfo.ServerSPN);
550+
Trace.Assert(srvSPN.Length <= _sniSpnBuffer.Length, "The provider SPN length exceeded the buffer size.");
551+
Array.Copy(srvSPN, _sniSpnBuffer, srvSPN.Length);
547552
SqlClientEventSource.Log.TryTraceEvent("<sc.TdsParser.Connect|SEC> SSPI or Active Directory Authentication Library for SQL Server based integrated authentication");
548553
}
549554
else

src/Microsoft.Data.SqlClient/src/Microsoft/Data/Common/DbConnectionStringCommon.cs

+8
Original file line numberDiff line numberDiff line change
@@ -976,6 +976,8 @@ internal static class DbConnectionStringDefaults
976976
internal const SqlConnectionAttestationProtocol AttestationProtocol = SqlConnectionAttestationProtocol.NotSpecified;
977977
internal const SqlConnectionIPAddressPreference IPAddressPreference = SqlConnectionIPAddressPreference.IPv4First;
978978
internal const PoolBlockingPeriod PoolBlockingPeriod = SqlClient.PoolBlockingPeriod.Auto;
979+
internal const string ServerSPN = "";
980+
internal const string FailoverPartnerSPN = "";
979981
}
980982

981983
internal static class DbConnectionStringKeywords
@@ -1029,6 +1031,8 @@ internal static class DbConnectionStringKeywords
10291031
internal const string EnclaveAttestationUrl = "Enclave Attestation Url";
10301032
internal const string AttestationProtocol = "Attestation Protocol";
10311033
internal const string IPAddressPreference = "IP Address Preference";
1034+
internal const string ServerSPN = "Server SPN";
1035+
internal const string FailoverPartnerSPN = "Failover Partner SPN";
10321036

10331037
// common keywords (OleDb, OracleClient, SqlClient)
10341038
internal const string DataSource = "Data Source";
@@ -1122,5 +1126,9 @@ internal static class DbConnectionStringSynonyms
11221126

11231127
//internal const string WorkstationID = WSID;
11241128
internal const string WSID = "wsid";
1129+
1130+
//internal const string server SPNs
1131+
internal const string ServerSPN = "ServerSPN";
1132+
internal const string FailoverPartnerSPN = "FailoverPartnerSPN";
11251133
}
11261134
}

src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlConnectionString.cs

+21-3
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ internal static class DEFAULT
5959
internal static readonly SqlAuthenticationMethod Authentication = DbConnectionStringDefaults.Authentication;
6060
internal static readonly SqlConnectionAttestationProtocol AttestationProtocol = DbConnectionStringDefaults.AttestationProtocol;
6161
internal static readonly SqlConnectionIPAddressPreference IpAddressPreference = DbConnectionStringDefaults.IPAddressPreference;
62+
internal const string ServerSPN = DbConnectionStringDefaults.ServerSPN;
63+
internal const string FailoverPartnerSPN = DbConnectionStringDefaults.FailoverPartnerSPN;
6264
#if NETFRAMEWORK
6365
internal static readonly bool TransparentNetworkIPResolution = DbConnectionStringDefaults.TransparentNetworkIPResolution;
6466
internal const bool Connection_Reset = DbConnectionStringDefaults.ConnectionReset;
@@ -113,6 +115,8 @@ internal static class KEY
113115
internal const string Connect_Retry_Count = DbConnectionStringKeywords.ConnectRetryCount;
114116
internal const string Connect_Retry_Interval = DbConnectionStringKeywords.ConnectRetryInterval;
115117
internal const string Authentication = DbConnectionStringKeywords.Authentication;
118+
internal const string Server_SPN = DbConnectionStringKeywords.ServerSPN;
119+
internal const string Failover_Partner_SPN = DbConnectionStringKeywords.FailoverPartnerSPN;
116120
#if NETFRAMEWORK
117121
internal const string TransparentNetworkIPResolution = DbConnectionStringKeywords.TransparentNetworkIPResolution;
118122
#if ADONET_CERT_AUTH
@@ -173,6 +177,9 @@ private static class SYNONYM
173177
internal const string User = DbConnectionStringSynonyms.User;
174178
// workstation id
175179
internal const string WSID = DbConnectionStringSynonyms.WSID;
180+
// server SPNs
181+
internal const string ServerSPN = DbConnectionStringSynonyms.ServerSPN;
182+
internal const string FailoverPartnerSPN = DbConnectionStringSynonyms.FailoverPartnerSPN;
176183

177184
#if NETFRAMEWORK
178185
internal const string TRANSPARENTNETWORKIPRESOLUTION = DbConnectionStringSynonyms.TRANSPARENTNETWORKIPRESOLUTION;
@@ -212,9 +219,9 @@ internal static class TRANSACTIONBINDING
212219
}
213220

214221
#if NETFRAMEWORK
215-
internal const int SynonymCount = 29;
222+
internal const int SynonymCount = 31;
216223
#else
217-
internal const int SynonymCount = 26;
224+
internal const int SynonymCount = 28;
218225
internal const int DeprecatedSynonymCount = 2;
219226
#endif // NETFRAMEWORK
220227

@@ -257,6 +264,8 @@ internal static class TRANSACTIONBINDING
257264
private readonly string _initialCatalog;
258265
private readonly string _password;
259266
private readonly string _userID;
267+
private readonly string _serverSPN;
268+
private readonly string _failoverPartnerSPN;
260269

261270
private readonly string _workstationId;
262271

@@ -322,6 +331,8 @@ internal SqlConnectionString(string connectionString) : base(connectionString, G
322331
_enclaveAttestationUrl = ConvertValueToString(KEY.EnclaveAttestationUrl, DEFAULT.EnclaveAttestationUrl);
323332
_attestationProtocol = ConvertValueToAttestationProtocol();
324333
_ipAddressPreference = ConvertValueToIPAddressPreference();
334+
_serverSPN = ConvertValueToString(KEY.Server_SPN, DEFAULT.ServerSPN);
335+
_failoverPartnerSPN = ConvertValueToString(KEY.Failover_Partner_SPN, DEFAULT.FailoverPartnerSPN);
325336

326337
// Temporary string - this value is stored internally as an enum.
327338
string typeSystemVersionString = ConvertValueToString(KEY.Type_System_Version, null);
@@ -675,6 +686,8 @@ internal SqlConnectionString(SqlConnectionString connectionOptions, string dataS
675686
_columnEncryptionSetting = connectionOptions._columnEncryptionSetting;
676687
_enclaveAttestationUrl = connectionOptions._enclaveAttestationUrl;
677688
_attestationProtocol = connectionOptions._attestationProtocol;
689+
_serverSPN = connectionOptions._serverSPN;
690+
_failoverPartnerSPN = connectionOptions._failoverPartnerSPN;
678691
#if NETFRAMEWORK
679692
_connectionReset = connectionOptions._connectionReset;
680693
_contextConnection = connectionOptions._contextConnection;
@@ -732,7 +745,8 @@ internal SqlConnectionString(SqlConnectionString connectionOptions, string dataS
732745
internal string UserID => _userID;
733746
internal string WorkstationId => _workstationId;
734747
internal PoolBlockingPeriod PoolBlockingPeriod => _poolBlockingPeriod;
735-
748+
internal string ServerSPN => _serverSPN;
749+
internal string FailoverPartnerSPN => _failoverPartnerSPN;
736750

737751
internal TypeSystem TypeSystemVersion => _typeSystemVersion;
738752
internal Version TypeSystemAssemblyVersion => _typeSystemAssemblyVersion;
@@ -843,6 +857,8 @@ internal static Dictionary<string, string> GetParseSynonyms()
843857
{ KEY.Connect_Retry_Interval, KEY.Connect_Retry_Interval },
844858
{ KEY.Authentication, KEY.Authentication },
845859
{ KEY.IPAddressPreference, KEY.IPAddressPreference },
860+
{ KEY.Server_SPN, KEY.Server_SPN },
861+
{ KEY.Failover_Partner_SPN, KEY.Failover_Partner_SPN },
846862

847863
{ SYNONYM.APP, KEY.Application_Name },
848864
{ SYNONYM.APPLICATIONINTENT, KEY.ApplicationIntent },
@@ -871,6 +887,8 @@ internal static Dictionary<string, string> GetParseSynonyms()
871887
{ SYNONYM.UID, KEY.User_ID },
872888
{ SYNONYM.User, KEY.User_ID },
873889
{ SYNONYM.WSID, KEY.Workstation_Id },
890+
{ SYNONYM.ServerSPN, KEY.Server_SPN },
891+
{ SYNONYM.FailoverPartnerSPN, KEY.Failover_Partner_SPN },
874892
#if NETFRAMEWORK
875893
#if ADONET_CERT_AUTH
876894
{ KEY.Certificate, KEY.Certificate },

0 commit comments

Comments
 (0)