2011-06-24 26 views
6

當爲用戶查詢Active Directory時 - 是否有辦法過濾出爲計算機創建的用戶帳戶?理想情況下,這是大多數典型網絡中常見的一種方式。例如: -任何方式來區分「人員用戶帳戶」和「計算機用戶帳戶」?

DirectorySearcher ds = new DirectorySearcher(new DirectoryEntry([Users_OU_root]));  
ds.filter = "(&(objectClass=User)([CRITERIA_TO_FILTER_OUT_COMPUTER_USER_ACCOUNTS]))";  
ds.FindAll();  
... 

回答

6

如果你在.NET 3.5及以上,你應該看看System.DirectoryServices.AccountManagement(S.DS.AM)命名空間。在這裏閱讀全部內容:

基本上,你可以定義域範圍內,並可以輕鬆地查找用戶和/或組AD:

// set up domain context 
PrincipalContext ctx = new PrincipalContext(ContextType.Domain); 

// find a user 
UserPrincipal user = UserPrincipal.FindByIdentity(ctx, "SomeUserName"); 

if(user != null) 
{ 
    // do something here....  
} 

// find the group in question 
GroupPrincipal group = GroupPrincipal.FindByIdentity(ctx, "YourGroupNameHere"); 

// if found.... 
if (group != null) 
{ 
    // iterate over members 
    foreach (Principal p in group.GetMembers()) 
    { 
     Console.WriteLine("{0}: {1}", p.StructuralObjectClass, p.DisplayName); 
     // do whatever you need to do to those members 
    } 
} 

的新的S.DS.AM可以很方便地與AD中的用戶和羣組玩轉:

計算機帳戶將顯示爲ComputerPrincipal(源自Principal) - 因此您可以輕鬆地將用戶和計算機帳戶分開。

如果您不能或不想移動到S.DS.AM--您還可以通過在您的LDAP過濾器中使用objectCategory而不是objectClass來分開用戶和計算機。無論如何,objectCategory是有益的,因爲它被索引,而不是多值 - 所以查詢性能會好很多。

對於真實的用戶,請使用objectCategory = Person,而對於計算機,請在您的LDAP過濾器中使用objectCategory = Computer

3

即使我同意答案。 Active-Directory保持爲LDAP服務器。這裏是你正在尋找的過濾器:

(&(objectCategory=user)(objectClass=user)(...)) 

objectCategory=user」是由活動目錄瞭解「objectCategory=CN=User,CN=Schema,CN=Configuration,DC=dom,DC=fr」的快捷方式,但它也是在其他目錄的方式,這就是爲什麼我把一個答案,即使再答案被接受。