Manually logging in a user without password

前端 未结 5 2017
暖寄归人
暖寄归人 2020-12-07 10:18

I hope you can help me figure the best way to implement a manual (server-side initiated) login without using the password. Let me explain the workflow:

5条回答
  •  半阙折子戏
    2020-12-07 10:57

    As of Django 1.10, the process has been simplified.

    In all versions of Django, in order for a user to be logged in, they must be authenticated by one of your app's backends (controlled by the AUTHENTICATION_BACKENDS setting).

    If you simply want to force a login, you can just claim that the user was authenticated by the first backend from that list:

    from django.conf import settings
    from django.contrib.auth import login
    
    
    # Django 1.10+
    login(request, user, backend=settings.AUTHENTICATION_BACKENDS[0])
    
    # Django <1.10 -  fake the authenticate() call
    user.backend = settings.AUTHENTICATION_BACKENDS[0]
    login(request, user)
    

提交回复
热议问题