获取给定 UserPrincipal 的组列表
- 作者: 爱的旋律88
- 来源: 51数据库
- 2022-10-21
问题描述
我想获取用户所在组的列表.
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}
- C#通过fleck实现wss协议的WebSocket多人Web实时聊天(附源码)
- 团队城市未满足要求:MSBuildTools12.0_x86_Path 存在
- 使用 MSBuild.exe 在发布模式下构建 C# 解决方案
- 当我发布 Web 应用程序时,AfterPublish 脚本不运行
- 构建时 T4 转换的产品仅在下一个构建中使用
- ASP.NET Core Application (.NET Framework) for Windows x64 only error in project.assets.json
- 新的 .csproj 格式 - 如何将整个目录指定为“链接文件"到子目录?
- 如何将条件编译符号(DefineConstants)传递给 msbuild
- MSBuild 支持 Visual Studio 2017 RTM 中的 T4 模板
- NuGet 包还原找不到包,没有源
