views:

263

answers:

1

hi, I'm having trouble retrieving the members of a certain group in active directory. The code im usinf is the following

[Microsoft.SqlServer.Server.SqlFunction(FillRowMethodName = "fillRow")]
public static IEnumerable getNTGroupMembers(string groupName)
    {
        SearchResult result;
        DirectorySearcher search = new DirectorySearcher();
        search.Filter = String.Format("(cn={0})", groupName);
        search.PropertiesToLoad.Add("member");
        result = search.FindOne();

        ArrayList userNames = new ArrayList();
        if (result != null)
        {
            for (int counter = 0; counter < result.Properties["member"].Count; counter++)
            {
                object user = (object)result.Properties["member"][counter];
                userNames.Add(user);
            }
        }
        return userNames;
    }

but it returns me a list of the following:

CN=X,OU=x,OU=X,OU=X,OU=X,DC=X,DC=X,DC=X

Does anyone know how i can return the members username. I've tried getting different properties, but i haven't being able to get to work.

This is part of a clr function I'm doing for sql server 2005.

A: 

Well i managed to do it. It still has some problems, for some reason for some AD groups it doesn't return some of its members.
If anyone knows a better way to do this please tell me!

[Microsoft.SqlServer.Server.SqlFunction(FillRowMethodName = "fillRow")]
public static IEnumerable getNTGroupMembers(string groupName)
{
    SearchResult result;
    DirectorySearcher search = new DirectorySearcher();
    search.Filter = String.Format("(cn={0})", groupName);
    search.PropertiesToLoad.Add("member");
    result = search.FindOne();
    ArrayList userNames = new ArrayList();
    if (result != null)
    {
        for (int counter = 0; counter < result.Properties["member"].Count; counter++)
        {
            string st = (string) result.Properties["member"][counter];
            DirectoryEntry gpMemberEntry = new DirectoryEntry(("LDAP://" + st));
            if (!(gpMemberEntry == null))
            {
                PropertyCollection userProps = gpMemberEntry.Properties;
                object objUser = userProps["sAMAccountname"].Value;
                userNames.Add(objUser);
            }
        }
    }
    return userNames;
}
private static void fillRow(Object obj, out string user)
{
    object row = (object)obj;
    user = (string)row;
}
Alan FL