获取组的列表中给出UserPrincipal列表中、UserPrincipal

2023-09-08 12:43:10 作者:︶风起人散灬

欲获得组该用户是列表中的

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

这是我的code:

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);

When I run, I got the following error at the line 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:

推荐答案

在省略了LDAP容器属性中PrincipalContext类描述,运行code用户必须具有读取权限,无论是默认的用户容器(即CN =用户​​,DC =您的域,DC = COM)和计算机容器(如CN =计算机,DC​​ =您的域,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}

((新System.Linq.SystemCore_EnumerableDebugView(组))。项目[5])。说明引发了异常类型'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}

请查看我的博客文章 与PrincipalContext">验证问题与P​​rincipalContext

Please check my blog post Authentication Problems with PrincipalContext