一、安装ldap3模块(python版本为python3以上,Django=1.11.8)
pip install ldap3

二、相关代码

from ldap3 import Server, Connection, ALL, SUBTREE, ServerPool,ALL_ATTRIBUTES

LDAP_SERVER_POOL = ["AD_IP1", "AD_IP2"]
LDAP_SERVER_PORT = 389
ADMIN_DN = "administrator@domainname.com"
ADMIN_PASSWORD = "xxxxxxx"
SEARCH_BASE = "ou=Users,dc=domainname,dc=com" def ldap_auth(username, password):
ldap_server_pool = ServerPool(LDAP_SERVER_POOL)
conn = Connection(ldap_server_pool, user=ADMIN_DN, password=ADMIN_PASSWORD, check_names=True, lazy=False, raise_exceptions=False)
conn.open()
conn.bind() res = conn.search(
search_base = SEARCH_BASE,
search_filter = '(sAMAccountName={})'.format(username),
search_scope = SUBTREE,
attributes = ['cn', 'givenName', 'mail', 'sAMAccountName','department','manager'],
#ALL_ATTRIBUTES:获取所有属性值
# attributes=ALL_ATTRIBUTES,
paged_size = 5
) if res:
entry = conn.response[0]
# print(entry)
dn = entry['dn']
attr_dict = entry['attributes'] # check password by dn
try:
conn2 = Connection(ldap_server_pool, user=dn, password=password, check_names=True, lazy=False, raise_exceptions=False)
conn2.bind()
if conn2.result["description"] == "success":
print((True,attr_dict["sAMAccountName"],password, attr_dict["mail"], attr_dict["cn"],attr_dict["department"], attr_dict["givenName"]))
return (True, attr_dict["sAMAccountName"],password, attr_dict["mail"],attr_dict["cn"],attr_dict["department"],attr_dict["givenName"])
else:
print("auth fail")
return (False, None, None, None)
except Exception as e:
print("auth fail")
return (False, None, None, None)
else:
return (False, None, None, None) if __name__ == "__main__":
ldap_auth("administrator", "xxxxxxxx")

官方文档链接:
        https://ldap3.readthedocs.io/index.html

Python使用ldap3认证的更多相关文章

  1. python使用ldap3进行接口调用

    把自己使用到的ldap调用的代码分享出来,希望大家可以参考 #!/usr/bin/python # -*- coding: utf-8 -*- """ @Time : 2 ...

  2. 用Python建设企业认证和权限控制平台

    目前大家对Python的了解更多来源是数据分析.AI.运维工具开发,在行业中使用Python进行web开发,同样也是非常受欢迎的,例如:FaceBook,豆瓣,知乎,饿了么等等,本文主要是介绍是利用P ...

  3. python urllib2 Basic认证

    1.通过添加http header 来实现 import urllib2 from base64 import encodestring url = 'http://202.108.1.51' use ...

  4. python之滑动认证(图片)

    from PIL import Image, ImageEnhance from io import BytesIO def cutImg(imgsrc): """ 根据 ...

  5. python api接口认证脚本

    import requests import sys def acces_api_with_cookie(url_login, USERNAME, PASSWORD, url_access):     ...

  6. Python—实现ssl认证

    https://blog.csdn.net/vip97yigang/article/details/84721027 https://www.cnblogs.com/lsdb/p/9397530.ht ...

  7. python 购物车+用户认证程序

    创建文件a.txt,b.txt.c.txt用于存放应该持续保存的信息 a.txt :用户密码输入错误3次就锁定 b.txt :购物时的活动,每个用户只能参与一次 c:txt :购物完后的发票在这里查看 ...

  8. 使用python发送简单的邮件

    from:http://blog.csdn.net/zhaoweikid/article/details/125898 前些时间,论坛上有人讨论怎么用python发送需要认证的邮件,我在我的FreeB ...

  9. IBM Python 技术专题

    Python 技术专题 Python 是由 Guido van Rossum 开发的,可免费获得的.是一种非常高级的解释型语言.其语法简单易懂,而且面向对象的语义功能强大又灵活,Python 可以广泛 ...

随机推荐

  1. iOS系统NSNotificationCenter中的常用通知名称

    //音频 AVF_EXPORT NSString *const AVAudioSessionInterruptionNotification //音频中断出现 AVF_EXPORT NSString ...

  2. daemon函数详解

    https://blog.csdn.net/xinyuan510214/article/details/50903280

  3. java异常处理解决方案

    一.异常概念 Throwable类是Java中所有错误或异常的超类. 1.只有当对象是此类(或其子类)的实例时,才能通过Java虚拟机或着Java throw语句抛出.     2.只有此类或其子类才 ...

  4. Spring Boot 基于Spring Initializer 的快速构建 day02

    一.基于Spring Initializr 快速构建Spring Boot项目(快速) 备注:需要联网 这是使用Intellij Idea快速构建可以为我们省去大量的pom.xml配置时间,简单操作, ...

  5. jQuery 学习笔记(3)(内容选择器、attr方法、prop方法,类的操作)

    内容选择器: 1.$("div:empty"): 空的div元素 2.$("div:parent"): 非空div元素 3.$("div:contai ...

  6. Python3学习之路~5.10 PyYAML模块

    Python也可以很容易的处理ymal文档格式,只不过需要安装一个模块,参考文档:http://pyyaml.org/wiki/PyYAMLDocumentation

  7. java框架之SpringCloud(2)-Rest微服务案例

    在上一章节已经对微服务与 SpringCloud 做了介绍,为方便后面学习,下面以 Dept 部门模块为例做一个微服务通用 Demo —— Consumer 消费者(Client) 通过 REST 调 ...

  8. phpstudy如何安装ssl证书

    网站上面部署ssl证书的站点越来越大,但有很多集成式的web服务器无法按照一般站点的配置来部署ssl证书,现在,卓趣科技就以集成式phpstudy为例(apache+mysql),为大家展示一下正确的 ...

  9. PHP----------安装包lnmp1.3-full安装的lnmp环境,如何安装PHP扩展

    1. 如果已经安装LNMP套件,请按以下步骤处理 a. 跳转到fileinfo源代码目录` cd /root/downloads/lnmp1.2-full/src/php-7.0.7/ext/file ...

  10. 第三章 document对象及数组

    1.数组的使用(1)声明数组var 数组名=new Array();(2)数组赋值数组名[下标]=值: 2.数组声明,分配空间,赋值同时进行var 数组名=new Array(值1,值2....)va ...