Solved

Query Active Directory

Posted on 2004-09-15
5
606 Views
Last Modified: 2008-05-30
I need to query Active Directory using ASP.NET / VB.NET to find all groups in the AD that a user is a member of.  I can use this code to return some information.  I want to return the list of groups as an array that I can then search through to find specific Groups that belong to that user.

        Dim entry As New DirectoryServices.DirectoryEntry("LDAP://int.domain.net")
        Dim mySearcher As New System.DirectoryServices.DirectorySearcher(entry)
        Dim result As System.DirectoryServices.SearchResult
        mySearcher.Filter = ("(anr= jdoe)")

        For Each result In mySearcher.FindAll()
            Response.Write(result.GetDirectoryEntry().Path)
        Next

Thanks for the help.
0
Comment
Question by:imsolost
  • 3
  • 2
5 Comments
 
LVL 5

Expert Comment

by:jmacmicking
ID: 12074065
Why not open the user's account in AD directly?  For instance:

Set oUser = GetObject("LDAP://cn=Doe John, ou=IS, dc=int, dc=domain, dc=net")
For Each oGroup in oUser.Groups
    Response.Write(oGroup.Path)
Next

That's assuming you have the LDAP name of the user; if not you can use the WinNT provider (it works just as well for getting group names).  The line looks like this:

Set oUser = GetObject("WinNT://domain/" & username)

This should be a lot faster then user the search function.  If you need help putting these in an array as well let me know.
0
 
LVL 7

Author Comment

by:imsolost
ID: 12074512
is that classic asp or asp.net?

When I try this I get the following error.
**********************************  ERROR ***************************
Public member 'Path' on type '_ComObject' not found.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.MissingMemberException: Public member 'Path' on type '_ComObject' not found.

********************************* END ERROR *************************

It gives that error on the line
response.Write(oGroup.Path)

Any ideas?
Thanks
0
 
LVL 5

Accepted Solution

by:
jmacmicking earned 500 total points
ID: 12074957
I'm sorry; that should be oGroup.Name; don't know why I typed .path.
0
 
LVL 7

Author Comment

by:imsolost
ID: 12075438
Ok the code works exactly as I wanted.  Thank you.  

I know you have them as objects but do oUser and oGroup have a defined data type?

Thanks
0
 
LVL 5

Expert Comment

by:jmacmicking
ID: 12076376
IADsUser and IADsGroup, respectively.  Part of the ActiveDS type library.  Out of curiousity I did a quick search to find the equivilant in .NET and found this tutorial on MSDN, http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sds/sds/enumerating_user_memberships.asp, that does exactly what you want user the DirectoryEntry object of .NET.
0

Featured Post

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

Question has a verified solution.

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

Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
This article describes my battle tested process for setting up delegation. I use this process anywhere that I need to setup delegation. In the article I will show how it applies to Active Directory
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

828 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