C# 正在Active Directory中获取组成员资格(memberOf)列表

C# 正在Active Directory中获取组成员资格(memberOf)列表,c#,active-directory,C#,Active Directory,我带着我的Active Directory工具回来了 我正在尝试列出用户的“成员”属性中的组。 下面是我使用的函数: public static DataTable ListGroupsByUser(string selectedOu) { DataTable groupListByUser = new DataTable(); String dom = "OU=" + selectedOu + ",OU=XXX,DC=XXX,DCXXX,DC=XXX,DC=XXX";

我带着我的Active Directory工具回来了

我正在尝试列出用户的“成员”属性中的组。 下面是我使用的函数:

public static DataTable ListGroupsByUser(string selectedOu)
{
    DataTable groupListByUser = new DataTable();
    String dom = "OU=" + selectedOu + ",OU=XXX,DC=XXX,DCXXX,DC=XXX,DC=XXX";
    DirectoryEntry directoryObject = new DirectoryEntry("LDAP://" + dom);

    DataColumn column;
    DataRow row;

    column = new DataColumn();
    column.ColumnName = "ID";
    groupListByUser.Columns.Add(column);

    column = new DataColumn();
    column.ColumnName = "User";
    groupListByUser.Columns.Add(column);

    column = new DataColumn();
    column.ColumnName = "Groups";
    groupListByUser.Columns.Add(column);
    int i = 1;

    foreach (DirectoryEntry child in directoryObject.Children)
    {                
        row = groupListByUser.NewRow();
        groupListByUser.Rows.Add(row);
        row["ID"] = i++;

        if (child.Properties["memberOf"].Value != null)
        {                    
            row["User"] = child.Properties["sAMAccountName"].Value.ToString();
            row["Groups"] = child.Properties["memberOf"].Value.ToString();
        }
        else
        {
            row["Groups"] = "blabla";
        }
    }
    return groupListByUser;
}
它为只属于一个组的用户返回正确的组。一旦有多个组,它就会返回System.Object[]


如何查看所有组?

问题在于您的
属性[“memberOf”].Value.ToString()

我做了一点调查,这个代码对我很有用:

var memberGroups = child.Properties["memberOf"].Value;

if (memberGroups.GetType() == typeof(string))
{
    row["Groups"] = (String)memberGroups;
}
else if (memberGroups.GetType().IsArray)
{
    var memberGroupsEnumerable = memberGroups as IEnumerable;

    if (memberGroupsEnumerable != null)
    {
        var asStringEnumerable = memberGroupsEnumerable.OfType<object>().Select(obj => obj.ToString());
        row["Groups"] = String.Join(", ", asStringEnumerable);
    }
}
else
{
    row["Groups"] = "No group found.";
}
var memberGroups=child.Properties[“memberOf”].Value;
if(memberGroups.GetType()==typeof(string))
{
行[“组”]=(字符串)成员组;
}
else if(memberGroups.GetType().IsArray)
{
var memberGroupsEnumerable=作为IEnumerable的成员组;
if(memberGroupsEnumerable!=null)
{
var asstringuenumerable=memberGroupsEnumerable.OfType().Select(obj=>obj.ToString());
行[“组”]=String.Join(“,”,asStringEnumerable);
}
}
其他的
{
行[“组”]=“未找到组。”;
}

它不是很可爱,但它很有效,并为进一步改进提供了空间。;-)

如果您使用的是.NET 3.5及更高版本,则应检查
System.DirectoryServices.AccountManagement
(S.DS.AM)命名空间。请在此处阅读所有相关内容:

基本上,您可以定义域上下文并在AD中轻松找到用户和/或组:

// set up domain context
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);

// find a user
UserPrincipal user = UserPrincipal.FindByIdentity(ctx, "SomeUserName");

if(user != null)
{
   var groups = user.GetGroups();
   // or there's also:
   //var authGroups = userByEmail.GetAuthorizationGroups()
}
调用
GetGroups()
GetAuthorizationGroups()
也将返回嵌套组成员身份-因此您不再需要搜索这些嵌套成员身份


新的S.DS.AM使得在广告中与用户和群组进行互动变得非常容易

谢谢Marc,我设法与用户和组打交道,但我不能做的是循环浏览用户成员列表。我用accountManagement名称空间而不是directoryServices来重建这个工具值得吗?@Oliver:谢谢大家。尽管有您的建议,我还是找到了以下解决方案:'code'if(child.Properties[“memberOf”].Value!=null){foreach(Object memberOf in child.Properties[“memberOf”]){//row[“User”]=child.Properties[“sAMAccountName”].Value.ToString();row[“]+=memberof.ToString()+”//“;}}}的成员{row[“成员”]=“未定义组”}
GetGroups()
vs
GetAuthorizationGroups()
差异?如果不想获得所有(嵌套的)差异,该怎么办组,但只想要名称?为了回答我自己的问题:在主体上调用GetUnderlyingObject并通过属性数组获取“memberOf”谢谢Oliver,我尝试了你的解决方案,但似乎有点棘手。我真的不明白我在做什么:var groups=objArray.Cast().Select(elem=>elem.Values.ToString());它返回一个错误:值不能为null。参数名称:source@HenryMeyer:我重新编写了我的代码,但我认为从长远来看,marc_的方法确实更好,因为你不必为这些字符串和对象数组而烦恼。它们是特定于类型的类,使访问所有这些内容更加方便.