获取给定 UserPrincipal 的组列表

4

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

The End

相关推荐

C# 中的多播委托奇怪行为?
Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)...
2023-11-11 C#/.NET开发问题
6

参数计数与调用不匹配?
Parameter count mismatch with Invoke?(参数计数与调用不匹配?)...
2023-11-11 C#/.NET开发问题
26

如何将代表存储在列表中
How to store delegates in a List(如何将代表存储在列表中)...
2023-11-11 C#/.NET开发问题
6

代表如何工作(在后台)?
How delegates work (in the background)?(代表如何工作(在后台)?)...
2023-11-11 C#/.NET开发问题
5

没有 EndInvoke 的 C# 异步调用?
C# Asynchronous call without EndInvoke?(没有 EndInvoke 的 C# 异步调用?)...
2023-11-11 C#/.NET开发问题
2

Delegate.CreateDelegate() 和泛型:错误绑定到目标方法
Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)...
2023-11-11 C#/.NET开发问题
14