【问题标题】:Python - Adding an AD user with "userPassword" and "userAccountControl" defined returns LDAP 53 - "Unwilling to perform"Python - 添加定义了“userPassword”和“userAccountControl”的 AD 用户返回 LDAP 53 -“不愿意执行”
【发布时间】:2017-08-31 20:45:35
【问题描述】:

首先,我使用 Python 才大约 5 个月。我一直在尝试编写一个将(最终)进行批量用户创建的程序。当像下面这样格式化时,它将成功创建一个新的用户对象,但“userAccountControl”属性将默认为 546,ACCOUNTDISABLE | PASSWD_NOTREQD | NORMAL_ACCOUNT,“userPass”的值将在 AD 中对象的属性编辑器中以八位字节字符串的形式显示为纯文本。该程序正在使用 ldap3 库:https://pypi.python.org/pypi/ldap3

class fromconfig:
    def __init__(self):
        Config = configparser.ConfigParser()
        Config.read("config.ini")
        self.serverip = Config.get('serverinfo', 'ip')
        self.basepath = Config.get('serverinfo', 'base')
        self.container = Config.get('serverinfo', 'container')
        self.dc1 = Config.get('serverinfo', 'dc1')
        self.dc2 = Config.get('serverinfo', 'dc2')
        self.ou = Config.get('serverinfo', 'ou')

def add_user(username, givenname, surname, userPrincipalName, SAMAccountName, userPassword):

    ad_server = Server(config.serverip, use_ssl=True, get_info=ALL) 

    ad_c = Connection(ad_server, user='domain\\user', password='password', authentication=NTLM)

    if ad_c.bind():
        ad_c.add('cn={},cn={},dc={},dc={}'.format(username, config.ou, config.dc1, config.dc2), ['person', 'user'], {'givenName': givenname, 'sn': surname, 'userPrincipalName': userPrincipalName, 'sAMAccountName': SAMAccountName, 'userPassword': userPassword})
        print(ad_c.result)

    ad_c.unbind() 

我希望能够在程序中为 userAccountControl 定义一个 512 值,或者以其他方式成功启用该帐户,这样我就不必在以后返回并取消选中 AD 中的“帐户已禁用”。当我尝试通过它时,ad_c.result 返回错误 53。这与我通过 AD 进入并尝试直接修改属性或取消选中禁用帐户复选框时收到的错误相同。 AD 服务器上的错误 53 对话框显示“密码不符合长度或复杂性要求”,但我用于测试的密码是我过去在 AD 上使用的密码,没有任何问题。所以我认为这个问题与 userPassword 的存储方式有关,而不是复杂性或权限。

【问题讨论】:

    标签: python active-directory ldap


    【解决方案1】:

    感谢this 中的示例,我相信我已经弄明白了。当您手动启用帐户时,稍后在属性字典中定义“userPassword”键的值将不起作用。相反,在添加用户帐户后,您可以使用扩展操作解锁帐户,修改密码,然后将“userAccountControl”属性更新为所需的值(在这种情况下,我想要 512:NORMAL_ACCOUNT)。

    它非常混乱,但幸运的是它可以工作,所以我现在要开始重构程序的其余部分,让它看起来不那么可怕

    (continued from above)
        ad_c.add(...)
        ad_c.extend.microsoft.unlock_account(user='cn={},cn={},dc={},dc={}'.format(username, config.container, config.dc1, config.dc2))
        ad_c.extend.microsoft.modify_password(user='cn={},cn={},dc={},dc={}'.format(username, config.container, config.dc1, config.dc2), new_password=userpassword, old_password=None)
        changeUACattribute = {"userAccountControl": (MODIFY_REPLACE, [512])}
        ad_c.modify('cn={},cn={},dc={},dc={}'.format(username, config.container, config.dc1, config.dc2), changes=changeUACattribute)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-22
      相关资源
      最近更新 更多