active directory group.members.contains crashes on "Users" with unknown error

static void Main(string[] args)
    var groupNames = args;

    var principalContext = new PrincipalContext(ContextType.Domain);

    var user = UserPrincipal.FindByIdentity(principalContext, Environment.UserName);

    var groupPrincipals = new List<GroupPrincipal>();

    groupPrincipals.AddRange(groupNames.Select(name => GroupPrincipal.FindByIdentity(principalContext, IdentityType.Name, name)));
    groupPrincipals = groupPrincipals.Where(gp => gp != null).ToList();

    Console.WriteLine(groupPrincipals.Any(gp => gp.Members.Contains(user)));


When I'm trying to run this code it crashes on

Console.WriteLine(groupPrincipals.Any(gp => gp.Members.Contains(user)));

With Exception :

Unknown error (0x80005000)

I'm trying to determine whether I'm a member of group "Users".Debug view shows me that "Users" contains 3 items, but membership check crashes

asked on Stack Overflow Feb 15, 2011 by illegal-immigrant • edited Dec 7, 2015 by Josh Crozier

1 Answer


So, I found out that it's a Access denied error code. After rewriting this code (getting user's groups and then check whether they contain specified group) it works

answered on Stack Overflow Feb 17, 2011 by illegal-immigrant

User contributions licensed under CC BY-SA 3.0