Exemplo n.º 1
0
        private void Login(int timeout)
        {
            // create a new login record
            SqlLogin login = new SqlLogin();

            // gather all the settings the user set in the connection string or properties and
            login.timeout          = timeout;
            login.hostName         = _connectionOptions.WorkstationId;
            login.userName         = _connectionOptions.UserID;
            login.applicationName  = _connectionOptions.ApplicationName;
            login.language         = _connectionOptions.CurrentLanguage;
            login.database         = _connectionOptions.InitialCatalog;
            login.attachDBFilename = _connectionOptions.AttachDBFilename;
            login.serverName       = _connectionOptions.DataSource;
            login.useSSPI          = _connectionOptions.IntegratedSecurity;
            login.packetSize       = _connectionOptions.PacketSize;
#if USECRYPTO
            try {
                login.password = Crypto.DecryptString(_connectionOptions.Password);
                GCHandle textHandle = GCHandle.Alloc(login.password, GCHandleType.Pinned); // MDAC 82615
                try {
                    // do the login
                    _parser.TdsLogin(login);
                }
                finally {
                    Array.Clear(login.password, 0, login.password.Length);
                    if (textHandle.IsAllocated)
                    {
                        textHandle.Free();
                    }
                    login.password = ADP.EmptyByteArray;
                }
            }
            catch {
                throw;
            }
#else
            login.password = System.Text.Encoding.Unicode.GetBytes(_connectionOptions.Password);
            _parser.TdsLogin(login);
#endif
            _parser.Run(RunBehavior.UntilDone);
        }
Exemplo n.º 2
0
        private void Login(ServerInfo server, TimeoutTimer timeout, string newPassword)
        {
            SqlLogin rec = new SqlLogin();

            base.CurrentDatabase    = server.ResolvedDatabaseName;
            this._currentPacketSize = base.ConnectionOptions.PacketSize;
            this._currentLanguage   = base.ConnectionOptions.CurrentLanguage;
            int num2 = 0;

            if (!timeout.IsInfinite)
            {
                long num = timeout.MillisecondsRemaining / 0x3e8L;
                if (0x7fffffffL > num)
                {
                    num2 = (int)num;
                }
            }
            rec.timeout         = num2;
            rec.userInstance    = base.ConnectionOptions.UserInstance;
            rec.hostName        = base.ConnectionOptions.ObtainWorkstationId();
            rec.userName        = base.ConnectionOptions.UserID;
            rec.password        = base.ConnectionOptions.Password;
            rec.applicationName = base.ConnectionOptions.ApplicationName;
            rec.language        = this._currentLanguage;
            if (!rec.userInstance)
            {
                rec.database         = base.CurrentDatabase;
                rec.attachDBFilename = base.ConnectionOptions.AttachDBFilename;
            }
            rec.serverName     = server.UserServerName;
            rec.useReplication = base.ConnectionOptions.Replication;
            rec.useSSPI        = base.ConnectionOptions.IntegratedSecurity;
            rec.packetSize     = this._currentPacketSize;
            rec.newPassword    = newPassword;
            rec.readOnlyIntent = base.ConnectionOptions.ApplicationIntent == ApplicationIntent.ReadOnly;
            this._parser.TdsLogin(rec);
        }
Exemplo n.º 3
0
        internal void TdsLogin(SqlLogin rec, TdsEnums.FeatureExtension requestedFeatures, SessionData recoverySessionData)
        {
            _physicalStateObj.SetTimeoutSeconds(rec.timeout);

            Debug.Assert(recoverySessionData == null || (requestedFeatures | TdsEnums.FeatureExtension.SessionRecovery) != 0, "Recovery session data without session recovery feature request");
            Debug.Assert(TdsEnums.MAXLEN_HOSTNAME >= rec.hostName.Length, "_workstationId.Length exceeds the max length for this value");

            Debug.Assert(rec.userName == null || (rec.userName != null && TdsEnums.MAXLEN_USERNAME >= rec.userName.Length), "_userID.Length exceeds the max length for this value");

            Debug.Assert(rec.password == null || (rec.password != null && TdsEnums.MAXLEN_PASSWORD >= rec.password.Length), "_password.Length exceeds the max length for this value");
            Debug.Assert(TdsEnums.MAXLEN_APPNAME >= rec.applicationName.Length, "_applicationName.Length exceeds the max length for this value");
            Debug.Assert(TdsEnums.MAXLEN_SERVERNAME >= rec.serverName.Length, "_dataSource.Length exceeds the max length for this value");
            Debug.Assert(TdsEnums.MAXLEN_LANGUAGE >= rec.language.Length, "_currentLanguage .Length exceeds the max length for this value");
            Debug.Assert(TdsEnums.MAXLEN_DATABASE >= rec.database.Length, "_initialCatalog.Length exceeds the max length for this value");
            Debug.Assert(TdsEnums.MAXLEN_ATTACHDBFILE >= rec.attachDBFilename.Length, "_attachDBFileName.Length exceeds the max length for this value");

            Debug.Assert(_connHandler != null, "SqlConnectionInternalTds handler can not be null at this point.");
            _connHandler.TimeoutErrorInternal.EndPhase(SqlConnectionTimeoutErrorPhase.LoginBegin);
            _connHandler.TimeoutErrorInternal.SetAndBeginPhase(SqlConnectionTimeoutErrorPhase.ProcessConnectionAuth);

            // get the password up front to use in sspi logic below
            byte[] encryptedPassword = null;
            int encryptedPasswordLengthInBytes;
            bool useFeatureExt = (requestedFeatures != TdsEnums.FeatureExtension.None);

            string userName;

            {
                userName = rec.userName;
                encryptedPassword = TdsParserStaticMethods.EncryptPassword(rec.password);
                encryptedPasswordLengthInBytes = encryptedPassword.Length;  // password in clear text is already encrypted and its length is in byte
            }



            // set the message type
            _physicalStateObj._outputMessageType = TdsEnums.MT_LOGIN7;

            // length in bytes
            int length = TdsEnums.YUKON_LOG_REC_FIXED_LEN;

            string clientInterfaceName = TdsEnums.SQL_PROVIDER_NAME;
            Debug.Assert(TdsEnums.MAXLEN_CLIENTINTERFACE >= clientInterfaceName.Length, "cchCltIntName can specify at most 128 unicode characters. See Tds spec");

            // add up variable-len portions (multiply by 2 for byte len of char strings)
            //
            checked
            {
                length += (rec.hostName.Length + rec.applicationName.Length +
                            rec.serverName.Length + clientInterfaceName.Length +
                            rec.language.Length + rec.database.Length +
                            rec.attachDBFilename.Length) * 2;
                if (useFeatureExt)
                {
                    length += 4;
                }
            }

            // allocate memory for SSPI variables
            byte[] outSSPIBuff = null;
            UInt32 outSSPILength = 0;

            // only add lengths of password and username if not using SSPI
            if (!rec.useSSPI)
            {
                checked
                {
                    length += (userName.Length * 2) + encryptedPasswordLengthInBytes
                    ;
                }
            }
            else
            {
                if (rec.useSSPI)
                {
                    // now allocate proper length of buffer, and set length
                    outSSPIBuff = new byte[s_maxSSPILength];
                    outSSPILength = s_maxSSPILength;

                    // Call helper function for SSPI data and actual length.
                    // Since we don't have SSPI data from the server, send null for the
                    // byte[] buffer and 0 for the int length.
                    Debug.Assert(SniContext.Snix_Login == _physicalStateObj.SniContext, String.Format((IFormatProvider)null, "Unexpected SniContext. Expecting Snix_Login, actual value is '{0}'", _physicalStateObj.SniContext));
                    _physicalStateObj.SniContext = SniContext.Snix_LoginSspi;
                    SSPIData(null, 0, outSSPIBuff, ref outSSPILength);
                    if (outSSPILength > Int32.MaxValue)
                    {
                        throw SQL.InvalidSSPIPacketSize();  // SqlBu 332503
                    }
                    _physicalStateObj.SniContext = SniContext.Snix_Login;

                    checked
                    {
                        length += (Int32)outSSPILength;
                    }
                }
            }

            int feOffset = length;

            if (useFeatureExt)
            {
                if ((requestedFeatures & TdsEnums.FeatureExtension.SessionRecovery) != 0)
                {
                    length += WriteSessionRecoveryFeatureRequest(recoverySessionData, false);
                };
                length++; // for terminator
            }

            try
            {
                WriteInt(length, _physicalStateObj);
                if (recoverySessionData == null)
                {
                    WriteInt((TdsEnums.DENALI_MAJOR << 24) | (TdsEnums.DENALI_INCREMENT << 16) | TdsEnums.DENALI_MINOR, _physicalStateObj);
                }
                else
                {
                    WriteUnsignedInt(recoverySessionData._tdsVersion, _physicalStateObj);
                }
                WriteInt(rec.packetSize, _physicalStateObj);
                WriteInt(TdsEnums.CLIENT_PROG_VER, _physicalStateObj);
                WriteInt(TdsParserStaticMethods.GetCurrentProcessIdForTdsLoginOnly(), _physicalStateObj);
                WriteInt(0, _physicalStateObj); // connectionID is unused

                // Log7Flags (DWORD)
                int log7Flags = 0;

                /*
                 Current snapshot from TDS spec with the offsets added:
                    0) fByteOrder:1,                // byte order of numeric data types on client
                    1) fCharSet:1,                  // character set on client
                    2) fFloat:2,                    // Type of floating point on client
                    4) fDumpLoad:1,                 // Dump/Load and BCP enable
                    5) fUseDb:1,                    // USE notification
                    6) fDatabase:1,                 // Initial database fatal flag
                    7) fSetLang:1,                  // SET LANGUAGE notification
                    8) fLanguage:1,                 // Initial language fatal flag
                    9) fODBC:1,                     // Set if client is ODBC driver
                   10) fTranBoundary:1,             // Transaction boundary notification
                   11) fDelegatedSec:1,             // Security with delegation is available
                   12) fUserType:3,                 // Type of user
                   15) fIntegratedSecurity:1,       // Set if client is using integrated security
                   16) fSQLType:4,                  // Type of SQL sent from client
                   20) fOLEDB:1,                    // Set if client is OLEDB driver
                   21) fSpare1:3,                   // first bit used for read-only intent, rest unused
                   24) fResetPassword:1,            // set if client wants to reset password
                   25) fNoNBCAndSparse:1,           // set if client does not support NBC and Sparse column
                   26) fUserInstance:1,             // This connection wants to connect to a SQL "user instance"
                   27) fUnknownCollationHandling:1, // This connection can handle unknown collation correctly.
                   28) fExtension:1                 // Extensions are used                 
                   32 - total
                */

                // first byte
                log7Flags |= TdsEnums.USE_DB_ON << 5;
                log7Flags |= TdsEnums.INIT_DB_FATAL << 6;
                log7Flags |= TdsEnums.SET_LANG_ON << 7;

                // second byte
                log7Flags |= TdsEnums.INIT_LANG_FATAL << 8;
                log7Flags |= TdsEnums.ODBC_ON << 9;
                if (rec.useReplication)
                {
                    log7Flags |= TdsEnums.REPL_ON << 12;
                }
                if (rec.useSSPI)
                {
                    log7Flags |= TdsEnums.SSPI_ON << 15;
                }

                // third byte
                if (rec.readOnlyIntent)
                {
                    log7Flags |= TdsEnums.READONLY_INTENT_ON << 21; // read-only intent flag is a first bit of fSpare1
                }

                // 4th one
                if (rec.userInstance)
                {
                    log7Flags |= 1 << 26;
                }
                if (useFeatureExt)
                {
                    log7Flags |= 1 << 28;
                }

                WriteInt(log7Flags, _physicalStateObj);

                WriteInt(0, _physicalStateObj);  // ClientTimeZone is not used
                WriteInt(0, _physicalStateObj);  // LCID is unused by server

                // Start writing offset and length of variable length portions
                int offset = TdsEnums.YUKON_LOG_REC_FIXED_LEN;

                // write offset/length pairs

                // note that you must always set ibHostName since it indicaters the beginning of the variable length section of the login record
                WriteShort(offset, _physicalStateObj); // host name offset
                WriteShort(rec.hostName.Length, _physicalStateObj);
                offset += rec.hostName.Length * 2;

                // Only send user/password over if not fSSPI...  If both user/password and SSPI are in login
                // rec, only SSPI is used.  Confirmed same bahavior as in luxor.
                if (rec.useSSPI == false)
                {
                    WriteShort(offset, _physicalStateObj);  // userName offset
                    WriteShort(userName.Length, _physicalStateObj);
                    offset += userName.Length * 2;

                    // the encrypted password is a byte array - so length computations different than strings
                    WriteShort(offset, _physicalStateObj); // password offset
                    WriteShort(encryptedPasswordLengthInBytes / 2, _physicalStateObj);
                    offset += encryptedPasswordLengthInBytes;
                }
                else
                {
                    // case where user/password data is not used, send over zeros
                    WriteShort(0, _physicalStateObj);  // userName offset
                    WriteShort(0, _physicalStateObj);
                    WriteShort(0, _physicalStateObj);  // password offset
                    WriteShort(0, _physicalStateObj);
                }

                WriteShort(offset, _physicalStateObj); // app name offset
                WriteShort(rec.applicationName.Length, _physicalStateObj);
                offset += rec.applicationName.Length * 2;

                WriteShort(offset, _physicalStateObj); // server name offset
                WriteShort(rec.serverName.Length, _physicalStateObj);
                offset += rec.serverName.Length * 2;

                WriteShort(offset, _physicalStateObj);
                if (useFeatureExt)
                {
                    WriteShort(4, _physicalStateObj); // length of ibFeatgureExtLong (which is a DWORD)
                    offset += 4;
                }
                else
                {
                    WriteShort(0, _physicalStateObj); // ununsed (was remote password ?)
                }

                WriteShort(offset, _physicalStateObj); // client interface name offset
                WriteShort(clientInterfaceName.Length, _physicalStateObj);
                offset += clientInterfaceName.Length * 2;

                WriteShort(offset, _physicalStateObj); // language name offset
                WriteShort(rec.language.Length, _physicalStateObj);
                offset += rec.language.Length * 2;

                WriteShort(offset, _physicalStateObj); // database name offset
                WriteShort(rec.database.Length, _physicalStateObj);
                offset += rec.database.Length * 2;

                if (null == s_nicAddress)
                    s_nicAddress = TdsParserStaticMethods.GetNetworkPhysicalAddressForTdsLoginOnly();

                _physicalStateObj.WriteByteArray(s_nicAddress, s_nicAddress.Length, 0);

                WriteShort(offset, _physicalStateObj); // ibSSPI offset
                if (rec.useSSPI)
                {
                    WriteShort((int)outSSPILength, _physicalStateObj);
                    offset += (int)outSSPILength;
                }
                else
                {
                    WriteShort(0, _physicalStateObj);
                }

                WriteShort(offset, _physicalStateObj); // DB filename offset
                WriteShort(rec.attachDBFilename.Length, _physicalStateObj);
                offset += rec.attachDBFilename.Length * 2;

                WriteShort(offset, _physicalStateObj); // reset password offset
                WriteShort(0, _physicalStateObj);

                WriteInt(0, _physicalStateObj);        // reserved for chSSPI

                // write variable length portion
                WriteString(rec.hostName, _physicalStateObj);

                // if we are using SSPI, do not send over username/password, since we will use SSPI instead
                // same behavior as Luxor
                if (!rec.useSSPI)
                {
                    WriteString(userName, _physicalStateObj);

                    {
                        _physicalStateObj.WriteByteArray(encryptedPassword, encryptedPasswordLengthInBytes, 0);
                    }
                }

                WriteString(rec.applicationName, _physicalStateObj);
                WriteString(rec.serverName, _physicalStateObj);

                // write ibFeatureExtLong
                if (useFeatureExt)
                {
                    WriteInt(feOffset, _physicalStateObj);
                }

                WriteString(clientInterfaceName, _physicalStateObj);
                WriteString(rec.language, _physicalStateObj);
                WriteString(rec.database, _physicalStateObj);

                // send over SSPI data if we are using SSPI
                if (rec.useSSPI)
                    _physicalStateObj.WriteByteArray(outSSPIBuff, (int)outSSPILength, 0);

                WriteString(rec.attachDBFilename, _physicalStateObj);
                if (useFeatureExt)
                {
                    if ((requestedFeatures & TdsEnums.FeatureExtension.SessionRecovery) != 0)
                    {
                        length += WriteSessionRecoveryFeatureRequest(recoverySessionData, true);
                    };
                    _physicalStateObj.WriteByte(0xFF); // terminator
                }
            }
            catch (Exception e)
            {
                if (ADP.IsCatchableExceptionType(e))
                {
                    // be sure to wipe out our buffer if we started sending stuff
                    _physicalStateObj._outputPacketNumber = 1;  // end of message - reset to 1 - per ramas
                    _physicalStateObj.ResetBuffer();
                }

                throw;
            }

            _physicalStateObj.WritePacket(TdsEnums.HARDFLUSH);
            _physicalStateObj._pendingData = true;
            _physicalStateObj._messageStatus = 0;
        }// tdsLogin
        private void Login(ServerInfo server, TimeoutTimer timeout, string newPassword, SecureString newSecurePassword) {
            // create a new login record
            SqlLogin login = new SqlLogin();

            // gather all the settings the user set in the connection string or
            // properties and do the login
            CurrentDatabase   = server.ResolvedDatabaseName;
            _currentPacketSize = ConnectionOptions.PacketSize;
            _currentLanguage   = ConnectionOptions.CurrentLanguage;

            int timeoutInSeconds = 0;

            // If a timeout tick value is specified, compute the timeout based
            // upon the amount of time left in seconds.
            if (!timeout.IsInfinite)
            {
                long t = timeout.MillisecondsRemaining/1000;
                if ((long)Int32.MaxValue > t)
                {
                    timeoutInSeconds = (int)t;
                }
            }

            login.authentication = ConnectionOptions.Authentication;
            login.timeout = timeoutInSeconds;
            login.userInstance     = ConnectionOptions.UserInstance;
            login.hostName         = ConnectionOptions.ObtainWorkstationId();
            login.userName         = ConnectionOptions.UserID;
            login.password         = ConnectionOptions.Password;
            login.applicationName  = ConnectionOptions.ApplicationName;

            login.language         = _currentLanguage;
            if (!login.userInstance) { // Do not send attachdbfilename or database to SSE primary instance
                login.database         = CurrentDatabase;;
                login.attachDBFilename = ConnectionOptions.AttachDBFilename;
            }

            // VSTS#795621 - Ensure ServerName is Sent During TdsLogin To Enable Sql Azure Connectivity.
            // Using server.UserServerName (versus ConnectionOptions.DataSource) since TdsLogin requires 
            // serverName to always be non-null.
            login.serverName = server.UserServerName;

            login.useReplication   = ConnectionOptions.Replication;
            login.useSSPI          = ConnectionOptions.IntegratedSecurity 
                                     || (ConnectionOptions.Authentication == SqlAuthenticationMethod.ActiveDirectoryIntegrated && !_fedAuthRequired);
            login.packetSize       = _currentPacketSize;
            login.newPassword      = newPassword;
            login.readOnlyIntent   = ConnectionOptions.ApplicationIntent == ApplicationIntent.ReadOnly;
            login.credential       = _credential;
            if (newSecurePassword != null) {
                login.newSecurePassword = newSecurePassword;
            }

            TdsEnums.FeatureExtension requestedFeatures = TdsEnums.FeatureExtension.None;
            if (ConnectionOptions.ConnectRetryCount>0) {
                requestedFeatures |= TdsEnums.FeatureExtension.SessionRecovery;
                _sessionRecoveryRequested = true;
            }

            // If the workflow being used is Active Directory Password or Active Directory Integrated and server's prelogin response
            // for FEDAUTHREQUIRED option indicates Federated Authentication is required, we have to insert FedAuth Feature Extension
            // in Login7, indicating the intent to use Active Directory Authentication Library for SQL Server.
            if (ConnectionOptions.Authentication == SqlAuthenticationMethod.ActiveDirectoryPassword
                || (ConnectionOptions.Authentication == SqlAuthenticationMethod.ActiveDirectoryIntegrated && _fedAuthRequired)) {
                requestedFeatures |= TdsEnums.FeatureExtension.FedAuth;
                _federatedAuthenticationInfoRequested = true;
                _fedAuthFeatureExtensionData = 
                    new FederatedAuthenticationFeatureExtensionData { 
                        libraryType = TdsEnums.FedAuthLibrary.ADAL,
                        authentication = ConnectionOptions.Authentication,
                        fedAuthRequiredPreLoginResponse = _fedAuthRequired
                    };
            }
            if (_accessTokenInBytes != null) {
                requestedFeatures |= TdsEnums.FeatureExtension.FedAuth;
                _fedAuthFeatureExtensionData = new FederatedAuthenticationFeatureExtensionData {
                        libraryType = TdsEnums.FedAuthLibrary.SecurityToken,
                        fedAuthRequiredPreLoginResponse = _fedAuthRequired,
                        accessToken = _accessTokenInBytes
                    };
                // No need any further info from the server for token based authentication. So set _federatedAuthenticationRequested to true
                _federatedAuthenticationRequested = true;
            }

            // The TCE and GLOBALTRANSACTIONS feature are implicitly requested
            requestedFeatures |= TdsEnums.FeatureExtension.Tce | TdsEnums.FeatureExtension.GlobalTransactions;
            _parser.TdsLogin(login, requestedFeatures, _recoverySessionData, _fedAuthFeatureExtensionData);
        }
Exemplo n.º 5
0
        private void Login(ServerInfo server, TimeoutTimer timeout)
        {
            // create a new login record
            SqlLogin login = new SqlLogin();

            // gather all the settings the user set in the connection string or
            // properties and do the login
            CurrentDatabase = server.ResolvedDatabaseName;
            _currentPacketSize = ConnectionOptions.PacketSize;
            _currentLanguage = ConnectionOptions.CurrentLanguage;

            int timeoutInSeconds = 0;

            // If a timeout tick value is specified, compute the timeout based
            // upon the amount of time left in seconds.
            if (!timeout.IsInfinite)
            {
                long t = timeout.MillisecondsRemaining / 1000;
                if ((long)Int32.MaxValue > t)
                {
                    timeoutInSeconds = (int)t;
                }
            }

            login.timeout = timeoutInSeconds;
            login.userInstance = ConnectionOptions.UserInstance;
            login.hostName = ConnectionOptions.ObtainWorkstationId();
            login.userName = ConnectionOptions.UserID;
            login.password = ConnectionOptions.Password;
            login.applicationName = ConnectionOptions.ApplicationName;

            login.language = _currentLanguage;
            if (!login.userInstance)
            { // Do not send attachdbfilename or database to SSE primary instance
                login.database = CurrentDatabase; ;
                login.attachDBFilename = ConnectionOptions.AttachDBFilename;
            }

            // VSTS#795621 - Ensure ServerName is Sent During TdsLogin To Enable Sql Azure Connectivity.
            // Using server.UserServerName (versus ConnectionOptions.DataSource) since TdsLogin requires 
            // serverName to always be non-null.
            login.serverName = server.UserServerName;

            login.useReplication = ConnectionOptions.Replication;
            login.useSSPI = ConnectionOptions.IntegratedSecurity;
            login.packetSize = _currentPacketSize;
            login.readOnlyIntent = ConnectionOptions.ApplicationIntent == ApplicationIntent.ReadOnly;

            TdsEnums.FeatureExtension requestedFeatures = TdsEnums.FeatureExtension.None;
            if (ConnectionOptions.ConnectRetryCount > 0)
            {
                requestedFeatures |= TdsEnums.FeatureExtension.SessionRecovery;
                _sessionRecoveryRequested = true;
            }

            _parser.TdsLogin(login, requestedFeatures, _recoverySessionData);
        }
 private void Login(ServerInfo server, TimeoutTimer timeout, string newPassword)
 {
     SqlLogin rec = new SqlLogin();
     base.CurrentDatabase = server.ResolvedDatabaseName;
     this._currentPacketSize = base.ConnectionOptions.PacketSize;
     this._currentLanguage = base.ConnectionOptions.CurrentLanguage;
     int num2 = 0;
     if (!timeout.IsInfinite)
     {
         long num = timeout.MillisecondsRemaining / 0x3e8L;
         if (0x7fffffffL > num)
         {
             num2 = (int) num;
         }
     }
     rec.timeout = num2;
     rec.userInstance = base.ConnectionOptions.UserInstance;
     rec.hostName = base.ConnectionOptions.ObtainWorkstationId();
     rec.userName = base.ConnectionOptions.UserID;
     rec.password = base.ConnectionOptions.Password;
     rec.applicationName = base.ConnectionOptions.ApplicationName;
     rec.language = this._currentLanguage;
     if (!rec.userInstance)
     {
         rec.database = base.CurrentDatabase;
         rec.attachDBFilename = base.ConnectionOptions.AttachDBFilename;
     }
     rec.serverName = server.UserServerName;
     rec.useReplication = base.ConnectionOptions.Replication;
     rec.useSSPI = base.ConnectionOptions.IntegratedSecurity;
     rec.packetSize = this._currentPacketSize;
     rec.newPassword = newPassword;
     rec.readOnlyIntent = base.ConnectionOptions.ApplicationIntent == ApplicationIntent.ReadOnly;
     this._parser.TdsLogin(rec);
 }
 internal void TdsLogin(SqlLogin rec)
 {
     this._physicalStateObj.SetTimeoutSeconds(rec.timeout);
     byte[] b = null;
     b = TdsParserStaticMethods.EncryptPassword(rec.password);
     byte[] buffer2 = null;
     buffer2 = TdsParserStaticMethods.EncryptPassword(rec.newPassword);
     this._physicalStateObj._outputMessageType = 0x10;
     int v = 0x5e;
     string s = ".Net SqlClient Data Provider";
     v += ((((((rec.hostName.Length + rec.applicationName.Length) + rec.serverName.Length) + s.Length) + rec.language.Length) + rec.database.Length) + rec.attachDBFilename.Length) * 2;
     byte[] sendBuff = null;
     uint sendLength = 0;
     if (!rec.useSSPI)
     {
         v += ((rec.userName.Length * 2) + b.Length) + buffer2.Length;
     }
     else if (rec.useSSPI)
     {
         sendBuff = new byte[s_maxSSPILength];
         sendLength = s_maxSSPILength;
         this._physicalStateObj.SniContext = SniContext.Snix_LoginSspi;
         this.SSPIData(null, 0, sendBuff, ref sendLength);
         if (sendLength > 0x7fffffff)
         {
             throw SQL.InvalidSSPIPacketSize();
         }
         this._physicalStateObj.SniContext = SniContext.Snix_Login;
         v += (int) sendLength;
     }
     try
     {
         this.WriteInt(v, this._physicalStateObj);
         this.WriteInt(0x730a0003, this._physicalStateObj);
         this.WriteInt(rec.packetSize, this._physicalStateObj);
         this.WriteInt(0x6000000, this._physicalStateObj);
         this.WriteInt(TdsParserStaticMethods.GetCurrentProcessIdForTdsLoginOnly(), this._physicalStateObj);
         this.WriteInt(0, this._physicalStateObj);
         int num2 = 0;
         num2 |= 0x20;
         num2 |= 0x40;
         num2 |= 0x80;
         num2 |= 0x100;
         num2 |= 0x200;
         if (rec.useReplication)
         {
             num2 |= 0x3000;
         }
         if (rec.useSSPI)
         {
             num2 |= 0x8000;
         }
         if (rec.readOnlyIntent)
         {
             num2 |= 0x200000;
         }
         if (!ADP.IsEmpty(rec.newPassword))
         {
             num2 |= 0x1000000;
         }
         if (rec.userInstance)
         {
             num2 |= 0x4000000;
         }
         this.WriteInt(num2, this._physicalStateObj);
         if (Bid.AdvancedOn)
         {
             Bid.Trace("<sc.TdsParser.TdsLogin|ADV> %d#, TDS Login7 flags = %d:\n", this.ObjectID, num2);
         }
         this.WriteInt(0, this._physicalStateObj);
         this.WriteInt(0, this._physicalStateObj);
         int num = 0x5e;
         this.WriteShort(num, this._physicalStateObj);
         this.WriteShort(rec.hostName.Length, this._physicalStateObj);
         num += rec.hostName.Length * 2;
         if (!rec.useSSPI)
         {
             this.WriteShort(num, this._physicalStateObj);
             this.WriteShort(rec.userName.Length, this._physicalStateObj);
             num += rec.userName.Length * 2;
             this.WriteShort(num, this._physicalStateObj);
             this.WriteShort(b.Length / 2, this._physicalStateObj);
             num += b.Length;
         }
         else
         {
             this.WriteShort(0, this._physicalStateObj);
             this.WriteShort(0, this._physicalStateObj);
             this.WriteShort(0, this._physicalStateObj);
             this.WriteShort(0, this._physicalStateObj);
         }
         this.WriteShort(num, this._physicalStateObj);
         this.WriteShort(rec.applicationName.Length, this._physicalStateObj);
         num += rec.applicationName.Length * 2;
         this.WriteShort(num, this._physicalStateObj);
         this.WriteShort(rec.serverName.Length, this._physicalStateObj);
         num += rec.serverName.Length * 2;
         this.WriteShort(num, this._physicalStateObj);
         this.WriteShort(0, this._physicalStateObj);
         this.WriteShort(num, this._physicalStateObj);
         this.WriteShort(s.Length, this._physicalStateObj);
         num += s.Length * 2;
         this.WriteShort(num, this._physicalStateObj);
         this.WriteShort(rec.language.Length, this._physicalStateObj);
         num += rec.language.Length * 2;
         this.WriteShort(num, this._physicalStateObj);
         this.WriteShort(rec.database.Length, this._physicalStateObj);
         num += rec.database.Length * 2;
         if (s_nicAddress == null)
         {
             s_nicAddress = TdsParserStaticMethods.GetNetworkPhysicalAddressForTdsLoginOnly();
         }
         this.WriteByteArray(s_nicAddress, s_nicAddress.Length, 0, this._physicalStateObj);
         this.WriteShort(num, this._physicalStateObj);
         if (rec.useSSPI)
         {
             this.WriteShort((int) sendLength, this._physicalStateObj);
             num += (int) sendLength;
         }
         else
         {
             this.WriteShort(0, this._physicalStateObj);
         }
         this.WriteShort(num, this._physicalStateObj);
         this.WriteShort(rec.attachDBFilename.Length, this._physicalStateObj);
         num += rec.attachDBFilename.Length * 2;
         this.WriteShort(num, this._physicalStateObj);
         this.WriteShort(buffer2.Length / 2, this._physicalStateObj);
         this.WriteInt(0, this._physicalStateObj);
         this.WriteString(rec.hostName, this._physicalStateObj);
         if (!rec.useSSPI)
         {
             this.WriteString(rec.userName, this._physicalStateObj);
             this._physicalStateObj._tracePasswordOffset = this._physicalStateObj._outBytesUsed;
             this._physicalStateObj._tracePasswordLength = b.Length;
             this.WriteByteArray(b, b.Length, 0, this._physicalStateObj);
         }
         this.WriteString(rec.applicationName, this._physicalStateObj);
         this.WriteString(rec.serverName, this._physicalStateObj);
         this.WriteString(s, this._physicalStateObj);
         this.WriteString(rec.language, this._physicalStateObj);
         this.WriteString(rec.database, this._physicalStateObj);
         if (rec.useSSPI)
         {
             this.WriteByteArray(sendBuff, (int) sendLength, 0, this._physicalStateObj);
         }
         this.WriteString(rec.attachDBFilename, this._physicalStateObj);
         if (!rec.useSSPI)
         {
             this._physicalStateObj._traceChangePasswordOffset = this._physicalStateObj._outBytesUsed;
             this._physicalStateObj._traceChangePasswordLength = buffer2.Length;
             this.WriteByteArray(buffer2, buffer2.Length, 0, this._physicalStateObj);
         }
     }
     catch (Exception exception)
     {
         if (ADP.IsCatchableExceptionType(exception))
         {
             this._physicalStateObj._outputPacketNumber = 1;
             this._physicalStateObj.ResetBuffer();
         }
         throw;
     }
     this._physicalStateObj.WritePacket(1);
     this._physicalStateObj._pendingData = true;
 }