2013-08-01 74 views
0

我在.NET 4.5中使用System.DirectoryServices.AccountManagement。Active Directory相關的搜索器組來自AccountManagement.Principal的屬性

我想從下面的代碼中獲取Active Directory中的所有組和屬性。但我有額外的屬性違抗作爲鍵值System.DirectoryServices.SearchResult,

我怎樣才能得到System.DirectoryServices.SearchResult下的所有屬性從System.DirectoryServices.AccountManagement.Principal

// create your domain context 
var ctx = new PrincipalContext(ContextType.Domain, _domain, _userName, _password); 

// define a "query-by-example" principal - here, we search for a GroupPrincipal 
var groupPrincipal = new GroupPrincipal(ctx); 
// create your principal searcher passing in the QBE principal  
var searcher = new PrincipalSearcher(groupPrincipal); 
foreach (var found in searcher.FindAll()) 
{ 
} 

回答

0

我已經用下面的擴展方法解決了這個問題,但是建議我是否有任何有效的方法來完成同樣的工作。

public static class AccountManagementExtensions 
    { 
     public static String GetProperty(this Principal principal, String property) 
     { 
      var directoryEntry = principal.GetUnderlyingObject() as DirectoryEntry; 
      return directoryEntry != null && directoryEntry.Properties.Contains(property) 
         ? directoryEntry.Properties[property].Value.ToString() 
         : String.Empty; 
     } 

     public static String GetCompany(this Principal principal) 
     { 
      return principal.GetProperty("company"); 
     } 

     public static String GetDepartment(this Principal principal) 
     { 
      return principal.GetProperty("department"); 
     } 
    } 
相關問題