获取本地组的成员

时间:2017-06-16 06:26:20

标签: c# active-directory directoryentry principalcontext

我正在检查用户属于特定群组的天气。我的代码编写如下

public static  bool IsInGroup(string user, string group)
    {
        Console.WriteLine("The user name and group name is {0} {1}", user, group); //Check the parameter values

        bool result = false;
        PrincipalContext context = new PrincipalContext(ContextType.Domain);
        UserPrincipal userPrincipal = UserPrincipal.FindByIdentity(context,user);
        GroupPrincipal groupPrincipal = GroupPrincipal.FindByIdentity(context, group);
        if (userPrincipal != null)
        {
            if (userPrincipal.IsMemberOf(groupPrincipal))
            {
                result = true;
            }
        }
        return result;
    }

但我面临一个看起来像这样的错误

The user name and group name is sampat TestGrp1
Value cannot be null.
Parameter name: group

这个问题是否有任何可能的解决方案?

1 个答案:

答案 0 :(得分:2)

groupPrincipal 为空,因为您搜索的组('TestGrp1')从未找到 - 很可能它不存在。

您的代码可以与现有群组一起正常使用。

相关问题