diff -r 4f4d617956c0 django/test/client.py
--- a/django/test/client.py	Mon Mar 05 17:30:27 2012 +0000
+++ b/django/test/client.py	Thu Mar 08 14:26:00 2012 -0300
@@ -497,8 +497,19 @@
         not available.
         """
         user = authenticate(**credentials)
-        if user and user.is_active \
-                and 'django.contrib.sessions.middleware.SessionMiddleware' in settings.MIDDLEWARE_CLASSES:
+        sessions_enabled = False
+        if user and user.is_active:
+            from django.contrib.sessions.middleware import SessionMiddleware
+            for middleware in settings.MIDDLEWARE_CLASSES:
+                try:
+                    mod, dot, klass_name = middleware.rpartition('.')
+                    mod = import_module(mod)
+                    if issubclass(getattr(mod, klass_name, None), SessionMiddleware):
+                        sessions_enabled = True
+                        break
+                except ImportError:
+                    continue
+        if sessions_enabled:
             engine = import_module(settings.SESSION_ENGINE)
 
             # Create a fake request to store login details.
diff -r 4f4d617956c0 tests/regressiontests/test_client_regress/middleware.py
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/regressiontests/test_client_regress/middleware.py	Thu Mar 08 14:26:00 2012 -0300
@@ -0,0 +1,9 @@
+from django.contrib.sessions.middleware import SessionMiddleware
+
+
+class CustomizedDjangoSessionMiddleware(SessionMiddleware):
+    pass
+
+
+class ReallyCustomSessionMiddleware(object):
+    pass
diff -r 4f4d617956c0 tests/regressiontests/test_client_regress/models.py
--- a/tests/regressiontests/test_client_regress/models.py	Mon Mar 05 17:30:27 2012 +0000
+++ b/tests/regressiontests/test_client_regress/models.py	Thu Mar 08 14:26:00 2012 -0300
@@ -2,6 +2,8 @@
 """
 Regression tests for the Test Client, especially the customized assertions.
 """
+from __future__ import absolute_import
+
 import os
 import warnings
 
@@ -17,6 +19,10 @@
 from django.template.response import SimpleTemplateResponse
 from django.http import HttpResponse
 
+from . import middleware
+
+CUSTOM_SESSION_MIDDLEWARE1 = '%s.CustomizedDjangoSessionMiddleware' % middleware.__name__
+CUSTOM_SESSION_MIDDLEWARE2 = '%s.ReallyCustomSessionMiddleware' % middleware.__name__
 
 class AssertContainsTests(TestCase):
     def setUp(self):
@@ -564,17 +570,42 @@
         self.assertEqual(response.context['user'].username, 'testclient')
 
 
-class NoSessionsAppInstalled(SessionEngineTests):
-    """#7836 - Test client can exercise sessions even when 'django.contrib.sessions' isn't installed."""
+class SessionMiddlewareTests(TestCase):
+    fixtures = ['testdata']
 
     # Remove the 'session' contrib app from INSTALLED_APPS
     @override_settings(INSTALLED_APPS=tuple(filter(lambda a: a!='django.contrib.sessions', settings.INSTALLED_APPS)))
-    def test_session(self):
+    def test_no_session_app(self):
+        """Test client can exercise sessions even when 'django.contrib.sessions' isn't installed (#7836)."""
         # This request sets a session variable.
         response = self.client.get('/test_client_regress/set_session/')
         self.assertEqual(response.status_code, 200)
         self.assertEqual(self.client.session['session_var'], 'YES')
 
+    # Use a custom session middleware that inherits from Django's
+    @override_settings(
+        MIDDLEWARE_CLASSES=map(
+            lambda m: m if m!='django.contrib.sessions.middleware.SessionMiddleware' else CUSTOM_SESSION_MIDDLEWARE1,
+            settings.MIDDLEWARE_CLASSES
+        )
+    )
+    def test_subclassed_session_middleware(self):
+        """Test client can login when a subclass of Django SessionMiddleware is in use (#16605)."""
+        login = self.client.login(username='testclient', password='password')
+        self.assertTrue(login, 'Could not log in')
+
+    # Use a custom session middleware
+    @override_settings(
+        MIDDLEWARE_CLASSES=map(
+            lambda m: m if m!='django.contrib.sessions.middleware.SessionMiddleware' else CUSTOM_SESSION_MIDDLEWARE2,
+            settings.MIDDLEWARE_CLASSES
+        )
+    )
+    def test_custom_session_middleware(self):
+        """Test client can login when a custom session middleware is in use."""
+        login = self.client.login(username='testclient', password='password')
+        self.assertTrue(login, 'Could not log in')
+
 
 class URLEscapingTests(TestCase):
     def test_simple_argument_get(self):
