We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Populate a dropdownlist with members of a security group in ASP.NET(VB)

Medium Priority
665 Views
Last Modified: 2013-11-26
I've got a security group called MANAGERS. How would I populate a dropdownlist with the names from this group?
Comment
Watch Question

Kyle AbrahamsSenior .Net Developer
CERTIFIED EXPERT

Commented:
Is it an AD group?  If not what group?
Praveen VenuTechnical Project Manager

Commented:
http://www.dotnetactivedirectory.com/learn/How_to_get_Users_Belonging_to_Active_Directory_group.htm



 public static DataSet GetUsersForGroup(string GroupName)
{
DataSet dsUser = new DataSet();
DirectoryEntry de = GetDirectoryObject();
 
//create instance fo the direcory searcher
DirectorySearcher deSearch = new DirectorySearcher();
 
//set the search filter
deSearch.SearchRoot =de;
//deSearch.PropertiesToLoad.Add("cn");
deSearch.Filter = "(&(objectClass=group)(cn=" + GroupName +"))";
 
//get the group result
SearchResult results= deSearch.FindOne();
 
//Create a new table object within the dataset
DataTable tbUser = dsUser.Tables.Add("Users");
tbUser.Columns.Add("UserName");
tbUser.Columns.Add("DisplayName");
tbUser.Columns.Add("EMailAddress");
 
//Create default row
DataRow rwDefaultUser = tbUser.NewRow();
rwDefaultUser ["UserName"]= "0";
rwDefaultUser ["DisplayName"]="(Not Specified)";
rwDefaultUser ["EMailAddress"]="(Not Specified)";
tbUser.Rows.Add(rwDefaultUser);
 
//if the group is valid, then continue, otherwise return a blank dataset
if(results !=null)
{
//create a link to the group object, so we can get the list of members
//within the group
DirectoryEntry deGroup= new DirectoryEntry(results.Path,ADAdminUser,ADAdminPassword,AuthenticationTypes.Secure);
//assign a property collection
System.DirectoryServices.PropertyCollection pcoll = deGroup.Properties;
int n = pcoll["member"].Count;
 
//if there are members fo the group, then get the details and assign to the table
for (int l = 0; l < n ; l++)
{
//create a link to the user object sot hat the FirstName, LastName and SUername can be gotten
DirectoryEntry deUser= new DirectoryEntry(ADFullPath + "/" +pcoll["member"][l].ToString(),ADAdminUser,ADAdminPassword,AuthenticationTypes.Secure);
 
//set a new empty row
DataRow rwUser = tbUser.NewRow();
 
//populate the column
rwUser["UserName"]= GetProperty(deUser,"cn");
rwUser["DisplayName"]= GetProperty(deUser,"givenName") + " " + GetProperty(deUser,"sn");
rwUser["EMailAddress"]= GetProperty(deUser,"mail");
//append the row to the table of the dataset
tbUser.Rows.Add(rwUser);
 
//close the directory entry object
deUser.Close();
 
}
de.Close();
deGroup.Close();
}
 
return dsUser;
}
 
  

Open in new window

Mike MillerSoftware Engineer

Author

Commented:
Yes, an AD group.
Senior .Net Developer
CERTIFIED EXPERT
Commented:
Below is the attached VB.Net code.

instead of slGroupMembers.Add you could just add it write to your drop down list.


Public Shared Function GetADS_Group_Members(ByVal strGroup_Name As
String) As SortedList
Dim deGroup As DirectoryEntry
Dim MembersCollection As IADsMembers
Dim member As IADsUser
Dim objRootDSE As New DirectoryEntry("LDAP://RootDSE")
Dim slGroupMembers As New SortedList
 
Try
deGroup = New DirectoryServices.DirectoryEntry("LDAP://CN=" &
strGroup_Name & ",OU=User Groups,OU=Groups,OU=SOMEOU1,OU=SOMEOU2," &
objRootDSE.Properties("defaultNamingContext")(0).T oString())
MembersCollection = CType(deGroup.Invoke("Members"),
IADsMembers)
For Each member In MembersCollection
slGroupMembers.Add(member.LastName, Replace(member.Name,
"CN=", "", , , CompareMethod.Text) & "|;|" & member.ADsPath)
Next
Catch ex As Exception
Throw
Finally
If Not IsNothing(deGroup) Then deGroup.Close()
deGroup = Nothing
End Try
 
Return slGroupMembers
End Function

Open in new window

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Mike MillerSoftware Engineer

Author

Commented:
What's the deal with IADsMembers and IADsUser? What ref/namespace would I use for those?
Kyle AbrahamsSenior .Net Developer
CERTIFIED EXPERT

Commented:
http://bytes.com/groups/net-asp/341086-ads-ldap-system-directoryservices-get-users-group

See post November 19th, 2005, 05:32 PM

Imports System
Imports System.DirectoryServices
Imports System.Web.HttpContext
Imports System.Collections
Imports ActiveDs

Mike MillerSoftware Engineer

Author

Commented:
I keep getting Index out of range on the following

deGroup = New DirectoryServices.DirectoryEntry("LDAP://CN=" &
strGroup_Name & ",OU=User Groups,OU=Groups,OU=SOMEOU1,OU=SOMEOU2," &
objRootDSE.Properties("defaultNamingContext")(0).T oString())


Have you actually used this?
Mike MillerSoftware Engineer

Author

Commented:
Nevermind, I got it. I just took off

," &objRootDSE.Properties("defaultNamingContext")(0).ToString()

and it worked fine. Thanks!
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.