Question

I have a little code to delete a security group from Active Directory, but when run, I get a COMException with the message "Unspecified error".

Here is the code:

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();
    }
}

And here's the message in the Windows Event Console:

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:
Was it helpful?

Solution

Based on the questions/answer in the comments section I revise my answer. I think you simply forgot the LDAP protocol identifier for the LDAP path of your organization unit. I think, unspecified error means invalid LDAP path.

Try the following code:

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(); 
    } 
  }
} 

By the same token, ensure that the LDAP protocol identifier is written using capital letters. Hope, this helps.

OTHER TIPS

Just guessing: Maybe the DirectoryEntry "ou" is not empty. MSDN says:

If the entry to be removed is a container, the container must be empty. To delete a container and all its children, use the DeleteTree method.

You also may try to catch the ComException and gather more information so the problem can be analysed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top