• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 622
  • Last Modified:

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

I've got a security group called MANAGERS. How would I populate a dropdownlist with the names from this group?
0
Mike Miller
Asked:
Mike Miller
  • 4
  • 3
1 Solution
 
Kyle AbrahamsSenior .Net DeveloperCommented:
Is it an AD group?  If not what group?
0
 
Praveen VenuTechnical Project ManagerCommented:
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

0
 
Mike MillerSoftware EngineerAuthor Commented:
Yes, an AD group.
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
Kyle AbrahamsSenior .Net DeveloperCommented:
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

0
 
Mike MillerSoftware EngineerAuthor Commented:
What's the deal with IADsMembers and IADsUser? What ref/namespace would I use for those?
0
 
Kyle AbrahamsSenior .Net DeveloperCommented:
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

0
 
Mike MillerSoftware EngineerAuthor 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?
0
 
Mike MillerSoftware EngineerAuthor Commented:
Nevermind, I got it. I just took off

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

and it worked fine. Thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now