我一直在經歷前一篇文章,試圖解決這個問題整個上午,但沒有一個似乎工作。操作錯誤發生枚舉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塊上失敗。
任何人都可以指出我做錯了什麼。
乾杯
爲什麼要從System.DirectoryServices.AccountManagement(.NET 3.5)命名空間中將'PrincipalContext'和'GroupPrincipal'與'System.DirectoryServices'命名空間中的'DirectoryEntry'和'DirectorySearcher'(.NET 2.0)混合使用??真的沒有道理,並且不會很好......請解釋你想要做的第一件事 – 2012-07-06 12:32:27