2013-01-17 239 views
8

我正在使用System.DirectoryServices.AccountManagement.dll來處理Active Directory 以獲取「域用戶」組中的所有用戶。從Active Directory獲取已啓用帳戶

這是返回域中的所有用戶,但我只需要獲取啓用的用戶。

下面是一些示例代碼:

List<string> users = new List<string>(); 

PrincipalContext pcContext = GetPrincipalContext(); 

GroupPrincipal grp = GroupPrincipal.FindByIdentity(pcContext, 
           IdentityType.Name, 
           "Domain Users"); 

foreach (Principal user in grp.GetMembers(true).OfType<UserPrincipal>()) 
{ 
    if (user.Enabled != false) 
    { 
     users.Add(user.Name); 
    } 
} 

其他組做工精細,但當組是「域用戶」,該Enabled屬性的值是false於所有用戶。這使得無法區分啓用和禁用的用戶而無需爲每個用戶進行進一步的查詢。

+0

你只需要帳號名嗎?是否需要使用AccountManagement命名空間?除了存儲名稱之外,您是否對UserPrincipal對象執行任何操作?我只問,因爲您的需求可能會更好地使用DirectoryServices命名空間和DirectorySearcher與LDAP過濾器如下所示:(&(objectclass = user)(memberOf = CN = Domain Users,dc = company,dc = com) (userAccountControl:1.2.840.113556.1.4.803:= 2)))這將返回該組中啓用的所有用戶。 – randcd

+1

randcd:我寧願使用AM命名空間,因爲它提供了一個非常乾淨的API。我當然可以使用LDAP,但是一切都變得更加複雜(特別是在用戶只是域用戶的成員的情況下,因爲這是他們的主要組,因爲它沒有在'memberOf'中列出)。 – RobSiklos

+0

ForEach循環中的「Principal」實際上應該是「UserPrincipal」 - 「Enabled」不是可在Princpal對象上訪問的方法或屬性。至少不在.net 4.6.1中。這可以通過System.DirectoryServices.AccountManagement命名空間完成。 – DtechNet

回答

1

UserPrinciple對象爲此具有bool Enabled屬性。

http://msdn.microsoft.com/en-us/library/system.directoryservices.accountmanagement.userprincipal_properties.aspx

// Add this to GetUserDetails 
objUserDetails.EmployeeId = UserPrinical.EmployeeId; 


// Then condition the add to only add enabled 
if (objUserDetails.Enabled) { 
    objUserDetails.Add(GetUserDetails(p.Name)); 
} 
+0

這並不能解決問題,因爲當被查詢的組是「域用戶」時,「Enabled」屬性似乎沒有被正確填充。 – RobSiklos

0

有一個在MSDN page of the Enabled property了一句話說:

如果委託人沒有在店裏堅持,這個屬性返回null。在委託人被保留後,默認的啓用設置取決於商店。 AD DS和AD LDS存儲會在新主體持續存在時禁用新主體,而SAM會在新主體持續存在時啓用新主體。應用程序只能將該屬性設置爲在商店中保存後的值。

也許這是相關的,如果默認爲false?

此外,MSDN論壇上有一篇關於UserPrincipal.Enabled returns False for accounts that are in fact enabled?的帖子,這聽起來與您的問題非常相似。根據這個帖子,這裏可能有一個解決辦法:

我想我誤解了。無視我之前發佈的內容。我想我 知道發生了什麼。 GetMembers方法顯然不是加載 UserPrincipal數據。我不知道是否有更好的解決方案,但 以下工作(至少在我的AD):

foreach (UserPrincipal user in group.GetMembers(false)) 
{ 
    UserPrincipal tempUser = UserPrincipal.FindByIdentity(context, user.SamAccountName); 
    // use tempUser.Enabled 
    // other code here 
} 
+0

用戶已被保留,因此您答案的第一部分與這個問題。至於其他方面,是的 - 我知道我可以做到這一點 - 這個問題表明,我們希望對Enabled屬性有一個正確的值,而不必做任何額外的查詢(特別是不是每個用戶都有一個像這樣的foreach循環)。你從甚至引用的帖子說這個解決方案太慢而無用。 – RobSiklos

1

解決此問題的方法可能是使用PrincipalSearcher使用戶第一次搜索然後用信安的方法IsMemberOf()

List<string> users = List<string>(); 
PrincipalContext pcContext = GetPrincipalContext(); 
GroupPrincipal grp = GroupPrincipal.FindByIdentity(pcContext, IdentityType.Name, "Domain Users"); 
UserPrincipal searchFilter = new UserPrincipal(pcContext){ Enabled = true } 
PrincipalSearcher searcher = new PrincipalSearcher(searchFilter); 
PrincipalSearchResult<Principal> results = searcher.FindAll(); 
foreach (Principal user in results) 
    if (user.IsMemberOf(grp)) 
     users.Add(user.SamAccountName); 
相關問題