繁体   English   中英

Active Directoy LDAP - 锁定用户帐户

[英]Active Directoy LDAP - Lock User Account

锁定Active Directory帐户的首选方法是什么?

int val = (int)directoryentry.Properties["userAccountControl"].Value;
directoryentry.Properties["userAccountControl"].Value = val | 0x0010;

directoryentry.InvokeSet("IsAccountLocked", true); 

有没有更好的办法?

实际上,您必须执行按位操作才能将正确的位设置为适当的值。 在下面的链接中,您将遇到用户帐户控制标志。 因此,您只需对该属性执行适当的逻辑操作即可锁定或解锁该帐户。

我想,以下链接会引起您的兴趣。

如何(几乎)AD中的一切

我稍后会添加一个示例代码C#代码。

这是建议的代码:

public class AdUser {
    private int _userAccountControl
    public bool IsLocked {
        get {
            return _userAccountControl & UserAccountControls.Lock
        } set {
            if(value)
                _userAccountControl = _userAccountControl | UserAccountControls.Lock
            else
                // Must reverse all the bits in the filter when performing an And operation
                _userAccountControl = _userAccountControl & ~UserAccountControls.Lock
        }
    }
    public enum UserAccountControls {
        Lock = 0x10
    }
}

请考虑对此代码进行一些更改,因为我还没有测试过。 但是您的代码应该与锁定和解锁用户帐户相似或类似。 迟早,您必须使用DirectoryEntry.Properties []将其设置为对象类中的值。

编辑

锁定Active Directory帐户的首选方法是什么?

  int val = (int)directoryentry.Properties["userAccountControl"].Value; directoryentry.Properties["userAccountControl"].Value = val | 0x0010; 

  directoryentry.InvokeSet("IsAccountLocked", true); 

在回答你提出的问题时,我会说这些是最简单的方法,至少我知道。 就我而言,我更喜欢将这些功能包装在我的代码示例中,因为其他程序员不必关心按位操作等等。 对他们来说,他们正在操纵物体。

至于这两者之间的最佳方式,我想这主要是一个偏好问题。 如果您对逻辑操作感到放心,这些通常是首选。 相比之下,第二种选择更容易使用。

你是在.NET 3.5(或者你可以升级到它)?

如果是这样,请查看新的System.DirectoryServices.AccountManagement命名空间及其提供的所有内容! 优秀的介绍是MSDN文章管理.NET Framework 3.5中的目录安全主体

对于您的情况,您必须以某种方式获得UserPrincipal ,例如

PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "YOURDOMAIN");
UserPrincipal me = UserPrincipal.Current;

然后你可以访问过多的非常容易使用的属性和方法 - 例如:

bool isLockedOut = me.IsAccountLockedOut();

您可以使用以下方法解锁锁定的帐户:

me.UnlockAccount();

远远高于普通的老容易System.DirectoryServices的东西!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM