【问题标题】:Changing AD User Account Property by using the UserPrincipal使用 UserPrincipal 更改 AD 用户帐户属性
【发布时间】:2012-05-05 05:45:03
【问题描述】:

我正在尝试使用 UserPrincipal 更改 Active Directory 中的用户帐户属性。

我了解到我们必须使用对 Active Directory 具有写入权限的特殊帐户,而不是当前登录用户。所以,我创建了一个特殊的类来使用特殊帐户来模拟。但我仍然有

System.UnauthorizedAccessException: General access denied error

在 user.Save(ctx);行。

System.Security.Principal.WindowsImpersonationContext newUser = clsImpersonate.ImpersonateUser("ADUser", "ADPassword");

            if (newUser != null)
            {
                PrincipalContext ctx = blAD.GetAdminPrincipalContext();
                UserPrincipal user = blAD.GetUserPrincipal(this.SAMAccount);
                user.Enabled = false;
                user.Save(ctx);
                newUser.Undo();
            }

我怎样才能达到这个要求?谢谢。

【问题讨论】:

    标签: c# active-directory impersonation userprincipal


    【解决方案1】:

    要以其他用户身份访问 Principal,请使用用户的凭据定义您的 PrincipalContext,并在获取 UserPrincipal 时使用该 PrincipalContext。

        PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "domain.tld", "ADUser", "ADPassword");
        UserPrincipal user = UserPrincipal.FindByIdentity(ctx, IdentityType.SamAccountName, this.SAMAccount);
        if (user != null) 
        {
            user.Enabled = false;
            user.Save();
        }
    

    如果您仍然收到 UnauthorizedAccess 异常,可能是因为您指定的帐户无权在 Active Directory/LDS 中的用户对象上写入 userAccountControl 属性。

    【讨论】:

    • 如何知道the account you are specifying does not have access to write the userAccountControl attribute on the user object in Active Directory/LDS.
    【解决方案2】:

    我不会先冒充帐户!首先通过广告传递值来获得访问权限。

    真正的问题,看错误:

    1. 获取 principalContect。
    2. 获取 userprincipal。
    3. 做你想做的事。
    4. 保存它,你为什么要使用撤消?删除 Undo()。

    【讨论】:

      【解决方案3】:

      哪些权限已委派给您的特殊用户?它需要能够在相关用户上写userAccountControl

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-01-29
        • 2019-12-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多