djangosessionauthenticationdjango-auth-ldap

What exactly is _auth_user_id (in connection with django-auth-ldap)?


In Django you can read _auth_user_id from request.session dictionary after you authenticate and log in user. As I understand in "normal" conditions (standard authentication backend) it should be user id (pk).

But what is _auth_user_id when I'm using django-auth-ldap as authentication backend? It returns integers but there is no such values in my LDAP database.


Solution

  • Every Django authentication backend--django-auth-ldap included--ultimately returns an instance of django.contrib.auth.models.User on a successful authentication. django.contrib.sessions, in turn, knows only about these user objects; it doesn't know or care which backend produced them. So request.session['_auth_user_id'] should be a User pk regardless of your backend. As a rule, of course, you would access request.user for convenience.

    If you're using django-auth-ldap and you need to get back to the LDAP user, you can look at request.user.ldap_user. See the documentation for details and performance considerations.