2013-08-26 71 views
0

這給我們的ActiveDirectory UserPrincipals的列表,用戶在組「X」:過濾器用戶可以通過屬性

var domainContext = new PrincipalContext(ContextType.Domain); 
var groupPrincipal = GroupPrincipal.FindByIdentity(domainContext, IdentityType.SamAccountName, "x"); 

現在我將如何通過自定義屬性篩選該列表中的用戶?所有用戶都在自定義屬性「Building」中有條目,並且我希望列表僅包含來自某個建築物的用戶。

SOLUTION

愚蠢的我...投的成員groupPrincipal到的DirectoryEntry,然後訪問屬性..

 foreach (var member in groupPrincipal.Members) 
     { 
      // maybe some try-catch .. 
      System.DirectoryServices.DirectoryEntry i = (System.DirectoryServices.DirectoryEntry)member.GetUnderlyingObject(); 
      if (i.Properties["building"].Value.toString() == "NSA HQ") 
      { 
       // Do stuff here 
      } 

     } 
+0

http://stackoverflow.com/questions/7151770/retrieve-ad-custom-attribute-in-one-batch可能會有所幫助。 – mswietlicki

+0

謝謝,我編輯了我的問題。作爲回答發佈,我會投票。 – peter

回答

1

是的,你可以使用member.GetUnderlyingObject()

var members = groupPrincipal.Members.Where(member=>(member.GetUnderlyingObject() as DirectoryEntry).Properties["building"].Value.ToString() == "NSA HQ"); 

如在Retrieve AD Custom Attribute in One Batch中指出的那樣