@@ -28,14 +28,20 @@ internal void ProcessSSPI(int receivedLength)
28
28
// allocate send buffer and initialize length
29
29
var writer = SqlObjectPools . BufferWriter . Rent ( ) ;
30
30
31
- // make call for SSPI data
32
- _authenticationProvider ! . SSPIData ( receivedBuff . AsSpan ( 0 , receivedLength ) , writer , _serverSpn ) ;
31
+ try
32
+ {
33
+ // make call for SSPI data
34
+ _authenticationProvider ! . SSPIData ( receivedBuff . AsSpan ( 0 , receivedLength ) , writer , _serverSpn ) ;
33
35
34
- // DO NOT SEND LENGTH - TDS DOC INCORRECT! JUST SEND SSPI DATA!
35
- _physicalStateObj . WriteByteSpan ( writer . WrittenSpan ) ;
36
+ // DO NOT SEND LENGTH - TDS DOC INCORRECT! JUST SEND SSPI DATA!
37
+ _physicalStateObj . WriteByteSpan ( writer . WrittenSpan ) ;
36
38
37
- SqlObjectPools . BufferWriter . Return ( writer ) ;
38
- ArrayPool < byte > . Shared . Return ( receivedBuff , clearArray : true ) ;
39
+ }
40
+ finally
41
+ {
42
+ SqlObjectPools . BufferWriter . Return ( writer ) ;
43
+ ArrayPool < byte > . Shared . Return ( receivedBuff , clearArray : true ) ;
44
+ }
39
45
40
46
// set message type so server knows its a SSPI response
41
47
_physicalStateObj . _outputMessageType = TdsEnums . MT_SSPI ;
@@ -139,60 +145,65 @@ internal void TdsLogin(
139
145
// allocate memory for SSPI variables
140
146
ArrayBufferWriter < byte > sspiWriter = null ;
141
147
142
- // only add lengths of password and username if not using SSPI or requesting federated authentication info
143
- if ( ! rec . useSSPI && ! ( _connHandler . _federatedAuthenticationInfoRequested || _connHandler . _federatedAuthenticationRequested ) )
148
+ try
144
149
{
145
- checked
150
+ // only add lengths of password and username if not using SSPI or requesting federated authentication info
151
+ if ( ! rec . useSSPI && ! ( _connHandler . _federatedAuthenticationInfoRequested || _connHandler . _federatedAuthenticationRequested ) )
146
152
{
147
- length += ( userName . Length * 2 ) + encryptedPasswordLengthInBytes
148
- + encryptedChangePasswordLengthInBytes ;
153
+ checked
154
+ {
155
+ length += ( userName . Length * 2 ) + encryptedPasswordLengthInBytes
156
+ + encryptedChangePasswordLengthInBytes ;
157
+ }
149
158
}
150
- }
151
- else
152
- {
153
- if ( rec . useSSPI )
159
+ else
154
160
{
155
- sspiWriter = SqlObjectPools . BufferWriter . Rent ( ) ;
161
+ if ( rec . useSSPI )
162
+ {
163
+ sspiWriter = SqlObjectPools . BufferWriter . Rent ( ) ;
156
164
157
- // Call helper function for SSPI data and actual length.
158
- // Since we don't have SSPI data from the server, send null for the
159
- // byte[] buffer and 0 for the int length.
160
- Debug . Assert ( SniContext . Snix_Login == _physicalStateObj . SniContext , $ "Unexpected SniContext. Expecting Snix_Login, actual value is '{ _physicalStateObj . SniContext } '") ;
161
- _physicalStateObj . SniContext = SniContext . Snix_LoginSspi ;
162
- _authenticationProvider . SSPIData ( ReadOnlySpan < byte > . Empty , sspiWriter , _serverSpn ) ;
165
+ // Call helper function for SSPI data and actual length.
166
+ // Since we don't have SSPI data from the server, send null for the
167
+ // byte[] buffer and 0 for the int length.
168
+ Debug . Assert ( SniContext . Snix_Login == _physicalStateObj . SniContext , $ "Unexpected SniContext. Expecting Snix_Login, actual value is '{ _physicalStateObj . SniContext } '") ;
169
+ _physicalStateObj . SniContext = SniContext . Snix_LoginSspi ;
170
+ _authenticationProvider . SSPIData ( ReadOnlySpan < byte > . Empty , sspiWriter , _serverSpn ) ;
163
171
164
- _physicalStateObj . SniContext = SniContext . Snix_Login ;
172
+ _physicalStateObj . SniContext = SniContext . Snix_Login ;
165
173
166
- checked
167
- {
168
- length += ( int ) sspiWriter . WrittenCount ;
174
+ checked
175
+ {
176
+ length += ( int ) sspiWriter . WrittenCount ;
177
+ }
169
178
}
170
179
}
171
- }
172
180
173
- int feOffset = length ;
174
- // calculate and reserve the required bytes for the featureEx
175
- length = ApplyFeatureExData ( requestedFeatures , recoverySessionData , fedAuthFeatureExtensionData , useFeatureExt , length ) ;
176
-
177
- WriteLoginData ( rec ,
178
- requestedFeatures ,
179
- recoverySessionData ,
180
- fedAuthFeatureExtensionData ,
181
- encrypt ,
182
- encryptedPassword ,
183
- encryptedChangePassword ,
184
- encryptedPasswordLengthInBytes ,
185
- encryptedChangePasswordLengthInBytes ,
186
- useFeatureExt ,
187
- userName ,
188
- length ,
189
- feOffset ,
190
- clientInterfaceName ,
191
- sspiWriter is { } ? sspiWriter . WrittenSpan : ReadOnlySpan < byte > . Empty ) ;
192
-
193
- if ( sspiWriter is not null )
181
+ int feOffset = length ;
182
+ // calculate and reserve the required bytes for the featureEx
183
+ length = ApplyFeatureExData ( requestedFeatures , recoverySessionData , fedAuthFeatureExtensionData , useFeatureExt , length ) ;
184
+
185
+ WriteLoginData ( rec ,
186
+ requestedFeatures ,
187
+ recoverySessionData ,
188
+ fedAuthFeatureExtensionData ,
189
+ encrypt ,
190
+ encryptedPassword ,
191
+ encryptedChangePassword ,
192
+ encryptedPasswordLengthInBytes ,
193
+ encryptedChangePasswordLengthInBytes ,
194
+ useFeatureExt ,
195
+ userName ,
196
+ length ,
197
+ feOffset ,
198
+ clientInterfaceName ,
199
+ sspiWriter is { } ? sspiWriter . WrittenSpan : ReadOnlySpan < byte > . Empty ) ;
200
+ }
201
+ finally
194
202
{
195
- SqlObjectPools . BufferWriter . Return ( sspiWriter ) ;
203
+ if ( sspiWriter is not null )
204
+ {
205
+ SqlObjectPools . BufferWriter . Return ( sspiWriter ) ;
206
+ }
196
207
}
197
208
198
209
_physicalStateObj . WritePacket ( TdsEnums . HARDFLUSH ) ;
0 commit comments