DirectoryEntry.Children.Remove抛出“未指定的错误”

时间:2011-08-23 18:35:07

标签: c# active-directory directoryentry

我有一些代码可以从Active Directory中删除安全组,但是在运行时,我收到一条带有“未指定错误”消息的COMException。

以下是代码:

public void DeleteGroup(Model.Asset pADSecurityGroup)
{
    using(DirectoryEntry ou = new DirectoryEntry(pADSecurityGroup.Organization.ActiveDirectoryMappings.Single().Identifier))
    using(DirectoryEntry group = new DirectoryEntry("LDAP://" + pADSecurityGroup.ActiveDirectoryMappings.Single().Identifier))
    {
        ou.Children.Remove(group);
        group.CommitChanges();
    }
}

这是Windows事件控制台中的消息:

Event code: 3005 
Event message: An unhandled exception has occurred.
Event time: 8/23/2011 11:29:35 AM  
Event time (UTC): 8/23/2011 5:29:35 PM  
Event ID: 67e6356c9ff146c7a0d9024350cbb3a0  
Event sequence: 79  
Event occurrence: 1  
Event detail code: 0

Application information: 
    Application domain: /LM/W3SVC/1/ROOT-2-129585938920392018 
    Trust level: Full 
    Application Virtual Path: / 
    Application Path: C:\inetpub\wwwroot\vo\Web\Portal\ 
    Machine name: TR-2K8-001    Process information: 
    Process ID: 8348 
    Process name: w3wp.exe 
    Account name: VO\treed    Exception information: 
    Exception type: COMException 
    Exception message: Unspecified error

   at System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail)
   at System.DirectoryServices.DirectoryEntry.Bind()
   at System.DirectoryServices.DirectoryEntry.get_IsContainer()
   at System.DirectoryServices.DirectoryEntries.Remove(DirectoryEntry entry)
   at VirtualOffice.DirectoryServices.Impl.DirectoryService.DeleteGroup(ResourcePool pResourcePool) in C:\inetpub\wwwroot\vo\Common Libraries\VirtualOffice.DirectoryServices\Impl\DirectoryService.cs:line 249
   at VirtualOffice.Controllers.ResourcePoolController.Delete(Int32 pServiceProviderId) in C:\inetpub\wwwroot\vo\Common Libraries\VirtualOffice.Controllers\ResourcePoolController.cs:line 171
   at Organization_ResourcePools.rtbResourcePools_OnButtonClick(Object sender, RadToolBarEventArgs e) in c:\inetpub\wwwroot\vo\Web\Portal\Organization\ResourcePools.aspx.cs:line 85
   at Telerik.Web.UI.RadToolBar.OnButtonClick(RadToolBarEventArgs e)
   at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

    Request information: 
    Request URL: https://localhost:443/Organization/ResourcePools.aspx

    Request path: /Organization/ResourcePools.aspx 
    User host address: ::1 
    User: Portal Admin 
    Is authenticated: True 
    Authentication Type: Federation 
    Thread account name: VO\treed    Thread information: 
    Thread ID: 5 
    Thread account name: VO\treed 
    Is impersonating: False 
    Stack trace:

   at System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail)
   at System.DirectoryServices.DirectoryEntry.Bind()
   at System.DirectoryServices.DirectoryEntry.get_IsContainer()
   at System.DirectoryServices.DirectoryEntries.Remove(DirectoryEntry entry)
   at VirtualOffice.DirectoryServices.Impl.DirectoryService.DeleteGroup(ResourcePool pResourcePool) in C:\inetpub\wwwroot\vo\Common Libraries\VirtualOffice.DirectoryServices\Impl\DirectoryService.cs:line 249
   at VirtualOffice.Controllers.ResourcePoolController.Delete(Int32 pServiceProviderId) in C:\inetpub\wwwroot\vo\Common Libraries\VirtualOffice.Controllers\ResourcePoolController.cs:line 171
   at Organization_ResourcePools.rtbResourcePools_OnButtonClick(Object sender, RadToolBarEventArgs e) in c:\inetpub\wwwroot\vo\Web\Portal\Organization\ResourcePools.aspx.cs:line 85
   at Telerik.Web.UI.RadToolBar.OnButtonClick(RadToolBarEventArgs e)
   at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

    Custom event details:

2 个答案:

答案 0 :(得分:2)

根据评论部分中的问题/答案,我修改了我的答案。 我想您只是忘记了组织单位的LDAP路径的LDAP协议标识符。我认为,未指定的错误意味着无效的LDAP路径。

请尝试以下代码:

public void DeleteGroup(Model.Asset pAsset) 
{ 
  using(DirectoryEntry ou = new DirectoryEntry("LDAP://" + pResourcePool.Organization.ActiveDirectoryMappings.Single().Identifier)) 
  {
    using(DirectoryEntry group = new DirectoryEntry("LDAP://" + pResourcePool.ActiveDirectoryMappings.Single().Identifier), username, userpwd) 
    { 
    ou.Children.Remove(group); 
    group.CommitChanges(); 
    } 
  }
} 

出于同样的原因,请确保使用大写字母书写LDAP协议标识符。 希望,这有帮助。

答案 1 :(得分:0)

猜测:也许DirectoryEntry“ou”不是空的。 MSDN说:

  

如果要删除的条目是容器,则容器必须为空。要删除容器及其所有子容器,请使用DeleteTree方法。

您也可以尝试捕获ComException并收集更多信息,以便分析问题。