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)));
Console.ReadKey();
}
When I'm trying to run this code it crashes on
Console.WriteLine(groupPrincipals.Any(gp => gp.Members.Contains(user)));
With Exception :
PrincipalOperationException
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
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
User contributions licensed under CC BY-SA 3.0