UserPrincipal 不允许我添加新用户

UserPrincipal doesn't let me add new user

我在尝试添加新用户或尝试通过我的 C# 程序访问 Active Directory 中的现有用户时遇到了一些问题。

    var principalContext = new PrincipalContext(ContextType.Domain, "domain", "OU=Users,OU=SI");
    UserPrincipal usr = UserPrincipal.FindByIdentity(principalContext, IdentityType.SamAccountName, samAccountStr);

它抛出 "An operations error ocurred" 异常。我发现如果用户没有 Active Directory 的正确权限,但我是 运行 该程序的管理员,并且拥有可以将用户添加到 AD 的帐户。所以我真的不知道出了什么问题。当我尝试添加新用户时,我会这样尝试:

    var principalContext = new PrincipalContext(ContextType.Domain, "domain", "OU=Users,OU=SI");
    UserPrincipal usr = new UserPrincipal(principalContext);

当我调用 FindByIdentity 时,现有用户的代码已经中断。然而,在我尝试为新用户原则设置一些值后,新用户的代码中断了。例如:

    usr.Surname = sn;

扩展错误说是: SvcErr:DSID-031007DF,问题 5012 (DIR_ERROR)

如果不是权限问题,是否知道是什么原因造成的?

您必须为 OU 提供完整的专有名称,包括域。这是无效的:

"OU=Users,OU=SI"

类似这样的内容(如果您的域是 "domain.com"):

"OU=Users,OU=SI,DC=domain,DC=com"