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

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

问题描述

我正在尝试在测试 Django 项目中实现 LDAP 身份验证.我想我已经正确完成了所有配置(我正在使用 django_ldap_auth 包)并且我想测试身份验证而不必创建一个带有登录等的整个站点.这可能吗?仅供参考,我这样做只是为了让 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.

我尝试在 localhost 上运行服务器并通过终端 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 项目文件夹并启动 python 解释器

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)

如果您无法连接到 LDAP 服务器,这将返回 SERVER_DOWN: {'desc': "Can't contact LDAP server"} 异常.

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天全站免登陆