2012-07-06 61 views
1

我一直在經歷前一篇文章,試圖解決這個問題整個上午,但沒有一個似乎工作。操作錯誤發生枚舉DirectoryEntry組

我一直在爲我們的課程管理員修改AD的用戶管理界面,這個想法是隻顯示他們需要的東西,而解決方案在開發服務器上工作正常,我在產品上得到上述錯誤。

我嘗試了所有可以找到的東西,例如HostingEnvironment.Impersonate,將服務帳戶提升爲域管理員,但注意到了作品。

public static List<GroupPrincipal> GetGroups(string client) 
{ 
     List<GroupPrincipal> List = new List<GroupPrincipal>(); 

     DirectoryEntry ou = null; 
     GroupPrincipal group = null; 
     PrincipalContext context = null; 

     if (domain.Path.ToLower().Contains(DevDN.ToLower())) 
     { 
      context = new PrincipalContext(ContextType.Domain, 
       DevDom, 
       DevDN, 
       DevService, 
       DevServicePass); 
     } 
     else 
     { 
      context = new PrincipalContext(
       ContextType.Domain, 
       LiveDom, 
       LiveDN, 
       LiveService, 
       LiveServicePass); 
     } 

     DirectorySearcher searcher = new DirectorySearcher(domain, "(&(ou=" + client + ")(objectClass=organizationalUnit))"); 
     try 
     { 
      ou = new DirectoryEntry(searcher.FindOne().Path); 
     } 
     catch (System.Exception ex) 
     { 
      Log.WriteError("SUGM.ADLink.GetGroups", "Unable to locate client: " + ex.Message); 
      List = null; 
      return List; 
     } 
     try 
     { 
      foreach (DirectoryEntry groups in ou.Children) 
      { 
       if (groups.SchemaClassName == "group") 
       { 
        string name = groups.Name.Replace("CN=", ""); 
        group = GroupPrincipal.FindByIdentity(context, name); 
        List.Add(group); 
       } 
      } 
     } 
     catch (System.Exception ex) 
     { 
      Log.WriteError("SUGM.ADLink.GetGroups", "Unable to add groups to list: " + ex.Message); 
      List = null; 
      return List; 
     } 

     return List; 
    } 

在調試過程中,我檢查並傳遞了所有正確的值,但它總是在foreach塊上失敗。

任何人都可以指出我做錯了什麼。

乾杯

+0

爲什麼要從System.DirectoryServices.AccountManagement(.NET 3.5)命名空間中將'PrincipalContext'和'GroupPrincipal'與'System.DirectoryServices'命名空間中的'DirectoryEntry'和'DirectorySearcher'(.NET 2.0)混合使用??真的沒有道理,並且不會很好......請解釋你想要做的第一件事 – 2012-07-06 12:32:27

回答

0

您應該避免混合System.DirectoryServicesSystem.DirectoryServices.AccountManagement命名空間 - 這不是一個很好的策略!

你也可以在S.DS.AM(.NET 3.5)中做所有你想要的!而且更容易。

您可以使用PrincipalSearcher和「查詢通過例如」主要做你的搜索:

// create your domain context and specify the initial container to work from 
PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "YOURDOMAIN", "OU=YourStartingPoint,DC=YourCompany,DC=com"); 

// define a "query-by-example" principal - here, we search for a GroupPrincipal 
GroupPrincipal qbeGroup = new GroupPrincipal(ctx); 

// create your principal searcher passing in the QBE principal  
PrincipalSearcher srch = new PrincipalSearcher(qbeGroup); 

// find all matches 
foreach(var found in srch.FindAll()) 
{ 
    // do whatever here - "found" is of type "Principal" - it could be user, group, computer.....   
} 

如果您尚未 - 絕對看MSDN文章Managing Directory Security Principals in the .NET Framework 3.5這很好地說明如何使System.DirectoryServices.AccountManagement中的新功能的最佳使用。或者查看MSDN documentation on the System.DirectoryServices.AccountManagement命名空間。

+0

Woops甚至沒有注意到,我多麼調皮。您的解決方案非常感謝 – CodedBeard 2012-07-09 11:17:08