Active Directory是否需要用户停用代码片段?

时间:2010-06-14 13:05:48

标签: c# active-directory directoryservices directoryentry

是否可以发布一些信息来了解特定用户是否是Windows广告中已停用的用户?

3 个答案:

答案 0 :(得分:3)

如果您使用的是.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,大多数用户和组的日常操作都得到了极大的改进 - 使用这些新功能!

答案 1 :(得分:2)

您需要查询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);
}

无论如何这有帮助吗?

答案 2 :(得分:2)

以下是AD操作Howto: (Almost) Everything In Active Directory via C#

的良好链接

你需要查询userAccountControl属性,它是一个按位标志,我相信它对于一个禁用的帐户是514,但是这些值是累积的,所以你需要解决它。 (NORMAL ACCOUNT + ACCOUNT DISABLED = 512 + 2 = 514)

以下是所有User Account Control flags的参考资料。