Active Directory 用户组成员资格 GroupPrincipal
- 作者: 网络没名
- 来源: 51数据库
- 2022-10-21
问题描述
我正在尝试使用 GroupPrincipal(System.DirectoryServices.AccountManagement 命名空间的一部分)来填充类型字符串的列表,因此我可以检查是否用户是 Active Directory 组的成员.这是我迄今为止编写的编辑类:
I am trying to use GroupPrincipal (part of the System.DirectoryServices.AccountManagement namespace) to populate a list of type string, so I can check to see if a user is a member of an Active Directory group. Here is the edited class that I have written so far:
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);
}
}
那么,我哪里出错了?
提前致谢:)
推荐答案
此代码修改有效(我进行了测试以确保):
This modification of your code works (I made tests to ensure):
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#通过fleck实现wss协议的WebSocket多人Web实时聊天(附源码)
- 团队城市未满足要求:MSBuildTools12.0_x86_Path 存在
- 使用 MSBuild.exe 在发布模式下构建 C# 解决方案
- 当我发布 Web 应用程序时,AfterPublish 脚本不运行
- 构建时 T4 转换的产品仅在下一个构建中使用
- ASP.NET Core Application (.NET Framework) for Windows x64 only error in project.assets.json
- 新的 .csproj 格式 - 如何将整个目录指定为“链接文件"到子目录?
- 如何将条件编译符号(DefineConstants)传递给 msbuild
- MSBuild 支持 Visual Studio 2017 RTM 中的 T4 模板
- NuGet 包还原找不到包,没有源
热点文章
团队城市未满足要求:MSBuildTools12.0_x86_Path 存在
0
使用 MSBuild.exe 在发布模式下构建 C# 解决方案
0
当我发布 Web 应用程序时,AfterPublish 脚本不运行
0
构建时 T4 转换的产品仅在下一个构建中使用
0
ASP.NET Core Application (.NET Framework) for Windows x64 only error in project.assets.json
0
新的 .csproj 格式 - 如何将整个目录指定为“链接文件"到子目录?
0
如何将条件编译符号(DefineConstants)传递给 msbuild
0
MSBuild 支持 Visual Studio 2017 RTM 中的 T4 模板
0
NuGet 包还原找不到包,没有源
0
使用 C# 6.0 功能运行 TFS 构建
0
