【问题标题】:Active Directory Is User Deactivated Code Snippet Needed?Active Directory 是否需要用户停用的代码片段?
【发布时间】:2010-06-14 13:05:47
【问题描述】:

有人可以在 windows 广告中发布知道特定用户是否是停用用户的方法吗?

【问题讨论】:

  • +1 在整个 SO 中找到与 Active Directory 相关的问题总是很有趣。 =)

标签: c# active-directory directoryservices directoryentry


【解决方案1】:

如果您使用 .NET 3.5 或可以升级到 .NET 3.5 - 请查看新的 System.DirectoryServices.AccountManagement 命名空间,它使许多这些操作变得轻而易举。有关介绍,请参阅 Managing Directory Security Principals in the .NET Framework 3.5

在您的情况下,您可以编写如下代码:

PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "YOURDOMAIN")

UserPrincipal user = UserPrincipal.FindByIdentity("somename");

bool locked = user.IsAccountLockedOut();

就是这样! .NET 3.5 极大地改进了用户和组的大多数日常操作 - 使用这些新功能!

【讨论】:

  • 这就是我想要的!谢谢!
【解决方案2】:

这是一个很好的 AD 操作链接Howto: (Almost) Everything In Active Directory via C#

您需要查询 userAccountControl 属性,它是一个按位标志,我相信它是禁用帐户的 514,但这些值是累积的,因此您需要计算出来。 (NORMAL ACCOUNT + ACCOUNT DISABLED = 512 + 2 = 514).

这是所有User Account Control flags 的参考。

【讨论】:

  • +1 感谢您在对我的回答的评论中给出的友好解释。我们都有一个很好的答案。 =)
【解决方案3】:

您需要查询userAccountControl属性。

userAccountControl 标志的值是:

    CONST   HEX
    -------------------------------
    SCRIPT 0x0001
    ACCOUNTDISABLE 0x0002
    HOMEDIR_REQUIRED 0x0008
    LOCKOUT 0x0010
    PASSWD_NOTREQD 0x0020
    PASSWD_CANT_CHANGE 0x0040
    ENCRYPTED_TEXT_PWD_ALLOWED 0x0080
    TEMP_DUPLICATE_ACCOUNT 0x0100
    NORMAL_ACCOUNT 0x0200
    INTERDOMAIN_TRUST_ACCOUNT 0x0800
    WORKSTATION_TRUST_ACCOUNT 0x1000
    SERVER_TRUST_ACCOUNT 0x2000
    DONT_EXPIRE_PASSWORD 0x10000
    MNS_LOGON_ACCOUNT 0x20000
    SMARTCARD_REQUIRED 0x40000
    TRUSTED_FOR_DELEGATION 0x80000
    NOT_DELEGATED 0x100000
    USE_DES_KEY_ONLY 0x200000
    DONT_REQ_PREAUTH 0x400000
    PASSWORD_EXPIRED 0x800000
    TRUSTED_TO_AUTH_FOR_DELEGATION 0x1000000

您需要使用 System.DirectoryServices 命名空间并使用 DirectorySearcher 类来查询 Active Directory,然后验证 userAccountControl 标志属性。

我想你应该参考的一个好页面如下:

How to (almost) everything in Active Directory in C#.

在与userAccountControl 标志属性进行比较时,您必须按位进行,如下所示:

using (DirectorySearcher searcher = new DirectorySearcher()) {
    searcher.SearchRoot = new DirectoryEntry(rootDSE); // Where rootDSE is a string which contains your LDAP path to your domain.
    searcher.SearchScope = SearchScope.Subtree;
    searcher.Filter = string.Format("(&(objectClass=user)(sAMAccountName={0}))", userName);

    SearchResult result = null;

    try {
        result = searcher.FindOne();
    } catch (Exception) {
        // You know what to do here... =P
    }

    if (result == null)
        return;

    DirectoryEntry user = result.GetDirectoryEntry();

    bool isAccountDisabled = ((user.Properties("userAccountControl").Value & ACCOUNTDISABLE) == ACCOUNTDISABLE);
}

这有帮助吗?

【讨论】:

  • @David Neale:它是如何被盗的?我参考了我从哪里得到的信息。另外,那里总是有 MSDN。我最近一直在使用 Active Directory,现在仍在使用。我不明白你在说什么。
  • 最初是因为您的答案已经过编辑,然后直接与我的相似。您现在已经花时间编写了一个很好的综合答案,所以我收回它。 :)
  • @David Neale:感谢您的解释。我这样做是因为在输入整个答案时,OP 得到他的答案太长了。然后我提供一个简短但直接的答案,然后我对其进行编辑以提供更全面的细节等。很多人这样做,以便他们保持“第一个回答”的排名,如果你明白我的意思的话。无论如何,你得到了一个非常好的答案,我会赞成。 =)
猜你喜欢
  • 2012-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多