Index: django/contrib/auth/__init__.py
===================================================================
--- django/contrib/auth/__init__.py	(revision 12381)
+++ django/contrib/auth/__init__.py	(working copy)
@@ -1,5 +1,6 @@
 import datetime
 from warnings import warn
+from django.conf import settings
 from django.core.exceptions import ImproperlyConfigured
 from django.utils.importlib import import_module
 
@@ -35,7 +36,6 @@
     return cls()
 
 def get_backends():
-    from django.conf import settings
     backends = []
     for backend_path in settings.AUTHENTICATION_BACKENDS:
         backends.append(load_backend(backend_path))
@@ -96,8 +96,11 @@
     try:
         user_id = request.session[SESSION_KEY]
         backend_path = request.session[BACKEND_SESSION_KEY]
-        backend = load_backend(backend_path)
-        user = backend.get_user(user_id) or AnonymousUser()
+        if backend_path in settings.AUTHENTICATION_BACKENDS:
+            backend = load_backend(backend_path)
+            user = backend.get_user(user_id) or AnonymousUser()
+        else:
+            user = AnonymousUser()
     except KeyError:
         user = AnonymousUser()
     return user
Index: django/contrib/auth/tests/auth_backends.py
===================================================================
--- django/contrib/auth/tests/auth_backends.py	(revision 12381)
+++ django/contrib/auth/tests/auth_backends.py	(working copy)
@@ -1,6 +1,8 @@
 from django.conf import settings
+from django.contrib import auth 
 from django.contrib.auth.models import User, Group, Permission, AnonymousUser
 from django.contrib.contenttypes.models import ContentType
+from django.http import HttpRequest 
 from django.test import TestCase
 
 
@@ -245,3 +247,49 @@
 
     def test_get_all_permissions(self):
         self.assertEqual(self.user1.get_all_permissions(TestObj()), set())
+
+
+class StubBackend(object): 
+    USER_1 = User() 
+    def get_user(self, id):
+        if id == 1: return self.USER_1
+        return None 
+
+class BackendCachedTest(TestCase): 
+    """
+    Tests that User is loaded with the backend cached in the session.
+    """
+
+    backend = 'django.contrib.auth.tests.auth_backends.StubBackend'
+    valid_uid = 1
+    invalid_uid = -1
+
+    def setUp(self): 
+        self.curr_auth = settings.AUTHENTICATION_BACKENDS
+        settings.AUTHENTICATION_BACKENDS = (self.backend,) 
+        self.request = HttpRequest() 
+        self.request.session = {} 
+      
+    def tearDown(self): 
+        settings.AUTHENTICATION_BACKENDS = self.curr_auth
+
+    def set_up_session(self, uid, backend): 
+        self.request.session[auth.SESSION_KEY] = uid 
+        self.request.session[auth.BACKEND_SESSION_KEY] = backend 
+       
+    def test_valid_user_valid_backend(self): 
+        self.set_up_session(uid=self.valid_uid, backend=self.backend) 
+        user = auth.get_user(self.request) 
+        self.assert_(user is StubBackend.USER_1) 
+       
+    def test_invalid_user_valid_backend(self): 
+        self.set_up_session(uid=self.invalid_uid, backend=self.backend) 
+        user = auth.get_user(self.request) 
+        self.assert_(isinstance(user,AnonymousUser))
+       
+    def test_valid_user_backend_not_in_settings(self): 
+        self.set_up_session(uid=self.invalid_uid, backend=self.backend) 
+        settings.AUTHENTICATION_BACKENDS = tuple()
+        user = auth.get_user(self.request) 
+        self.assert_(isinstance(user,AnonymousUser)) 
+
Index: django/contrib/auth/tests/__init__.py
===================================================================
--- django/contrib/auth/tests/__init__.py	(revision 12381)
+++ django/contrib/auth/tests/__init__.py	(working copy)
@@ -4,7 +4,7 @@
 from django.contrib.auth.tests.forms import FORM_TESTS
 from django.contrib.auth.tests.remote_user \
         import RemoteUserTest, RemoteUserNoCreateTest, RemoteUserCustomTest
-from django.contrib.auth.tests.auth_backends import BackendTest, RowlevelBackendTest, AnonymousUserBackendTest, NoAnonymousUserBackendTest
+from django.contrib.auth.tests.auth_backends import BackendTest, RowlevelBackendTest, AnonymousUserBackendTest, NoAnonymousUserBackendTest, BackendCachedTest
 from django.contrib.auth.tests.tokens import TOKEN_GENERATOR_TESTS
 
 # The password for the fixture data users is 'password'
