I have a very strange problem...I have a function which works in 99/100 times...But unfortunately I have noticed one group which generates me some problems.
The worst thing is that I don't see anything special in it...name is nomral, I have access to list it...But my program doesn't return any results...It threat it as if it was empty...
But I know its not true:

String listGroupUsers(string Name)
        {
            try
            {
                SearchResult result;
                DirectorySearcher search = new DirectorySearcher();
                search.Filter = String.Format("(cn={0})", Name);
                search.PropertiesToLoad.Add("member");
                result = search.FindOne();
                if (result == null)
                {
                    search.Filter = String.Format("(samAccountName=" + Name + ")");
                    result = search.FindOne();
                    if (result == null)
                    {
                        tempreport1.Add("ERROR: Input name couldn't be found in AD");
                        return "ERROR: Input name couldn't be found in AD"; 
                    }
                }
                StringBuilder userList = new StringBuilder();
                if (result != null)
                {
                    for (int counter = 0; counter < result.Properties["member"].Count; counter++)
                    {
                        string user = (string)result.Properties["member"][counter];
                        string property = getproperty("LDAP://" + user, "displayname");
                        if (property=="No record found") // grupy nie maja display name - wiec jak sie na nich wywali ma wyciagnac win2000 name
                        {  
                        property = getproperty("LDAP://" + user, "samAccountName");
                        }
                        userList.Append(property);
                        userList.Append("\r\n");
                    }
                }
                tempreport1.AddRange(userList.ToString().Split(new string[] { "\r\n" }, StringSplitOptions.None).ToList());
                return userList.ToString();
            }
            catch (Exception)
            {
                return " ";
            }
        }

The most interesting thing is that the most important condition is fulfilled (23rd line):

if (result != null)

so there ARE results but...when I try to show any results with line:

result.Properties["member"][X]
X = any number

it returns null...

result.Properties["member"].Count

is 0..so the for loop in my program isn't launched at all... WHY?...I am really curious because that group count about 3000 members...

can it be a bug? because for me the code is very OK..

Ok I have found out that this is bug, it was corrected in Framework 4.0. But I need to use FM 3.5

I have found a code:

    String listGroupUsers(string GroupName)
    {
        PrincipalContext ctx = new PrincipalContext(ContextType.Domain, domain);
        GroupPrincipal grp = GroupPrincipal.FindByIdentity(ctx, IdentityType.Name, GroupName);

        if (grp != null)
        {
            foreach (Principal p in grp.GetMembers(true))
            {
                AddMessage(p.Name + "\r\n", Color.DarkGreen, FontStyle.Regular);
            }


            grp.Dispose();
            ctx.Dispose();
        }
        return "";
    }

The problem is that this function returns all members, even in nested groups...Its nice, but I would like to be informed what group is nested (beside seeing members only).
IS there any way to do that?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.