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

本文关键字:错误 未指定 Children Remove 抛出 DirectoryEntry | 更新日期: 2023-09-27 18:06:39

我有一个小代码从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:

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

根据评论部分的问题/答案,我修改了我的答案。我认为您只是忘记了组织单元的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协议标识符使用大写字母。

只是猜测:也许DirectoryEntry "ou"不是空的。MSDN说:

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

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