2010-06-14 73 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

相關問題