C# 获取本地组的成员
我正在检查用户是否属于特定组。我的代码编写如下C# 获取本地组的成员,c#,active-directory,directoryentry,principalcontext,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; PrincipalCont
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
此问题是否有任何可能的解决方案?groupPrincipal为空,因为您正在搜索的组(“TestGrp1”)从未找到-很可能它不存在 您的代码在现有组中正常工作