Tuesday 17 December 2013

Get users based on LDAP group

  public DataTable getUsers(string username)
    {
        DataTable dtUsers = new DataTable();
        dtUsers.Columns.Add("users");
        try
        {
            // create domain context
            PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
            // fetch your group
            GroupPrincipal group = GroupPrincipal.FindByIdentity(ctx, username);
            // enumerate over the group's members
            foreach (Principal p in group.Members)
            {
                DataRow row = dtUsers.NewRow();
                row[0] = p.Name;
                dtUsers.Rows.Add(row);
                //Console.WriteLine("Principal '{0}', type '{1}'", p.Name, p.StructuralObjectClass);
            }

        }
        catch (Exception ex)
        {
            ErrorLog.Log(ex);
        }
        return dtUsers;
    }

No comments:

Post a Comment