2010-06-14 76 views

回答

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中,用户和组的大部分日常操作都得到了大幅改善 - 使用这些新功能!

+0

这就是我一直在寻找的!谢谢 ! – abmv 2010-06-15 06:16:24

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); 
} 

无论如何这篇帮助?

+0

@David Neale:它是如何被盗的?我参考了我得到的信息。另外,那里总是有MSDN。最近我一直在使用Active Directory,并且仍然使用Active Directory。我不明白你在说什么。 – 2010-06-14 13:27:11

+0

最初是因为你的答案已经被编辑过,然后直接与我的相似。你现在已经花时间编写了一个很好的综合答案,所以我收回它。 :) – 2010-06-14 14:13:47

+0

@David Neale:谢谢你的这种解释。我这样做是因为当输入整个答案时,OP得到他的答案太长了。然后我用一个简短但直接的答案进来,然后我编辑它以带来更全面的细节等等。许多人这样做,所以如果你明白我的意思,他们会保持“第一答案”的等级。无论如何,你有一个非常好的答案,我会upvote。 =) – 2010-06-14 14:43:11

2

这里有一个很好的链接,广告运营Howto: (Almost) Everything In Active Directory via C#

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

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

+0

+1感谢您对我的回答发表评论时给予的解释。我们都有一个很好的答案。 =) – 2010-06-14 14:44:19

相关问题