@@ -128,7 +128,7 @@ internal int ObjectID
128128
129129 private bool _is2022 = false;
130130
131- private byte[] _sniSpnBuffer = null;
131+ private string _sniSpn = null;
132132
133133 // UNDONE - need to have some for both instances - both command and default???
134134
@@ -430,27 +430,24 @@ internal void Connect(ServerInfo serverInfo,
430430 // AD Integrated behaves like Windows integrated when connecting to a non-fedAuth server
431431 if (integratedSecurity || authType == SqlAuthenticationMethod.ActiveDirectoryIntegrated)
432432 {
433- _authenticationProvider = _physicalStateObj.CreateSSPIContextProvider();
434-
435433 if (!string.IsNullOrEmpty(serverInfo.ServerSPN))
436434 {
437- // Native SNI requires the Unicode encoding and any other encoding like UTF8 breaks the code.
438- byte[] srvSPN = Encoding.Unicode.GetBytes(serverInfo.ServerSPN);
439- Trace.Assert(srvSPN.Length <= SniNativeWrapper.SniMaxComposedSpnLength, "The provided SPN length exceeded the buffer size.");
440- _sniSpnBuffer = srvSPN;
435+ _sniSpn = serverInfo.ServerSPN;
441436 SqlClientEventSource.Log.TryTraceEvent("<sc.TdsParser.Connect|SEC> Server SPN `{0}` from the connection string is used.", serverInfo.ServerSPN);
442437 }
443438 else
444439 {
445- // now allocate proper length of buffer
446- _sniSpnBuffer = new byte[SniNativeWrapper.SniMaxComposedSpnLength] ;
440+ // Empty signifies to interop layer that SNI needs to be generated
441+ _sniSpn = string.Empty ;
447442 }
443+
444+ _authenticationProvider = _physicalStateObj.CreateSSPIContextProvider();
448445 SqlClientEventSource.Log.TryTraceEvent("<sc.TdsParser.Connect|SEC> SSPI or Active Directory Authentication Library for SQL Server based integrated authentication");
449446 }
450447 else
451448 {
452449 _authenticationProvider = null;
453- _sniSpnBuffer = null;
450+ _sniSpn = null;
454451
455452 switch (authType)
456453 {
@@ -529,7 +526,7 @@ internal void Connect(ServerInfo serverInfo,
529526 serverInfo.ExtendedServerName,
530527 timeout,
531528 out instanceName,
532- _sniSpnBuffer ,
529+ ref _sniSpn ,
533530 false,
534531 true,
535532 fParallel,
@@ -539,8 +536,6 @@ internal void Connect(ServerInfo serverInfo,
539536 FQDNforDNSCache,
540537 hostNameInCertificate);
541538
542- _authenticationProvider?.Initialize(serverInfo, _physicalStateObj, this);
543-
544539 if (TdsEnums.SNI_SUCCESS != _physicalStateObj.Status)
545540 {
546541 _physicalStateObj.AddError(ProcessSNIError(_physicalStateObj));
@@ -555,6 +550,8 @@ internal void Connect(ServerInfo serverInfo,
555550 Debug.Fail("SNI returned status != success, but no error thrown?");
556551 }
557552
553+ _authenticationProvider?.Initialize(serverInfo, _physicalStateObj, this);
554+
558555 _server = serverInfo.ResolvedServerName;
559556
560557 if (connHandler.PoolGroupProviderInfo != null)
@@ -629,7 +626,7 @@ internal void Connect(ServerInfo serverInfo,
629626 serverInfo.ExtendedServerName,
630627 timeout,
631628 out instanceName,
632- _sniSpnBuffer ,
629+ ref _sniSpn ,
633630 true,
634631 true,
635632 fParallel,
@@ -639,15 +636,15 @@ internal void Connect(ServerInfo serverInfo,
639636 serverInfo.ResolvedServerName,
640637 hostNameInCertificate);
641638
642- _authenticationProvider?.Initialize(serverInfo, _physicalStateObj, this);
643-
644639 if (TdsEnums.SNI_SUCCESS != _physicalStateObj.Status)
645640 {
646641 _physicalStateObj.AddError(ProcessSNIError(_physicalStateObj));
647642 SqlClientEventSource.Log.TryTraceEvent("<sc.TdsParser.Connect|ERR|SEC> Login failure");
648643 ThrowExceptionAndWarning(_physicalStateObj);
649644 }
650645
646+ _authenticationProvider?.Initialize(serverInfo, _physicalStateObj, this);
647+
651648 uint retCode = SniNativeWrapper.SniGetConnectionId(_physicalStateObj.Handle, ref _connHandler._clientConnectionId);
652649 Debug.Assert(retCode == TdsEnums.SNI_SUCCESS, "Unexpected failure state upon calling SniGetConnectionId");
653650 SqlClientEventSource.Log.TryTraceEvent("<sc.TdsParser.Connect|SEC> Sending prelogin handshake");
@@ -13860,7 +13857,7 @@ internal string TraceString()
1386013857 _is2000 ? bool.TrueString : bool.FalseString,
1386113858 _is2000SP1 ? bool.TrueString : bool.FalseString,
1386213859 _is2005 ? bool.TrueString : bool.FalseString,
13863- _sniSpnBuffer == null ? "(null)" : _sniSpnBuffer .Length.ToString((IFormatProvider)null),
13860+ _sniSpn == null ? "(null)" : _sniSpn .Length.ToString((IFormatProvider)null),
1386413861 _physicalStateObj != null ? "(null)" : _physicalStateObj.ErrorCount.ToString((IFormatProvider)null),
1386513862 _physicalStateObj != null ? "(null)" : _physicalStateObj.WarningCount.ToString((IFormatProvider)null),
1386613863 _physicalStateObj != null ? "(null)" : _physicalStateObj.PreAttentionErrorCount.ToString((IFormatProvider)null),
0 commit comments