private void Authenticate(IConnection connection)
 {
     if (!connection.IsAuthenticated)
     {
         if (SaslMechanism != null)
         {
             var result = SaslMechanism.Authenticate(connection);
             if (result)
             {
                 Log.Debug(
                     m =>
                     m("Authenticated {0} using {1} - {2} [{3}].", SaslMechanism.Username, SaslMechanism.GetType(),
                       _identity, EndPoint));
                 connection.IsAuthenticated = true;
             }
             else
             {
                 Log.Debug(
                     m =>
                     m("Could not authenticate {0} using {1} - {2} [{3}].", SaslMechanism.Username,
                       SaslMechanism.GetType(), _identity, EndPoint));
                 throw new AuthenticationException(ExceptionUtil.FailedBucketAuthenticationMsg.WithParams(SaslMechanism.Username));
             }
         }
     }
 }
        protected void Authenticate(IConnection connection)
        {
            if (Configuration.ClientConfiguration?.EnableCertificateAuthentication ?? false)
            {
                Log.Trace("Ignoring authentication using x509 cert.");
                //ignore auth if cert auth is being used
                return;
            }
            Log.Trace("1. Checking authentication [{0}|{1}]: {2} - {3}", connection.IsAuthenticated,
                      connection.IsDead, EndPoint, connection.Identity);

            if (connection.IsAuthenticated || connection.IsDead)
            {
                return;
            }

            Log.Trace("2. Checking authentication [{0}|{1}]: {2} - {3}", connection.IsAuthenticated,
                      connection.IsDead, EndPoint, connection.Identity);

            if (SaslMechanism != null)
            {
                Log.Trace("3. Checking authentication [{0}]: {1} - {2}", connection.IsAuthenticated, EndPoint,
                          connection.Identity);
                var result = SaslMechanism.Authenticate(connection);
                if (result)
                {
                    Log.Info(
                        "4. Authenticated {0} using {1} - {2} - {3} [{4}].", User(SaslMechanism.Username),
                        SaslMechanism.GetType(),
                        Identity, connection.Identity, EndPoint);
                    connection.IsAuthenticated = true;
                }
                else
                {
                    Log.Info(
                        "4. Could not authenticate {0} using {1} - {2} [{3}].", User(SaslMechanism.Username),
                        SaslMechanism.GetType(), Identity, EndPoint);

                    connection.IsDead = true;
                    connection.Dispose();

                    var message = SupportsEnhancedAuthentication
                        ? ExceptionUtil.FailedUserAuthenticationMsg.WithParams(User(SaslMechanism.Username))
                        : ExceptionUtil.FailedBucketAuthenticationMsg.WithParams(Configuration.BucketName);

                    throw new AuthenticationException(message);
                }
            }
        }
        protected void Authenticate(IConnection connection)
        {
            Log.Trace("1. Checking authentication [{0}|{1}]: {2} - {3}", connection.IsAuthenticated,
                      connection.IsDead, EndPoint, connection.Identity);

            if (connection.IsAuthenticated || connection.IsDead)
            {
                return;
            }

            Log.Trace("2. Checking authentication [{0}|{1}]: {2} - {3}", connection.IsAuthenticated,
                      connection.IsDead, EndPoint, connection.Identity);

            if (SaslMechanism != null)
            {
                Log.Trace("3. Checking authentication [{0}]: {1} - {2}", connection.IsAuthenticated, EndPoint,
                          connection.Identity);
                var result = SaslMechanism.Authenticate(connection);
                if (result)
                {
                    Log.Info(
                        "4. Authenticated {0} using {1} - {2} - {3} [{4}].", SaslMechanism.Username,
                        SaslMechanism.GetType(),
                        Identity, connection.Identity, EndPoint);
                    connection.IsAuthenticated = true;
                }
                else
                {
                    Log.Info(
                        "4. Could not authenticate {0} using {1} - {2} [{3}].", SaslMechanism.Username,
                        SaslMechanism.GetType(), Identity, EndPoint);

                    var message = SupportsEnhancedAuthentication
                        ? ExceptionUtil.FailedUserAuthenticationMsg.WithParams(SaslMechanism.Username)
                        : ExceptionUtil.FailedBucketAuthenticationMsg.WithParams(Configuration.BucketName);

                    throw new AuthenticationException(message);
                }
            }
        }