1

我正在检索所有组、子组和用户

我正在使用以下代码

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);
    }

它工作正常,但由于我将由代码质量控制器控制,有没有更好的方法来编写它?或者你觉得可以吗?

4

1 回答 1

2

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.

于 2013-02-06T14:01:49.683 回答