2010-12-23 32 views
17

que estoy tratando de conseguir todos los grupos de Active Directory para un usuario, con el siguiente código:UserPrincipal.GetGroups falla con error desconocido

private static IEnumerable<string> GetGroupNames(string userName) 
    { 
     using (var context = new PrincipalContext(ContextType.Domain)) 
     { 
      using (var userPrincipal = UserPrincipal.FindByIdentity(context, userName)) 
      { 
       var groupSearch = userPrincipal.GetGroups(context); 
       var result = new List<string>(); 
       foreach (var principal in groupSearch) 
       { 
        Log.LogDebug("User {0} is member of group {0}", userPrincipal.DisplayName, principal.DisplayName); 
        result.Add(principal.SamAccountName); 
       } 
       return result; 
      } 
     } 
    } 

Este código se encuentra correctamente el usuario principal, pero falla cuando se llama con GetGroups a PrincipalOperationException: Error desconocido (0x80005000).

excepción Raíz:

at System.DirectoryServices.AccountManagement.ADStoreCtx.GetGroupsMemberOf(Principal foreignPrincipal, StoreCtx foreignContext) 
    at System.DirectoryServices.AccountManagement.Principal.GetGroupsHelper(PrincipalContext contextToQuery) 
    at System.DirectoryServices.AccountManagement.Principal.GetGroups(PrincipalContext contextToQuery) 
    at [line of the GetGroup call] 

excepción interior (COMException):

at System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail) 
    at System.DirectoryServices.DirectoryEntry.Bind() 
    at System.DirectoryServices.DirectoryEntry.get_AdsObject() 
    at System.DirectoryServices.PropertyValueCollection.PopulateList() 
    at System.DirectoryServices.PropertyValueCollection..ctor(DirectoryEntry entry, String propertyName) 
    at System.DirectoryServices.PropertyCollection.get_Item(String propertyName) 
    at System.DirectoryServices.AccountManagement.ADUtils.RetriveWkDn(DirectoryEntry deBase, String defaultNamingContext, String serverN 

Another report with this problem.

¿Alguna pista?

+1

¿Sucede a todos los usuarios? ¿O sucede solo en un usuario en particular? Sé que hay un error en la biblioteca .NET que arroja esta COMException cuando el DN del usuario contiene "/". También tengo una solución a este problema si confirma que esto solo ocurre en el usuario con DN que contiene "/" solo –

+0

Tengo el problema que describe. Tengo el problema de obtener grupos cuando el DN del usuario contiene "/". ¿Me puede decir cuál es la solución que usa, por favor? – Lamelas84

+0

Tengo el mismo problema para los usuarios que contienen "/" en DN. ¿Cuál fue la solución? – bahramzy

Respuesta

29

Adición Environment.UserDomainName como el nombre de argumento a la PrincipalContext ayudaron:

using (var context = new PrincipalContext(ContextType.Domain, Environment.UserDomainName)) 

Todavía no sé por qué PrincipalContext (ContextType.Domain) sólo funciona para encontrar el UserPrincipal y no los grupos de usuario . El mensaje de error COM "error desconocido" no es muy útil y el constructor PrincipalContext sobrecarga con solo el ContextType prácticamente no está documentado en MSDN. Huele como un problema con el framework .NET como lo señala Harvey Kwok.

+0

Tenía exactamente el mismo resultado de la llamada 'UserPrincipal.IsMemberOf (GroupPrincipal)' cuando el usuario actual no es miembro. De hecho, el uso del constructor 'PrincipalContext (ContextType, string)' resuelve el problema interno. – Astrogator

+0

También tenía este problema exacto. Arrojaría aleatoriamente 'DirectoryServicesCOMException' con 'HRESULT 0x80072030' o convertiría de' uint' a 'int', es' .ErrorCode == -2147016656'. Sucedió solo en producción pero no en máquinas virtuales de desarrollo con conexión lenta a AD. El uso de 'PrincipalContext (ContextType, string)' .ctor parece haber resuelto finalmente el problema. –

Cuestiones relacionadas