[英]Django Authentication LDAP
我有一个Django项目,我想在其中对Active Directory进行用户身份验证。 因此,我正在使用django-python3-ldap框架。 我能够同步用户( ./manage.py ldap_sync_users
),授予超级用户管理员访问权限,并使用框架作为后端登录默认的Django管理页面。 但是,当我尝试在我的网站上进行身份验证时,用户状态仍为AnonymousUser
。
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
<!-- 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>
进口
from django.contrib.auth import authenticate, login
import django_python3_ldap
后端
AUTHENTICATION_BACKENDS = ('django_python3_ldap.auth.LDAPBackend',)
urls.py
path('login', auth_views.login, {'template_name': 'website/login.html'}, name='login'),
我看到后端可能未指向有效后端的两个问题。 另外,您的尝试除了有点危险外,请删除它。 除非进行测试,否则请勿使用空白。 使用except Exception as e: print(e)
东西except Exception as e: print(e)
。 我说的原因是空白,除了隐藏了无效的后端错误。
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.