.NET Core中用于LDAP搜索及Active Directory数据获取的推荐包
适合.NET Core 7 Worker Service的Active Directory数据获取工具包
以下是几个实用的工具包,可用于在.NET Core 7 Worker Service中获取OU、用户及组信息:
1. System.DirectoryServices.AccountManagement
这是微软官方提供的封装库,对AD常用操作做了友好封装,上手门槛低,无需额外安装NuGet包(.NET Core 3.0及以上版本内置支持)。
核心特点:
- 提供
PrincipalContext、UserPrincipal、GroupPrincipal等封装类,直接对应AD实体 - 支持身份验证、用户/组查询、成员关系检查等常见操作
- 兼容Windows环境下的AD集成认证
示例代码:
using System.DirectoryServices.AccountManagement; // 初始化AD上下文 using var context = new PrincipalContext(ContextType.Domain, "your-domain.com"); // 获取单个用户 var user = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, "username"); Console.WriteLine($"用户:{user.DisplayName},邮箱:{user.EmailAddress}"); // 获取所有组 var groupSearcher = new PrincipalSearcher(new GroupPrincipal(context)); foreach (var group in groupSearcher.FindAll()) { Console.WriteLine($"组:{group.DisplayName}"); } // 查询OU(需结合DirectorySearcher) using var directoryEntry = context.ConnectedServer as DirectoryEntry; var ouSearcher = new DirectorySearcher(directoryEntry, "(objectCategory=organizationalUnit)"); foreach (SearchResult result in ouSearcher.FindAll()) { Console.WriteLine($"OU:{result.Properties["name"][0]}"); }
2. Novell.Directory.Ldap.NETStandard
这是一款跨平台的LDAP客户端库,专注于LDAP协议的原生操作,轻量高效,适合需要精细控制查询逻辑的场景,完美支持.NET Core环境。
核心特点:
- 纯.NET Standard实现,跨Windows、Linux、macOS
- 直接操作LDAP协议,支持复杂查询筛选、分页、属性选择
- 体积小,性能优于封装程度高的库
示例代码:
using Novell.Directory.Ldap; // 连接AD服务器 using var ldapConn = new LdapConnection(); ldapConn.Connect("your-domain-controller.com", 389); ldapConn.Bind("domain\\username", "password"); // 查询OU var ouResults = ldapConn.Search( "DC=your-domain,DC=com", LdapConnection.ScopeSub, "(objectCategory=organizationalUnit)", new[] { "name", "distinguishedName" }, false ); foreach (var entry in ouResults) { Console.WriteLine($"OU名称:{entry.GetAttributeValue("name")},DN:{entry.DN}"); } // 查询组 var groupResults = ldapConn.Search( "DC=your-domain,DC=com", LdapConnection.ScopeSub, "(objectCategory=group)", new[] { "name", "samAccountName" }, false ); foreach (var entry in groupResults) { Console.WriteLine($"组名称:{entry.GetAttributeValue("name")}"); } // 查询用户 var userResults = ldapConn.Search( "DC=your-domain,DC=com", LdapConnection.ScopeSub, "(objectCategory=person)", new[] { "displayName", "email", "samAccountName" }, false ); foreach (var entry in userResults) { Console.WriteLine($"用户:{entry.GetAttributeValue("displayName")},邮箱:{entry.GetAttributeValue("email")}"); } ldapConn.Disconnect();
3. System.DirectoryServices.Protocols
这是微软官方提供的底层LDAP操作库,比AccountManagement更灵活,适合处理复杂的AD查询需求,性能表现更优。
核心特点:
- 直接基于LDAP协议通信,支持自定义请求和响应处理
- 适合大规模数据查询、分页查询、自定义筛选条件
- 官方维护,兼容性有保障
示例代码:
using System.DirectoryServices.Protocols; // 配置LDAP连接 var ldapConn = new LdapConnection(new LdapDirectoryIdentifier("your-domain-controller.com", 389)); ldapConn.Credential = new System.Net.NetworkCredential("domain\\username", "password"); ldapConn.AuthType = AuthType.Negotiate; ldapConn.Bind(); // 查询OU var ouSearchRequest = new SearchRequest( "DC=your-domain,DC=com", "(objectCategory=organizationalUnit)", SearchScope.Subtree, "name", "distinguishedName" ); var ouResponse = (SearchResponse)ldapConn.SendRequest(ouSearchRequest); foreach (var entry in ouResponse.Entries) { Console.WriteLine($"OU:{entry.Attributes["name"][0]}"); } // 查询组 var groupSearchRequest = new SearchRequest( "DC=your-domain,DC=com", "(objectCategory=group)", SearchScope.Subtree, "name", "samAccountName" ); var groupResponse = (SearchResponse)ldapConn.SendRequest(groupSearchRequest); foreach (var entry in groupResponse.Entries) { Console.WriteLine($"组:{entry.Attributes["name"][0]}"); } ldapConn.Dispose();
内容的提问来源于stack exchange,提问作者Imran Yaseen
相关产品推荐
相关产品推荐

