通过ldap python将用户添加到活动目录

时间:2018-05-25 11:30:44

标签: python python-3.x active-directory ldap

我编写了以下代码,用于将用户添加到活动目录2012中 我使用pycharmpython3.5 但是我收到了这个错误:

{'info': '000020D6: SvcErr: DSID-0310081B, problem 5012 (DIR_ERROR), data 0\n', 'desc': 'Operations error'}

我的代码如下:

server = 'ldap://31.184.132.39:389'
ldap_pass = 'function92'
ldap_bind = 'ou=DaaSUsers,dc=xaas,dc=local'

def create_user_activedirectory(username , password , name ):
    username = str(username)
    password=str(password)
    name=str(name)
    con = ldap.initialize(server)
    con.simple_bind_s("administrator@xaas.local", "function92")
    dn = "cn="+username+", ou=DaaSUsers, o=XaaS.local"
    mymodlist = {
         "objectClass": ["account".encode('utf-8'), "posixAccount".encode('utf-8'), "shadowAccount" .encode('utf-8')],
        #"objectClass": [str("inetOrgPerson").encode('utf-8')],
        "cn":[str(name).encode('utf-8')],
        "uid": [str(username).encode('utf-8')],
        "uidNumber": [str("5025").encode('utf-8')],
        "gidNumber": [str("30033").encode('utf-8')],
        "homeDirectory": [str("/home/"+name).encode('utf-8')],
        "loginShell": ["/bin/bash".encode('utf-8')],
        "gecos" : [str(username).encode('utf-8')],
        "userPassword": [password.encode('utf-8')] ,
        "shadowLastChange": [str("0").encode('utf-8')],
        "shadowMax": [str("0").encode('utf-8')],
        "shadowWarning": [str("0").encode('utf-8')],
        "sn": ["De Paepe".encode('utf8')],
        "givenName": ["Maarten".encode('utf8')],
        "displayName": ["Maarten De Paepe".encode('utf8')],
    }
    con.add_s(dn,ldap.modlist.addModlist(mymodlist))
    con.unbind_s()

请帮帮我。

1 个答案:

答案 0 :(得分:0)

该错误通常意味着它不喜欢您提供的某些值。以下是我注意到的一些事情,但可能并非一切:

  1. cF.Address <> FirstAddress。这些类在您的域中是否真的有效?现有的任何用户帐户都有这些类吗?用户对象的默认类通常是&#34; organizationalPerson&#34;,&#34; person&#34;,&#34; top&#34;和&#34; user&#34;。大多数情况下,您不需要自己实际设置此属性。

  2. 我没有看到您设置objectClasssAMAccountName。这些是必需的属性。