[英]Django Authentication LDAP
I have a Django project in which I want to authenticate users against an Active Directory. 我有一个Django项目,我想在其中对Active Directory进行用户身份验证。 Therefore, I am using the django-python3-ldap framework.
因此,我正在使用django-python3-ldap框架。 I am able to sync users(
./manage.py ldap_sync_users
), grant superuser admin access, and login to the default Django admin page using the framework as backend. 我能够同步用户(
./manage.py ldap_sync_users
),授予超级用户管理员访问权限,并使用框架作为后端登录默认的Django管理页面。 However, when I try to authenticate on my site, the user state remains AnonymousUser
. 但是,当我尝试在我的网站上进行身份验证时,用户状态仍为
AnonymousUser
。
views.py views.py
def login(request):
try:
username = request.POST['username']
password = request.POST['password']
user = authenticate(request, username=username, password=password)
if user is not None:
login(request, user, backend=django_python3_ldap)
context = {'user': user}
return render(request, 'website/home.html', context)
else:
context = {'': ''}
return render(request, 'website/login.html', context)
except:
print()
login.html login.html
<!-- Main content --> <section class="hero is-success is-fullheight"> <div class="hero-body"> <div class="container has-text-centered"> <div class="column is-4 is-offset-4"> <div class="box"> <figure class="avatar"> <img src=""> </figure> <form method="post"> {% csrf_token %} <div class="field"> <div class="control"> <input class="input is-large" type="text" placeholder="Username" autofocus=""> </div> </div> <div class="field"> <div class="control"> <input class="input is-large" type="password" placeholder="Password"> </div> </div> <div class="field"> <label class="checkbox"> <input type="checkbox"> Remember Me </label> </div> <button class="button is-block is-danger is-info is-large is-fullwidth">Login</button> {% endblock %} </form> </div> </div> </div> </div> </section>
imports 进口
from django.contrib.auth import authenticate, login
import django_python3_ldap
backend 后端
AUTHENTICATION_BACKENDS = ('django_python3_ldap.auth.LDAPBackend',)
urls.py urls.py
path('login', auth_views.login, {'template_name': 'website/login.html'}, name='login'),
I see possibly two issues the backend is not pointing to a valid backend. 我看到后端可能未指向有效后端的两个问题。 Also your try except is a bit dangerous please remove it.
另外,您的尝试除了有点危险外,请删除它。 Unless you are testing never use a blank except.
除非进行测试,否则请勿使用空白。 Use something like
except Exception as e: print(e)
. 使用
except Exception as e: print(e)
东西except Exception as e: print(e)
。 The reason I say that is the blank except was hiding the invalid backend error. 我说的原因是空白,除了隐藏了无效的后端错误。
login(request, user, backend=django_python3_ldap)
# Should be
login(request, user, backend=django_python3_ldap.auth.LDAPBackend)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.