ActiveDirectory Search Help

Posted on 2005-05-05
Last Modified: 2012-05-05
i am writing a ActiveDirectory DirectorySearcher with Filter.
I need to be able to find out whether a user belongs to a particular group.

I found that I can find all the groups the user belongs to by using the following:

DirectoryEntry dn = new DirectoryEntry("LDAP://DOMAIN");
                        string userName = "SOMEONE";
                        string filter = string.Format("(&(objectCategory=person)(objectClass=user)(sAMAccountName={0})(sn=chen))", userName);
                        DirectorySearcher mySearch = new DirectorySearcher(dn, filter);
                        SearchResult mySearchResult = mySearch.FindOne();

But what I really want to find out is whether this user belongs to group "A" (or anything I specify)

I am thinking of using regular expression to parse all the groups the user belongs to then if there is a match return true. However, I would like a cleaner way of doing this.

Right now the hierachy of the memberOf is as following:

CN=GroupName, OU=Groups, OU=DomainName, DC=Domain, DC=com

Is there a way I can specify a sub directory of memberOf in my filter string??
I have tried (memberof=CN=GroupName) and that failed.

Thanks for any help you can give
Question by:lordiano
    1 Comment
    LVL 20

    Accepted Solution

    You need to specify full-qualified distinguishedName of the group object in the query filter, which is something like this


    But since memberOf is not indexable, the search would be pretty slow if it returns lots of users.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Article by: Ivo
    Anonymous Types in C# by Ivo Stoykov Anonymous Types are useful when  we do not need to follow usual work-flow -- creating object of some type, assign some read-only values and then doing something with them. Instead we can encapsulate this read…
    Article by: Ivo
    C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    779 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

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now