获取给定 UserPrincipal 的组列表

Get the list of Groups for the given UserPrincipal(获取给定 UserPrincipal 的组列表)
本文介绍了获取给定 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 的组列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

ActiveDirectory error 0x8000500c when traversing properties(遍历属性时 ActiveDirectory 错误 0x8000500c)
search by samaccountname with wildcards(使用通配符按 samaccountname 搜索)
Can you find an Active Directory User#39;s Primary Group in C#?(你能在 C# 中找到 Active Directory 用户的主要组吗?)
Query From LDAP for User Groups(从 LDAP 查询用户组)
How can I get DOMAINUSER from an AD DirectoryEntry?(如何从 AD DirectoryEntry 获取 DOMAINUSER?)
Get List of Users From Active Directory In A Given AD Group(从给定 AD 组中的 Active Directory 获取用户列表)