Can't get all groups a user is member of.. RRS feed

  • Question

  • Hi,

    I'm trying to get the user's groups in Active Directory.

    When passing user which is in Administrator group, it works fine.

    But when passing a regular user, I get a System.DirectoryServices.AccountManagement.PrincipalOperationException.

    whith the message:

    An error (1301) occurred while enumerating the groups.  The group's SID could not be resolved.

    public IEnumerable<string> GetGroupNames(string user, string domain)
                var pc = new PrincipalContext(ContextType.Domain, domain);
                var src = UserPrincipal.FindByIdentity(pc, user);
                var groups = src.GetAuthorizationGroups();
                var result = new List<string>();
                groups.ToList().ForEach(sr => result.Add(sr.SamAccountName));
                return result;

    Any Ideas?


    Tuesday, June 25, 2013 1:54 PM


All replies

  • Hi Shlomi,

    Welcome to the MSDN Forum.

    How about this one:  

    Best regards,

    Mike Feng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, June 26, 2013 2:39 PM
  •  PrincipalContext ctx = new PrincipalContext(ContextType.Domain);

                // find the group in question
                GroupPrincipal group = GroupPrincipal.FindByIdentity(ctx, "GroupName");

                // if found....
                if (group != null)
                                  // iterate over members
                    foreach (Principal p in group.GetMembers(true))
                        Console.WriteLine("{0}: {1}", p.StructuralObjectClass, p.DisplayName);

                        // do whatever you need to do to those members
                        UserPrincipal theUser = p as UserPrincipal;

                        if (theUser != null && (theUser is UserPrincipal))
                            using (StreamWriter sr = new StreamWriter(@"FileName", true))
                                sr.WriteLine(string.Format("FirstName ={0} , LastName = {1} , Email = {2}", theUser.GivenName, theUser.Surname, theUser.EmailAddress));


    Tazeem Ansari

    Tuesday, April 12, 2016 4:02 PM