获取给定 UserPrincipal 的组列表

时间:2022-11-10
本文介绍了获取给定 UserPrincipal 的组列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我想获取用户所在组的列表.

I want to get the list of groups which the user is in.

这是我的代码:

PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "mydomain.ac.uk",   "DC=mydomain,DC=AC,DC=UK", "user", "password");

UserPrincipal user = UserPrincipal.FindByIdentity(ctx, IdentityType.SamAccountName, "MyUser");

PrincipalSearchResult<Principal> results = user.GetGroups();

foreach(Principal p in results)
{
   Response.Write(p.Name);
}

运行时,在 Response.Write(p.Name);

System.Runtime.InteropServices.COMException: 指定的目录服务属性或值不存在.

System.Runtime.InteropServices.COMException: The specified directory service attribute or value does not exist.

当我检查结果计数时,它返回 9,第一组是 DomainUsers.

When I checked the count of the results, it returned 9 and the first group is DomainUsers.

如何迭代列表中的所有 9 个组?谢谢.

How can I iterate all 9 groups in the list? Thanks.

以下是我得到的用户列表:

The following is the list of users I get:

推荐答案

当省略 PrincipalContext Class 中描述的 LDAP 容器属性时,运行代码的用户必须对默认的 User 都具有读取权限容器(即 CN=Users,DC=yourDomain,DC=COM)和 Computers 容器(即 CN=Computers,DC=yourDomain,DC=COM).

When omitting the LDAP container property as described in PrincipalContext Class, the user running the code must have read permissions to both the default User Container (i.e. CN=Users,DC=yourDomain,DC=COM) and the Computers Container (i.e. CN=Computers,DC=yourDomain,DC=COM).

如果用户没有所需的权限,您将收到以下错误消息:

If the user does not have the required permissions you will get the following error messages:

指定的目录服务属性或值不存在

The specified directory service attribute or value does not exist

  • ‘context.Container’抛出了‘System.NullReferenceException’字符串类型的异常{System.NullReferenceException}

  • ‘context.Container’ threw an exception of type ‘System.NullReferenceException’ string {System.NullReferenceException}

((new System.Linq.SystemCore_EnumerableDebugView(groups)).Items[5]).Description’抛出类型异常System.Runtime.InteropServices.COMException"字符串{System.Runtime.InteropServices.COMException}

((new System.Linq.SystemCore_EnumerableDebugView(groups)).Items[5]).Description’ threw an exception of type ‘System.Runtime.InteropServices.COMException’ string {System.Runtime.InteropServices.COMException}

这篇关于获取给定 UserPrincipal 的组列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:你能在 C# 中找到 Active Directory 用户的主要组吗? 下一篇:使用通配符按 samaccountname 搜索

相关文章

最新文章