在 C# 中确定用户给定 LDAP 服务器详细信息的 usergroups/claims
Determine usergroups/claims of user given LDAP server details in C#
我们有一个测试活动目录 LDAP 服务器。我还有一些用户名和密码。我想确定特定用户的 claims/user 组,同时登录到另一个域。这可以用一些 C# 代码来完成吗?我想我将不得不使用 System.DirectoryServices.dll
如果可以使用.Net 3.5 或更高版本,则尝试System.DirectoryServices.AccountManagement.dll 汇编。它提供 System.DirectoryServices.AccountManagement 命名空间和基于 Principal 的 classes,例如 UserPrincipal 和 GroupPrincipal。它们代表了更高层次的抽象并且更易于使用。
例如,要连接到另一个域中的 LDAP 服务器(根据此抽象获取 Principal Context),您需要使用 this constructor:[=18= 创建 PrincipalContext class 的实例]
PrincipalContext anotherDomainContext = new PrincipalContext(ContextType.Domain, DomainDnsName, RootOU, ContextOptions.SimpleBind, QueryUserName, QueryUserPassword);
RootOU 类似于 "DC=Company,DC=COM",因此 DomainDnsName 类似于 "company.com" 或 "ldapserver.company.com"。如果您的 AD 林中有多个域,请尝试连接到全局目录 (DomainDnsName = "ldapserver.company.com:3268")。 QueryUserName 和 QueryUserPassword 是带有用户名和密码的纯字符串,用于连接到 LDAP 服务器。用户名可以包含域名,例如:
string QueryUserName = @"company\username";
连接到 LDAP 服务器后,您可以搜索用户:
UserPrincipal user = UserPrincipal.FindByIdentity(anotherDomainContext , IdentityType.SamAccountName, samAccountName);
您在何处提供 samAccountName 和上下文(连接)。
有了 UserPrincipal 实例,您就可以访问 its properties and methods。例如,获取用户的安全组:
PrincipalSearchResult<Principal> searchResults = user.GetGroups();
List<GroupPrincipal> groupsList = searchResults.Select(result => result as GroupPrincipal).
Where(group => (group != null) &&
(group.IsSecurityGroup.HasValue) &&
(group.IsSecurityGroup.Value))
注意 GetGroups returns 只有用户直接所属的组。要获取包括嵌套在内的所有用户组,请调用 GetAuthorizationGroups。此外,您可以避免使用 LINQ,它只是用于从 GetGroups 中过滤安全组。
使用 GroupPrincipal 您可以检查名称 属性 或成员集合。
我们有一个测试活动目录 LDAP 服务器。我还有一些用户名和密码。我想确定特定用户的 claims/user 组,同时登录到另一个域。这可以用一些 C# 代码来完成吗?我想我将不得不使用 System.DirectoryServices.dll
如果可以使用.Net 3.5 或更高版本,则尝试System.DirectoryServices.AccountManagement.dll 汇编。它提供 System.DirectoryServices.AccountManagement 命名空间和基于 Principal 的 classes,例如 UserPrincipal 和 GroupPrincipal。它们代表了更高层次的抽象并且更易于使用。
例如,要连接到另一个域中的 LDAP 服务器(根据此抽象获取 Principal Context),您需要使用 this constructor:[=18= 创建 PrincipalContext class 的实例]
PrincipalContext anotherDomainContext = new PrincipalContext(ContextType.Domain, DomainDnsName, RootOU, ContextOptions.SimpleBind, QueryUserName, QueryUserPassword);
RootOU 类似于 "DC=Company,DC=COM",因此 DomainDnsName 类似于 "company.com" 或 "ldapserver.company.com"。如果您的 AD 林中有多个域,请尝试连接到全局目录 (DomainDnsName = "ldapserver.company.com:3268")。 QueryUserName 和 QueryUserPassword 是带有用户名和密码的纯字符串,用于连接到 LDAP 服务器。用户名可以包含域名,例如:
string QueryUserName = @"company\username";
连接到 LDAP 服务器后,您可以搜索用户:
UserPrincipal user = UserPrincipal.FindByIdentity(anotherDomainContext , IdentityType.SamAccountName, samAccountName);
您在何处提供 samAccountName 和上下文(连接)。
有了 UserPrincipal 实例,您就可以访问 its properties and methods。例如,获取用户的安全组:
PrincipalSearchResult<Principal> searchResults = user.GetGroups();
List<GroupPrincipal> groupsList = searchResults.Select(result => result as GroupPrincipal).
Where(group => (group != null) &&
(group.IsSecurityGroup.HasValue) &&
(group.IsSecurityGroup.Value))
注意 GetGroups returns 只有用户直接所属的组。要获取包括嵌套在内的所有用户组,请调用 GetAuthorizationGroups。此外,您可以避免使用 LINQ,它只是用于从 GetGroups 中过滤安全组。
使用 GroupPrincipal 您可以检查名称 属性 或成员集合。