diff --git a/src/System.Data.SqlClient/src/System/Data/ProviderBase/DbConnectionPoolIdentity.Windows.cs b/src/System.Data.SqlClient/src/System/Data/ProviderBase/DbConnectionPoolIdentity.Windows.cs index 8c398a03fa4f..363e79ff1904 100644 --- a/src/System.Data.SqlClient/src/System/Data/ProviderBase/DbConnectionPoolIdentity.Windows.cs +++ b/src/System.Data.SqlClient/src/System/Data/ProviderBase/DbConnectionPoolIdentity.Windows.cs @@ -22,8 +22,9 @@ private static DbConnectionPoolIdentity GetCurrentNative() using (WindowsIdentity identity = WindowsIdentity.GetCurrent()) { IntPtr token = identity.AccessToken.DangerousGetHandle(); - bool isNetwork = identity.User.IsWellKnown(WellKnownSidType.NetworkSid); - string sidString = identity.User.Value; + SecurityIdentifier user = identity.User; + bool isNetwork = user.IsWellKnown(WellKnownSidType.NetworkSid); + string sidString = user.Value; // Win32NativeMethods.IsTokenRestricted will raise exception if the native call fails bool isRestricted = Win32NativeMethods.IsTokenRestrictedWrapper(token); diff --git a/src/System.Data.SqlClient/src/System/Data/SqlClient/TdsParser.cs b/src/System.Data.SqlClient/src/System/Data/SqlClient/TdsParser.cs index e7e35d351c0e..b2bcce0efce8 100644 --- a/src/System.Data.SqlClient/src/System/Data/SqlClient/TdsParser.cs +++ b/src/System.Data.SqlClient/src/System/Data/SqlClient/TdsParser.cs @@ -3,6 +3,7 @@ // See the LICENSE file in the project root for more information. using System.Collections.Generic; +using System.Buffers; using System.Data.Common; using System.Data.Sql; using System.Data.SqlTypes; @@ -6190,6 +6191,7 @@ internal void TdsLogin(SqlLogin rec, TdsEnums.FeatureExtension requestedFeatures } // allocate memory for SSPI variables + byte[] rentedSSPIBuff = null; byte[] outSSPIBuff = null; uint outSSPILength = 0; @@ -6207,7 +6209,8 @@ internal void TdsLogin(SqlLogin rec, TdsEnums.FeatureExtension requestedFeatures if (rec.useSSPI) { // now allocate proper length of buffer, and set length - outSSPIBuff = new byte[s_maxSSPILength]; + rentedSSPIBuff = ArrayPool.Shared.Rent((int)s_maxSSPILength); + outSSPIBuff = rentedSSPIBuff; outSSPILength = s_maxSSPILength; // Call helper function for SSPI data and actual length. @@ -6515,6 +6518,11 @@ internal void TdsLogin(SqlLogin rec, TdsEnums.FeatureExtension requestedFeatures throw; } + if (rentedSSPIBuff != null) + { + ArrayPool.Shared.Return(rentedSSPIBuff, clearArray: true); + } + _physicalStateObj.WritePacket(TdsEnums.HARDFLUSH); _physicalStateObj.ResetSecurePasswordsInformation(); _physicalStateObj._pendingData = true; @@ -6569,7 +6577,8 @@ private void ProcessSSPI(int receivedLength) if (!result) { throw SQL.SynchronousCallMayNotPend(); } // allocate send buffer and initialize length - byte[] sendBuff = new byte[s_maxSSPILength]; + byte[] rentedSendBuff = ArrayPool.Shared.Rent((int)s_maxSSPILength); + byte[] sendBuff = rentedSendBuff; uint sendLength = s_maxSSPILength; // make call for SSPI data @@ -6579,6 +6588,8 @@ private void ProcessSSPI(int receivedLength) // DO NOT SEND LENGTH - TDS DOC INCORRECT! JUST SEND SSPI DATA! _physicalStateObj.WriteByteArray(sendBuff, (int)sendLength, 0); + ArrayPool.Shared.Return(rentedSendBuff, clearArray: true); + // set message type so server knows its a SSPI response _physicalStateObj._outputMessageType = TdsEnums.MT_SSPI;