在 Flask 的 Active-Directory 中创建用户不起作用

Creating User in Active-Directory in Flask dont work

我有一个 Flask Web 服务器 运行 一个站点,您可以在其中在我的 Windows Active-Directory 中创建一个用户。如果我 运行 将脚本作为一个单独的文件并手动设置变量,它就会正常工作。但是,当我尝试在 Flask 程序的脚本中使用来自网站的输入时,它不起作用。我收到以下错误代码:pywintypes.com_error: (-2147352567, 'Exception error occurred.', (0, None, None, 0, -2147221020), None)。我想知道在 Flask 脚本之外,它是否有效。当我用字符串替换所有变量时,它也不起作用。所以在我看来,我的功能与 Flask 有问题。

我的 Python 代码如下:

from flask import Flask, redirect, url_for, render_template, request
from pyad import *

app = Flask(__name__)


def replaceUmlauts(string):
    string = string.replace('ü', 'u')
    string = string.replace('ö', 'o')
    string = string.replace('ä', 'a')
    string = string.replace('Ü', 'U')
    string = string.replace('Ö', 'O')
    string = string.replace('Ä', 'A')
    return string


def createUser(firstname,lastname):
    initials = replaceUmlauts(firstname)[0:1] + replaceUmlauts(lastname)[0:1]
    loginName = replaceUmlauts(lastname)[0:4].lower() + replaceUmlauts(firstname)[0:2].lower()
    email = replaceUmlauts(firstname).lower() + "." + replaceUmlauts(lastname).lower() + "@my.domain"

    pyad.set_defaults(ldap_server="my.domain", username="Administrator", password="mypassword")
    ou = pyad.adcontainer.ADContainer.from_dn("ou=OU1, ou=OU2, ou=OU3, dc=my, dc=domain")
    new_user = pyad.aduser.ADUser.create(loginName, ou, password="Secret123", optional_attributes={
                                                     "givenName": firstname,
                                                     "sn": lastname,
                                                     "displayName": firstname + " " + lastname,
                                                     "mail": email,
                                                     "initials": initials
                                                 })
    return True


@app.route("/")
def home():
    return render_template("index.html")


@app.route("/addUser", methods=["POST", "GET"])
def addUser():
    if request.method == "POST":
        firstname = request.form["firstname"]
        lastname = request.form["lastname"]
        department = request.form["category"]
        passwort = request.form["password"]

        if len(firstname) == 0:
            return redirect(url_for("addUser", error="The first name must not be empty!"))
            exit(1)
        elif any(chr.isdigit() for chr in firstname):
            return redirect(url_for("addUser", error="The first name must not contain numbers!"))
            exit(1)
        elif len(lastname) == 0:
            return redirect(url_for("addUser", error="The last name must not be empty!"))
            exit(1)
        elif any(chr.isdigit() for chr in lastname):
            return redirect(url_for("addUser", error="The last name must not contain numbers!"))
            exit(1)
        elif len(passwort) < 6:
            return redirect(url_for("addUser", error="The password must not have less than 6 characters!"))
            exit(1)


        createUser(firstname,lastname)

    else:
        return render_template("addUser.html")


if __name__ == "__main__":
    app.run(debug=True)


我通过导入名为 pythoncom 的库修复了错误,并在我的函数中添加了初始化命令,如下所示:

import pythoncom

def createUser(firstname, lastname):
    pythoncom.CoInitialize()

是的,这是 pyadFlask

之间的问题