Design the Form as Following(Default.aspx)
<table>
<tr>
<td>
<asp:ListBox ID="lstGroups"runat="server"Height="520px"width="200px"AutoPostBack="true"EnableViewState="true"
onselectedindexchanged="lstGroups_SelectedIndexChanged"></asp:ListBox>
</td>
<td valign="top">
<asp:ListBox ID="lstGroupmembers"runat="server"Height="300px"Width="200px"AutoPostBack="true"></asp:ListBox>
</td>
</tr>
</table>
Default.aspx.cs
Add the following Namespaces
usingSystem.Collections.Specialized;
This namespace is used for StringCollection
Add the following reference.
· System.DirectoryServices.
· System.DirectoryServices.AccountManagement.
Then u can add the namespace
using System.DirectoryServices;
using System.Configuration;
-----------------------------------------------------------------------------------------------
Write the web.config File as following.
<appSettings>
<add key="DomainName" value="ABC0"/>
<addkey="DefaultActiveDirectoryServer"value="LDAP://ABC0"/>
Write the following Code in Page Load
protected void Page_Load(object sender, EventArgs e)
{
foreach (string @group in GetGroups())
{
lstGroups.Items.Add(@group);
}
}
Write the following Methods in the Page.
public StringCollection GetGroupMembers(string strDomain, string strGroup)
{
StringCollection groupMemebers = new StringCollection();
try
{
DirectoryEntry ent = new DirectoryEntry("LDAP://DC=" + strDomain + ",DC=com");
DirectorySearcher srch = new DirectorySearcher("(" + strGroup + ")");
SearchResultCollection coll = srch.FindAll();
foreach (SearchResult rs in coll)
{
ResultPropertyCollection resultPropColl = rs.Properties;
foreach (Object memberColl in resultPropColl["member"])
{
DirectoryEntry gpMemberEntry = new DirectoryEntry("LDAP://" + memberColl);
System.DirectoryServices.PropertyCollection userProps = gpMemberEntry.Properties;
object obVal = userProps["sAMAccountName"].Value;
if (null != obVal)
{
groupMemebers.Add(obVal.ToString());
}
}
}
}
catch (Exception ex)
{
Trace.Write(ex.Message);
}
return groupMemebers;
}
public static List<string> GetGroups()
{
DirectoryEntry objADAM = default(DirectoryEntry);
// Binding object.
DirectoryEntry objGroupEntry = default(DirectoryEntry);
// Group Results.
DirectorySearcher objSearchADAM = default(DirectorySearcher);
// Search object.
SearchResultCollection objSearchResults = default(SearchResultCollection);
// Results collection.
string strPath = null;
// Binding path.
List<string> result = new List<string>();
string Path = Convert.ToString(ConfigurationManager.AppSettings["DefaultActiveDirectoryServer"]);
// Construct the binding string.
strPath = Path;
//Change to your ADserver
// Get the AD LDS object.
try
{
objADAM = new DirectoryEntry(strPath);
objADAM.RefreshCache();
}
catch (Exception e)
{
throw e;
}
// Get search object, specify filter and scope,
// perform search.
try
{
objSearchADAM = new DirectorySearcher(objADAM);
objSearchADAM.Filter = "(&(objectClass=group))";
objSearchADAM.SearchScope = SearchScope.Subtree;
objSearchResults = objSearchADAM.FindAll();
}
catch (Exception e)
{
throw e;
}
// Enumerate groups
try
{
if (objSearchResults.Count != 0)
{
foreach (SearchResult objResult in objSearchResults)
{
objGroupEntry = objResult.GetDirectoryEntry();
result.Add(objGroupEntry.Name);
}
}
else
{
throw new Exception("No groups found");
}
}
catch (Exception e)
{
throw new Exception(e.Message);
}
return result;
}
========================================
protected void lstGroups_SelectedIndexChanged(object sender, EventArgs e)
{
string domainName = Convert.ToString(ConfigurationManager.AppSettings["DomainName"]);
string group = lstGroups.SelectedValue;
StringCollection groupMembers = this.GetGroupMembers(domainName, group);
//Response.Write("The members of"+group +"are");
lstGroupmembers.Items.Clear();
foreach (string strMember in groupMembers)
{
//Response.Write("<br><b>" + strMember + "</b>");
lstGroupmembers.Items.Add(strMember);
}
}