@@ -128,7 +128,7 @@ internal int ObjectID
128
128
129
129
private bool _is2022 = false;
130
130
131
- private byte[] _sniSpnBuffer = null;
131
+ private string _sniSpn = null;
132
132
133
133
// UNDONE - need to have some for both instances - both command and default???
134
134
@@ -430,27 +430,24 @@ internal void Connect(ServerInfo serverInfo,
430
430
// AD Integrated behaves like Windows integrated when connecting to a non-fedAuth server
431
431
if (integratedSecurity || authType == SqlAuthenticationMethod.ActiveDirectoryIntegrated)
432
432
{
433
- _authenticationProvider = _physicalStateObj.CreateSSPIContextProvider();
434
-
435
433
if (!string.IsNullOrEmpty(serverInfo.ServerSPN))
436
434
{
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;
441
436
SqlClientEventSource.Log.TryTraceEvent("<sc.TdsParser.Connect|SEC> Server SPN `{0}` from the connection string is used.", serverInfo.ServerSPN);
442
437
}
443
438
else
444
439
{
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 ;
447
442
}
443
+
444
+ _authenticationProvider = _physicalStateObj.CreateSSPIContextProvider();
448
445
SqlClientEventSource.Log.TryTraceEvent("<sc.TdsParser.Connect|SEC> SSPI or Active Directory Authentication Library for SQL Server based integrated authentication");
449
446
}
450
447
else
451
448
{
452
449
_authenticationProvider = null;
453
- _sniSpnBuffer = null;
450
+ _sniSpn = null;
454
451
455
452
switch (authType)
456
453
{
@@ -529,7 +526,7 @@ internal void Connect(ServerInfo serverInfo,
529
526
serverInfo.ExtendedServerName,
530
527
timeout,
531
528
out instanceName,
532
- _sniSpnBuffer ,
529
+ ref _sniSpn ,
533
530
false,
534
531
true,
535
532
fParallel,
@@ -539,8 +536,6 @@ internal void Connect(ServerInfo serverInfo,
539
536
FQDNforDNSCache,
540
537
hostNameInCertificate);
541
538
542
- _authenticationProvider?.Initialize(serverInfo, _physicalStateObj, this);
543
-
544
539
if (TdsEnums.SNI_SUCCESS != _physicalStateObj.Status)
545
540
{
546
541
_physicalStateObj.AddError(ProcessSNIError(_physicalStateObj));
@@ -555,6 +550,8 @@ internal void Connect(ServerInfo serverInfo,
555
550
Debug.Fail("SNI returned status != success, but no error thrown?");
556
551
}
557
552
553
+ _authenticationProvider?.Initialize(serverInfo, _physicalStateObj, this);
554
+
558
555
_server = serverInfo.ResolvedServerName;
559
556
560
557
if (connHandler.PoolGroupProviderInfo != null)
@@ -629,7 +626,7 @@ internal void Connect(ServerInfo serverInfo,
629
626
serverInfo.ExtendedServerName,
630
627
timeout,
631
628
out instanceName,
632
- _sniSpnBuffer ,
629
+ ref _sniSpn ,
633
630
true,
634
631
true,
635
632
fParallel,
@@ -639,15 +636,15 @@ internal void Connect(ServerInfo serverInfo,
639
636
serverInfo.ResolvedServerName,
640
637
hostNameInCertificate);
641
638
642
- _authenticationProvider?.Initialize(serverInfo, _physicalStateObj, this);
643
-
644
639
if (TdsEnums.SNI_SUCCESS != _physicalStateObj.Status)
645
640
{
646
641
_physicalStateObj.AddError(ProcessSNIError(_physicalStateObj));
647
642
SqlClientEventSource.Log.TryTraceEvent("<sc.TdsParser.Connect|ERR|SEC> Login failure");
648
643
ThrowExceptionAndWarning(_physicalStateObj);
649
644
}
650
645
646
+ _authenticationProvider?.Initialize(serverInfo, _physicalStateObj, this);
647
+
651
648
uint retCode = SniNativeWrapper.SniGetConnectionId(_physicalStateObj.Handle, ref _connHandler._clientConnectionId);
652
649
Debug.Assert(retCode == TdsEnums.SNI_SUCCESS, "Unexpected failure state upon calling SniGetConnectionId");
653
650
SqlClientEventSource.Log.TryTraceEvent("<sc.TdsParser.Connect|SEC> Sending prelogin handshake");
@@ -13785,7 +13782,7 @@ internal string TraceString()
13785
13782
_is2000 ? bool.TrueString : bool.FalseString,
13786
13783
_is2000SP1 ? bool.TrueString : bool.FalseString,
13787
13784
_is2005 ? bool.TrueString : bool.FalseString,
13788
- _sniSpnBuffer == null ? "(null)" : _sniSpnBuffer .Length.ToString((IFormatProvider)null),
13785
+ _sniSpn == null ? "(null)" : _sniSpn .Length.ToString((IFormatProvider)null),
13789
13786
_physicalStateObj != null ? "(null)" : _physicalStateObj.ErrorCount.ToString((IFormatProvider)null),
13790
13787
_physicalStateObj != null ? "(null)" : _physicalStateObj.WarningCount.ToString((IFormatProvider)null),
13791
13788
_physicalStateObj != null ? "(null)" : _physicalStateObj.PreAttentionErrorCount.ToString((IFormatProvider)null),
0 commit comments