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


- Azure Active Directory 与 MVC,客户端和资源标识同一 2022-01-01
- 在 LINQ to SQL 中使用 contains() 2022-01-01
- 使用 rss + c# 2022-01-01
- C# 通过连接字符串检索正确的 DbConnection 对象 2022-01-01
- Windows 喜欢在 LINUX 中使用 MONO 进行服务开发? 2022-01-01
- CanBeNull和ReSharper-将其用于异步任务? 2022-01-01
- 在 C# 中异步处理项目队列 2022-01-01
- 为什么 C# 中的堆栈大小正好是 1 MB? 2022-01-01
- 带问号的 nvarchar 列结果 2022-01-01
- 是否可以在 .Net 3.5 中进行通用控件? 2022-01-01