简体   繁体   English

在 Django 中测试身份验证

[英]Testing authentication in Django

I'm trying to implement LDAP authentication in a test Django project.我正在尝试在测试 Django 项目中实现 LDAP 身份验证。 I think I've done all the configuration correctly (I'm using the django_ldap_auth package) and I want to test the authentication without having to create a whole site with log in etc. Is that possible?我想我已经正确完成了所有配置(我正在使用 django_ldap_auth 包)并且我想测试身份验证而不必创建一个带有登录等的整个站点。这可能吗? FYI, I'm only doing this as a part of making LDAP the main authentication method at my company, and have to plug the functionality into a large project.仅供参考,我只是将 LDAP 作为我公司的主要身份验证方法的一部分,并且必须将该功能插入一个大型项目。

I've tried running the server on localhost and ssh-ing through the terminal, but apparently that only logs me onto my own computer, I think:) (not an experienced Linux user).我尝试在 localhost 上运行服务器并通过终端进行 ssh-ing,但显然这只会让我登录到我自己的计算机上,我认为:)(不是经验丰富的 Linux 用户)。

Any suggestions?有什么建议么?

Go to your Django project folder and Start the python interpreter with Go 到您的 Django 项目文件夹并启动 python 解释器

python manage.py shell

and then do,然后做,

from django_auth_ldap.backend import LDAPBackend

ldapobj = LDAPBackend()
user = ldapobj.populate_user(<LDAP username of an existing user>)
user.is_anonymous()

if the last function call returns false then it your LDAP auth module works as expected.如果最后一个 function 调用返回 false 那么您的 LDAP 身份验证模块按预期工作。

edit:编辑:

Run the Python interpreter as above and,如上所述运行 Python 解释器,

import ldap
server = 'ldap://<your server ip>'
user_dn = '<dn for a known user>'
password = '<his or her password>'
con = ldap.initialize(server)
con.simple_bind_s(user_dn, password)

This will return SERVER_DOWN: {'desc': "Can't contact LDAP server"} exception, if you can't connect to the LDAP sever.这将返回 SERVER_DOWN: {'desc': "Can't contact LDAP server"} 异常,如果您无法连接到 LDAP 服务器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM