开发者

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).ToLis开发者_JAVA技巧t();

    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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜