Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3395990
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T04:21:16+00:00 2026-05-18T04:21:16+00:00

I am trying to do a very simple AD query to see if a

  • 0

I am trying to do a very simple AD query to see if a computer is in a group. The following code seems intuitive enough but does not work. The LDAPString is a fully distinguised name for the group that the computer referenced by NetBIOSName is a memberOf.

public bool IsComputerInADGroup(String LDAPString, String NetBIOSName)
{
    using (DirectoryEntry entry = new DirectoryEntry(String.Format(@"LDAP://{0}", LDAPString)))
    using (DirectorySearcher computerSearch = new DirectorySearcher(entry))
    {
        ComputerSearch.Filter = String.Format("(&(objectCategory=computer)(CN={0}))", NetBIOSName);
        SearchResult match = ComputerSearch.FindOne();

        if (match != null)
        {
            return true;
        }
    }

    return false;
}

Can someone please explain why this is incorrect and what the correct/fastest way to to perform this search is.

Thanks
P

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T04:21:17+00:00Added an answer on May 18, 2026 at 4:21 am

    Your basic assumption is wrong – a computer (or user) cannot be in a group implying “containment” inside a group; a user or computer is only inside an OU.

    A user or computer can be member of any number of groups – but you need to check this against the member property of the group (or the memberOf attribute of the element that is a member of that group).

    So the easiest way, really, is to

    • bind to the object in question
    • refresh its property cache to get the latest entries in memberOf
    • enumerate of its memberOf entries and see if the group you’re looking for is present

    Something like:

     public static bool IsAccountMemberOfGroup(string account, string group)
     {
        bool found = false;
    
        using (DirectoryEntry entry = new DirectoryEntry(account))
        {
            entry.RefreshCache(new string[] { "memberOf" });
    
            foreach (string memberOf in entry.Properties["memberOf"])
            {
               if (string.Compare(memberOf, group, true) == 0)
               {
                  found = true;
                  break;
               }
            }
        }
    
        return found;
     }
    

    Call this like so:

    bool isMemberOf = 
         IsAccountMemberOfGroup("LDAP://cn=YourComputer,dc=Corp,dc=com",
                                "CN=yourGroupInQuestion,OU=SomeOU,dc=corp,dc=com");
    

    and you should be fine.

    Update: if you’re on .NET 3.5, you could also use the new System.DirectoryServices.AccountManagement namespace and LINQ to make things even easier:

    public static bool IsAccountMemberOfGroup2(PrincipalContext ctx, string account, string groupName)
    {
       bool found = false; 
       GroupPrincipal group = GroupPrincipal.FindByIdentity(ctx, groupName);
    
       if (group != null)
       {
          found = group.GetMembers()
                     .Any(m => string.Compare(m.DistinguishedName, account, true) == 0);
       }
    
       return found;
    }
    

    and call this:

    // establish default domain context    
    PrincipalContext domain = new PrincipalContext(ContextType.Domain);
    
    // call your function
    bool isMemberOf = 
       IsAccountMemberOfGroup2(domain, 
                               "cn=YourComputer,dc=Corp,dc=com",
                               "CN=yourGroupInQuestion,OU=SomeOU,dc=corp,dc=com");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying something very simple, but for some reason it does not work.
i am trying to compile this very simple piece of code class myList {
What I want to do is very simple but I'm trying to find the
Im trying to get to run a very simple example in liquibase 1.9.5 (see
A coworker and I are trying to fine tune a very simple query. He
Below I have a very simple example of what I'm trying to do. I
I'm trying to do a very simple INSERT using VB.NET. For some reason I'm
I'm trying to add a very simple action to the context menu of Eclipse:
I'm trying to do a very simple button that changes color based on mouseover,
I am trying to do some very simple form validation checking for null or

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.