Get the list of Groups for the given UserPrincipal

后端 未结 2 1821
再見小時候
再見小時候 2020-12-11 03:46

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

This is my code:

PrincipalContext ctx = new PrincipalContext(ContextType.Domain, \"mydomain.         


        
相关标签:
2条回答
  • 2020-12-11 04:14

    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’ threw an exception of type ‘System.NullReferenceException’ string {System.NullReferenceException}

    • ((new System.Linq.SystemCore_EnumerableDebugView(groups)).Items[5]).Description’ threw an exception of type ‘System.Runtime.InteropServices.COMException’ string {System.Runtime.InteropServices.COMException}

    0 讨论(0)
  • 2020-12-11 04:22

    try something like

    foreach(Principal p in results)
    { 
       if (p is GroupPrincipal) 
          Response.Write(p.DisplayName); 
    }
    

    I know it sounds dumb, but it has worked for me in the past. Your results look like it only actually found 1 security group and 8 "other" types of groups. Those "other" groups may not possess those attributes.

    0 讨论(0)
提交回复
热议问题