listing all members of an active directory group
I'm having trouble retrieving the members of a certain group in active directory. The code I'm using 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);
开发者_如何学Python }
}
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 member's username. I've tried getting different properties, but I haven't been able to get this to work.
This is part of a CLR function that I'm creating for SQL Server 2005.
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;
}
精彩评论