配置了多个身份验证后端,因此必须提供 `backend` 参数或在用户上设置 `backend` 属性
Posted
技术标签:
【中文标题】配置了多个身份验证后端,因此必须提供 `backend` 参数或在用户上设置 `backend` 属性【英文标题】:multiple authentication backends configured and therefore must provide the `backend` argument or set the `backend` attribute on the user 【发布时间】:2018-02-08 05:30:49 【问题描述】:首先我是 django/python 的新手。
我正在尝试创建一个登录网站,允许用户注册帐户并通过电子邮件验证或直接通过 fb 或 google(Oauth)登录
当我点击发送到电子邮件的验证 url 时收到错误消息。
错误:
ValueError at /activate/Mjk/4p1-dcc5f7ed2e7c847fe362/
You have multiple authentication backends configured and therefore must
provide the `backend` argument or set the `backend` attribute on the user.
Request Method: GET
Request URL: http://127.0.0.1:8000/activate/Mjk/4p1-dcc5f7ed2e7c847fe362/
Django Version: 1.11.3
Exception Type: ValueError
Exception Value:
You have multiple authentication backends configured and therefore must provide the `backend` argument or set the `backend` attribute on the user.
Exception Location: /usr/local/lib/python2.7/dist-packages/django/contrib/auth/__init__.py in login, line 149
Python Executable: /usr/bin/python
Python Version: 2.7.12
Python Path:
['/home/gaby/django projects/simple-signup-master/profile-model',
'/usr/local/lib/python2.7/dist-packages/virtualenv-15.1.0-py2.7.egg',
'/usr/lib/python2.7',
'/usr/lib/python2.7/plat-x86_64-linux-gnu',
'/usr/lib/python2.7/lib-tk',
'/usr/lib/python2.7/lib-old',
'/usr/lib/python2.7/lib-dynload',
'/home/gaby/.local/lib/python2.7/site-packages',
'/usr/local/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages/PILcompat',
'/usr/lib/python2.7/dist-packages/gtk-2.0',
'/usr/lib/python2.7/dist-packages/ubuntu-sso-client']
Server time: Wed, 30 Aug 2017 12:34:31 +0000
我的网站/设置
AUTHENTICATION_BACKENDS = (
'social_core.backends.facebook.FacebookOAuth2',
'social_core.backends.google.GoogleOAuth2',
'django.contrib.auth.backends.ModelBackend',
)
这是我收到错误时调用的函数
def activate(request, uidb64, token):
try:
uid = force_text(urlsafe_base64_decode(uidb64))
user = User.objects.get(pk=uid)
except (TypeError, ValueError, OverflowError, User.DoesNotExist):
user = None
if user is not None and account_activation_token.check_token(user,
token):
user.is_active = True
user.profile.email_confirmed = True
user.save()
login(request, user)
return redirect('home')
else:
return render(request, 'account_activation_invalid.html')
在通过 fb、google 添加誓言登录后,我开始收到错误消息。 如果我从 AUTHENTICATION_BACKENDS 中删除 'social_core.backends.facebook.FacebookOAuth2','social_core.backends.google.GoogleOAuth2' ,电子邮件确认将再次起作用。
我在网上搜索,这里我找到的唯一解决方案如下,但它没有解决我的问题。
here
【问题讨论】:
【参考方案1】:我找到了解决方案。我必须将后端添加到函数中。
def activate(request, uidb64, token, backend='django.contrib.auth.backends.ModelBackend'):
try:
uid = force_text(urlsafe_base64_decode(uidb64))
user = User.objects.get(pk=uid)
except (TypeError, ValueError, OverflowError, User.DoesNotExist):
user = None
if user is not None and account_activation_token.check_token(user, token):
user.is_active = True
user.profile.email_confirmed = True
user.save()
login(request, user, backend='django.contrib.auth.backends.ModelBackend')
return redirect('home')
else:
return render(request, 'account_activation_invalid.html')
【讨论】:
就我而言,我必须在调用登录方法之前对用户进行身份验证。【参考方案2】:所以在我的情况下,我只是没有注册用户。还有login(request, user, backend='django.contrib.auth.backends.ModelBackend')
注册用户:
# register.py
from django.contrib.auth.models import User
cfg_user = 'user'
user = User.objects.create_user(username=cfg_user,
email='email@domain.com',
password='password')
print('registered user '+cfg_user)
# register.sh
python manage.py shell < register.py
【讨论】:
【参考方案3】:如果您使用的是 django 类 PasswordResetConfirmView 中的构建,那么您可以通过添加来修复它:
post_reset_login_backend = "django.contrib.auth.backends.RemoteUserBackend"
类似这样的:
from django.contrib.auth import views
class PasswordResetConfirmView(views.PasswordResetConfirmView):
template_name = "reset_confirm.html"
success_url = "/"
post_reset_login = True
post_reset_login_backend = "django.contrib.auth.backends.RemoteUserBackend"
【讨论】:
【参考方案4】:在我的情况下,只需简单地将 Django 配置中的一些后端类分配给用户即可正常工作:
setattr(user, 'backend', 'django.contrib.auth.backends.RemoteUserBackend')
然后你就可以验证你的用户了
【讨论】:
【参考方案5】:按照here 的建议,您需要设置用户记录的后端。在login(request, user)
之前添加以下行:
user.backend = 'django.contrib.auth.backends.ModelBackend'
...
login(request, user)
【讨论】:
以上是关于配置了多个身份验证后端,因此必须提供 `backend` 参数或在用户上设置 `backend` 属性的主要内容,如果未能解决你的问题,请参考以下文章
在哪里存储身份验证令牌(前端)以及如何将其放入多个端点的 http 标头中?