gpt4 book ai didi

c# - Active Directory 用户组成员资格 GroupPrincipal

转载 作者:太空狗 更新时间:2023-10-29 20:31:58 25 4
gpt4 key购买 nike

我正在尝试使用 GroupPrincipal(System.DirectoryServices.AccountManagement 命名空间的一部分)来填充字符串类型的列表,因此我可以检查是否有用户是 Active Directory 组的成员。这是我到目前为止编写的编辑类:

public class ActiveDirectoryMembership
{
private PrincipalContext context = new PrincipalContext(ContextType.Domain, Environment.UserDomainName);
private List<string> GroupName {get;set;}

public ActiveDirectoryMembership()
{
//Code snipped - this part returns a list of users
populateGroups();
}

private void populateGroups()
{
GroupPrincipal SearchGroup = GroupPrincipal.FindByIdentity(context, "Group Name");
GroupName = new List<string>();
foreach (UserPrincipal p in GroupName.GetMembers())
{
GroupName.add(p.SamAccountName);
}
}

那么,我哪里出错了?

提前致谢:)

最佳答案

您的代码的这种修改有效(我进行了测试以确保):

using System.DirectoryServices.AccountManagement;


private static readonly string DomainName = "domaincontrollercomputer.domain.com";
private static readonly string DomainContainer = "DC=DOMAIN,DC=COM";
private static readonly string ADGroupName = "AD Group Name";
private List<string> GroupName {get;set;}

private void populateGroups()
{
using (var ctx = new PrincipalContext(ContextType.Domain, DomainName, DomainContainer))
{
using (var grp = GroupPrincipal.FindByIdentity(ctx, ADGroupName))
{
GroupName = new List<string>();
foreach (var member in grp.GetMembers())
{
GroupName.Add(member.SamAccountName);
}
}
}
}

关于c# - Active Directory 用户组成员资格 GroupPrincipal,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5247263/

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