gpt4 book ai didi

c# - 获取本地组的成员

转载 作者:行者123 更新时间:2023-11-30 23:11:41 27 4
gpt4 key购买 nike

我正在检查用户属于特定组的天气。我的代码是这样写的

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') 从未找到 - 很可能它不存在。

您的代码适用于现有组。

关于c# - 获取本地组的成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44582223/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com