Link to home
Start Free TrialLog in
Avatar of spandor
spandor

asked on

How do I get a list of members in a particular Active Directory group in C#.NET

I try and try. I can't find anything working for me. I simply want, when a domain user goes to the www page (ASP.NET and C#.NET), gets a list of members of a particiular group. Any hints, code examples?

IIS is on Windows Server 2003 SP2, LDAP is on Windows Server 2008 R2

Pls help!
Avatar of Postmaster
Postmaster
Flag of Australia image

Here is the LDIFDE command (LDAP query).

I am not a C# coder - but it may help.

Say your domain is DomainName.Domain.Com
The container is ContainerName

ldifde -f D:\ldif_grpexport.ldf -d "OU=ContainerName,OU=DomainName,DC=Domain,DC=Com" -r "(&(objectCategory=group)(displayName=name-of-group))" -l "DN,objectClass,cn,displayName"
Use Code:

using System.DirectoryServices;

ArrayList GetADGroupUsers(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 user = (string)result.Properties["member"][counter];
               userNames.Add(user);
       }
   }
   return userNames;
}

Reference :

http://www.codeproject.com/KB/system/QueryADwithDotNet.aspx
Avatar of spandor
spandor

ASKER

I tried this one and it didnt work. Maybe becouse it is from 2005 and I use Framework 4.0.
Are you referring to a Security Group, like "Domain Admins", or an Organizational Unit (i.e. the containers in the tree, on the left, when viewing in Active Directory Users & Computers)?  There are distinctly different things, and the code will be different for each.
Avatar of spandor

ASKER

tgerbert,

Yes, im talking about security groups. I setup a new security group which is a role in my app. In one case I need to check if user logged to his computer attached to the domain is in the group and also I need to list members from this group.
Here's a simple console application example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.DirectoryServices;
using System.Collections;

namespace ConsoleApplication1
{
	class Program
	{
		static void Main(string[] args)
		{
			string[] users = GetGroupMembers(Environment.UserDomainName, "Domain Admins");

			foreach (string user in users)
				Console.WriteLine(user);

			Console.ReadKey();
		}

		static string[] GetGroupMembers(string DomainName, string GroupName)
		{
			List<string> members = new List<string>();

			// Get the group's directory entry
			using (DirectoryEntry group = new DirectoryEntry(String.Format("WinNT://{0}/{1},group", DomainName, GroupName)))
			{

				// Add members' names to members list
				foreach (object member in (IEnumerable)group.Invoke("Members"))
				{
					using (DirectoryEntry memberEntry = new DirectoryEntry(member))
					{
						members.Add(memberEntry.Name);
					}
				}
			}

			return members.ToArray();
		}
	}
}

Open in new window

Avatar of spandor

ASKER

Is there a method to get a user accout attributes like Name, Surname, Email etc. ?
ASKER CERTIFIED SOLUTION
Avatar of angus_young_acdc
angus_young_acdc
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>> Is there a method to get a user accout attributes like Name, Surname, Email etc. ?
Do you mean you want to get these details while listing the members of a group? Or separately on a user-by-user basis?