从AD获取组子组和用户(质量检查

时间:2013-02-01 15:36:27

标签: c# active-directory

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

我正在使用以下代码

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

它运行正常,但由于我将由Code质量控制器控制,有没有更好的方法来编写它?或者你可以吗?

1 个答案:

答案 0 :(得分:2)

我并不完全清楚你想要做什么,但我看到一些重新发明的车轮在这里进行。

查看System.DirectoryServices.AccountManagement,尤其是GroupPrincipal class

你会发现一些很棒的方法,比如getMembers()(带递归),IsMemberOf()以及其他许多有用的东西。

相关问题