Maro
Maro

Reputation: 2629

Getting groups subgroups and users from AD (quality check

I'm retrieving all groups, subgroups, and users

I'm using the following code

ResultPropertyValueCollection resultCollection = result.Properties["member"];
                foreach (var oneResult in resultCollection)
                {

                    if (oneResult.ToString().IndexOf("OU=Groups") > -1)
                    {
                        group.SubGroupsNames.Add(GetCN(oneResult.ToString()));
                    }
                    else if (oneResult.ToString().IndexOf("OU=Users") > -1)
                    {
                        group.UsersNames.Add(GetCN(oneResult.ToString()));
                    }
                }
private string GetCN(string line)
    {
        return line.Substring(line.IndexOf("CN=") + 3, line.IndexOf(",") - line.IndexOf("=")-1);
    }

It works fine but as I will be controlled by Code quality controller, is there is a better way to write that? Or is it OK for you?

Upvotes: 1

Views: 656

Answers (1)

Jordan
Jordan

Reputation: 221

I am not totally clear on what you are trying to do but I see some wheel reinventing going on here.

Take a look at System.DirectoryServices.AccountManagement, particularly the GroupPrincipal class.

You will find some great methods there such as getMembers() (with recursion), IsMemberOf() and plenty of other useful stuff.

Upvotes: 2

Related Questions