Python使用ldap3认证
一、安装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认证的更多相关文章
- python使用ldap3进行接口调用
把自己使用到的ldap调用的代码分享出来,希望大家可以参考 #!/usr/bin/python # -*- coding: utf-8 -*- """ @Time : 2 ...
- 用Python建设企业认证和权限控制平台
目前大家对Python的了解更多来源是数据分析.AI.运维工具开发,在行业中使用Python进行web开发,同样也是非常受欢迎的,例如:FaceBook,豆瓣,知乎,饿了么等等,本文主要是介绍是利用P ...
- python urllib2 Basic认证
1.通过添加http header 来实现 import urllib2 from base64 import encodestring url = 'http://202.108.1.51' use ...
- python之滑动认证(图片)
from PIL import Image, ImageEnhance from io import BytesIO def cutImg(imgsrc): """ 根据 ...
- python api接口认证脚本
import requests import sys def acces_api_with_cookie(url_login, USERNAME, PASSWORD, url_access): ...
- Python—实现ssl认证
https://blog.csdn.net/vip97yigang/article/details/84721027 https://www.cnblogs.com/lsdb/p/9397530.ht ...
- python 购物车+用户认证程序
创建文件a.txt,b.txt.c.txt用于存放应该持续保存的信息 a.txt :用户密码输入错误3次就锁定 b.txt :购物时的活动,每个用户只能参与一次 c:txt :购物完后的发票在这里查看 ...
- 使用python发送简单的邮件
from:http://blog.csdn.net/zhaoweikid/article/details/125898 前些时间,论坛上有人讨论怎么用python发送需要认证的邮件,我在我的FreeB ...
- IBM Python 技术专题
Python 技术专题 Python 是由 Guido van Rossum 开发的,可免费获得的.是一种非常高级的解释型语言.其语法简单易懂,而且面向对象的语义功能强大又灵活,Python 可以广泛 ...
随机推荐
- ZOJ 4063 - Tournament - [递归][2018 ACM-ICPC Asia Qingdao Regional Problem F]
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4063 Input Output Sample Input 2 3 ...
- GIT 身份验证失败问题解决方案,由于修改密码产生的问题
fatal: Authentication failed for 'http:xxxxxxxxxx.git/' 解决方案 1. git config --global user.name " ...
- 微信小程序http 400问题
在v0.14.140900版本的wechat小程序开发工具中做网络请求,直接使用微信的网络请求代码debug过程中发生了400 (Bad request)错误. wx.request({ url: ' ...
- centos6 安装python2.7 并做软件兼容处理 及 MySQLdb模块安装
相关软件准备 https://www.python.org/ftp/python/2.7.15/Python-2.7.15.tgz https://pypi.org/project/setuptool ...
- 极验验证使用-滑动&选字验证码
准备 SDK下载 首先在极验官网下载好SDK,附上官网链接,点此可直接下载python版zip包. 模块安装 使用该SDK时发现它依赖两个模块,分别是geetest和requests. pip ins ...
- Could not open JDBC Connection for transaction; nested exception is com.alibaba.druid.pool.GetConnection
Could not open JDBC Connection for transaction; nested exception is com.alibaba.druid.pool.GetConnec ...
- fiddler学习总结--手机端(APP/微信小程序)抓包
步骤一.手机和电脑要在同一个局域网中 步骤二.完成fiddler的基本配置,与web端抓包一样: TOOLS-->options-->connections-->1.设置端口:2.勾 ...
- spring boot + vue + element-ui全栈开发入门——集成element-ui
一.IDE开发工具 常用的开发工具有webstorm和sublime. 我个人喜好用Atom+插件的形式 打开Atom,在file --> settings --> packages中收 ...
- GCD(IV)
死锁:2个任务相互等待造成的. - (void) GCD { NSLog(@"begin"); dispatch_queue_t queue = dispatch_queue_cr ...
- 通用订单搜索的API设计得失录
先把 Joshua Bloch 大神的 API PDF 放在这里膜拜下:"How to Design a Good API and Why it Matters.pdf" 总述 在 ...