在Django中测试身份验证 [英] Testing authentication in Django

查看:122
本文介绍了在Django中测试身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在测试Django项目中实现LDAP身份验证。我想我已经完成了所有的配置(我正在使用django_ldap_auth包),我想测试身份验证,而无需创建一个整个网站登录等等。 FYI,我只是这样做,作为使我的公司成为LDAP的主要认证方法的一部分,必须将功能插入一个大型项目。

I'm trying to implement LDAP authentication in a test Django project. 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 ? 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.

我尝试在本地主机上运行服务器,通过终端运行ssh-ing,但显然只将我登录到我自己的电脑上,我想:)(不是有经验的Linux用户)。

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).

任何建议?

推荐答案

转到您的Django项目文件夹,并使用

Go to your Django project folder and Start the python interpreter with

python manage.py shell

然后执行

from django_auth_ldap.backend import LDAPBackend

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

如果最后一个函数调用返回false,那么您的LDAP认证模块按预期工作。

if the last function call returns false then it your LDAP auth module works as expected.

编辑:

运行Python解释器如上,

Run the Python interpreter as above and,

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)

这将返回SERVER_DOWN:{'desc':无法联系LDAP服务器}异常,如果您无法连接到LDAP服务器。

This will return SERVER_DOWN: {'desc': "Can't contact LDAP server"} exception, if you can't connect to the LDAP sever.

这篇关于在Django中测试身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆