?
Solved

ASP.NET Get Members from Active Directory Group

Posted on 2008-06-13
11
Medium Priority
?
2,988 Views
Last Modified: 2010-04-21
I am trying to query an active directory group and return the members of that group. I have tried several different ways with no luck.  Attached is my last failed attempt.  Any help is greatly appreciated.
Imports System.DirectoryServices
Imports System.Security.Principal
 
Partial Class TestResults
    Inherits System.Web.UI.Page
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Response.Write(GetADGroupUsers("Health Department"))
    End Sub
 
    Public Function GetADGroupUsers(ByVal groupName As String) As ArrayList
        Dim result As SearchResult
        Dim search As New DirectorySearcher
        search.Filter = String.Format("(cn={0})", groupName)
        search.PropertiesToLoad.Add("member")
        result = search.FindOne()
        Dim i As Integer = 0
 
        Dim userNames As New ArrayList
 
        For i = 0 To result.Properties("members").Count
            userNames.Add(result.Properties("members")(i).ToString)
        Next
        GetADGroupUsers = userNames
    End Function
End Class

Open in new window

0
Comment
Question by:jayh99
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 3
11 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 21784840
could this sample code help:
http://msdn.microsoft.com/en-us/library/ms180885(VS.80).aspx

in short:
* you don't tell the directorysearch where to search from
* you asked findOne instead of findall

error:
        For i = 0 To result.Properties("members").Count
must be:

        For i = 0 To result.Properties("members").Count -1

0
 
LVL 1

Author Comment

by:jayh99
ID: 21785527
Thanks for the comment.  I will give it a try when I get in to work on Monday.
0
 
LVL 14

Expert Comment

by:Dustin Hopkins
ID: 21785729
the main problem other than not running the search. Is the property needs to be changed to "member" as members is not a property. Also you can use findone is you wish, so long as you don't have similiar group names. here is a quick example.

Note: the member property doesn't contain SAMAccounName(s) like (domain\username), but instead contains the user account's distinguished name (cn=username,dc=domain,dc=com) ect

Dim de As New DirectoryServices.DirectoryEntry("LDAP://domain.youcompany.com")'<---make sure to change to your ad connstring
        de.Username = SvcAcct '<--- domain accountname
        de.Password = SvcPass '<--- domain account password
        de.AuthenticationType = DirectoryServices.AuthenticationTypes.Secure
        Dim deSearch As New DirectoryServices.DirectorySearcher(de)
        Dim groupname As String = "Health Department" '<---group you wish to load
        deSearch.Filter = "(&(objectClass=group) (cn=" + groupname + "))"
        Dim results As DirectoryServices.SearchResultCollection = deSearch.FindAll()
        Dim result As DirectoryServices.SearchResult
        If (results.Count > 0) Then
            Dim userNames As New ArrayList
            For Each result In results
                For Each member As String In result.Properties("member")
                    userNames.add(member)
                Next
            Next
        End If
    End Sub

Open in new window

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 1

Author Comment

by:jayh99
ID: 21792981
   Private Sub GetADGroups()
        Dim de As New DirectoryServices.DirectoryEntry("LDAP://civicnet.com")        
        de.Username = "MyServiceAccount"
        de.Password = "MyServicePassword"
        de.AuthenticationType = DirectoryServices.AuthenticationTypes.Secure
        Dim deSearch As New DirectoryServices.DirectorySearcher(de)
        Dim groupname As String = "Health Department" '<---group you wish to load
        deSearch.Filter = "(&(objectClass=group) (cn=" + groupname + "))"
        Dim results As DirectoryServices.SearchResultCollection = deSearch.FindAll()
        Dim result As DirectoryServices.SearchResult
        If (results.Count > 0) Then
            Dim userNames As New ArrayList
            For Each result In results
                For Each member As String In result.Properties("member")
                    Response.Write(member)
                    userNames.Add(member)
                Next
            Next
        End If
    End Sub

I think it is getting close, but there are no items displayed when I call this.  I have double checked the groupname, and have even tried other groups with no luck.  Any ideas?
0
 
LVL 1

Author Comment

by:jayh99
ID: 21793239
It looks like it is the filter that is causing the problems.  If I comment out that line of code, I can display all members in the AD.  And if I add an if/then looking for member.Contains("Health Department") it displays all the members of that group.
0
 
LVL 1

Author Comment

by:jayh99
ID: 21793276
I also just noticed that Health Department is an organizational unit, not a common name, so that is probably part of the reason the filter is not working correctly.
0
 
LVL 14

Expert Comment

by:Dustin Hopkins
ID: 21793344
I'm getting confused as to what you are trying to accomplish. Aren't you trying to get all members of that group? Also is health department just an ou or is it also a group?
0
 
LVL 1

Author Comment

by:jayh99
ID: 21793387
I am wanting all members of health department. I think I worded my question wrong above because I didn't realize until this morning that it is an ou rather than a group.  

CN=D-Internet_FTP,OU=Users,OU=Health Department,DC=MYDOMAIN,DC=com
0
 
LVL 1

Author Comment

by:jayh99
ID: 21793406
And is it possible to get only the cn for each member of the ou?
0
 
LVL 14

Accepted Solution

by:
Dustin Hopkins earned 150 total points
ID: 21793470
You should just be able to plug that (OU=Health Department,DC=MYDOMAIN,DC=com), or something similiar, in as  as your diretory entry
Dim de As New DirectoryServices.DirectoryEntry("LDAP://OU=Health Department,DC=MYDOMAIN,DC=com")

0
 
LVL 1

Author Closing Comment

by:jayh99
ID: 31467089
Thanks for your help.  I have got it working now.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question