Search code examples
c#asp.netactive-directory

How to get the groups of a user in Active Directory? (c#, asp.net)


I use this code to get the groups of the current user. I want to provide the user in the parameter to get its groups. How can I do this?

using System.Security.Principal;

public ArrayList Groups()
{
    ArrayList groups = new ArrayList();

    foreach (IdentityReference group in System.Web.HttpContext.Current.Request.LogonUserIdentity.Groups)
    {
        groups.Add(group.Translate(typeof(NTAccount)).ToString());
    }

    return groups;
}

Solution

  • If you're on .NET 3.5 or up, you can use the new System.DirectoryServices.AccountManagement (S.DS.AM) namespace which makes this a lot easier than it used to be.

    Read all about it here: Managing Directory Security Principals in the .NET Framework 3.5

    Update: older MSDN magazine articles aren't online anymore, unfortunately - you'll need to download the CHM for the January 2008 MSDN magazine from Microsoft and read the article in there.

    Basically, you need to have a "principal context" (typically your domain), a user principal, and then you get its groups very easily:

    public List<GroupPrincipal> GetGroups(string userName)
    {
       List<GroupPrincipal> result = new List<GroupPrincipal>();
    
       // establish domain context
       PrincipalContext yourDomain = new PrincipalContext(ContextType.Domain);
    
       // find your user
       UserPrincipal user = UserPrincipal.FindByIdentity(yourDomain, userName);
    
       // if found - grab its groups
       if(user != null)
       {
          PrincipalSearchResult<Principal> groups = user.GetAuthorizationGroups();
    
          // iterate over all groups
          foreach(Principal p in groups)
          {
             // make sure to add only group principals
             if(p is GroupPrincipal)
             {
                 result.Add((GroupPrincipal)p);
             }
          }
       }
    
       return result;
    }
    

    and that's all there is! You now have a result (a list) of authorization groups that user belongs to - iterate over them, print out their names or whatever you need to do.

    Update: In order to access certain properties, which are not surfaced on the UserPrincipal object, you need to dig into the underlying DirectoryEntry:

    public string GetDepartment(Principal principal)
    {
        string result = string.Empty;
    
        DirectoryEntry de = (principal.GetUnderlyingObject() as DirectoryEntry);
    
        if (de != null)
        {
           if (de.Properties.Contains("department"))
           {
              result = de.Properties["department"][0].ToString();
           }
        }
    
        return result;
    }
    

    Update #2: seems shouldn't be too hard to put these two snippets of code together.... but ok - here it goes:

    public string GetDepartment(string username)
    {
        string result = string.Empty;
    
        // if you do repeated domain access, you might want to do this *once* outside this method, 
        // and pass it in as a second parameter!
        PrincipalContext yourDomain = new PrincipalContext(ContextType.Domain);
    
        // find the user
        UserPrincipal user = UserPrincipal.FindByIdentity(yourDomain, username);
    
        // if user is found
        if(user != null)
        {
           // get DirectoryEntry underlying it
           DirectoryEntry de = (user.GetUnderlyingObject() as DirectoryEntry);
    
           if (de != null)
           {
              if (de.Properties.Contains("department"))
              {
                 result = de.Properties["department"][0].ToString();
              }
           }
        }
    
        return result;
    }