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 509567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:01:27+00:00 2026-05-13T07:01:27+00:00

I have some code that queries Active Directory to verify user existence. I am

  • 0

I have some code that queries Active Directory to verify user existence. I am trying to verify a long list of about 1300 ids. I’ve tried several methods to verify if a user account (LINQ to AD, DirectorySearcher (with and without a parent DirectoryEntry) and also a DirectoryEntry that links to the WinNT:// path). Every time it will come back and say that several users do not exist. If I hardcode their userids in the code and execute for individually, it validates existence. If I try and do it in a foreach loop, I get several false negatives.

Here’s the code I am using right now..

static string[] userIDs = new string[] "user1","user2","user3","user4","user5","user6","user7","user8"...,"user1300"};

List<string> nonExistingUsers = new List<string>();
List<string> ExistingUsers = new List<string>();
foreach (string s in userIDs)
{
 DirectorySearcher search = new DirectorySearcher();
 search.Filter = String.Format("(SAMAccountName={0})", s);
 search.PropertiesToLoad.Add("cn");
 DirectorySearcher ds = new DirectorySearcher(de, "(&(objectClass=user)(cn=" + s + "))", new string[] { "Name" }, SearchScope.Subtree);
 SearchResultCollection resultCollection = ds.FindAll();
 SearchResult result = search.FindOne();
 if (result != null)
  ExistingUsers.Add(s);
 else
  nonExistingUsers.Add(s);
}

Any suggestions or reasons why I am getting the false negatives?

  • 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-13T07:01:27+00:00Added an answer on May 13, 2026 at 7:01 am

    Couple of things:

    • first of all, try using the “anr=” (ambiguous name resolution) in your LDAP filter – it searches several name-related attributes and make searching easier. The UserID might not be part of the actual “common name” (CN=user1)

    • secondly, use the objectCategory instead of objectClass – the objectCategory is single-valued and indexed and thus a fair bit faster on searches

    • thirdly: why are you first calling .FindAll() and then .FindOne() on the next line? Doesn’t seem really necessary at all….

    • WinNT:// really is only for backward compatibility and if you need to deal with local computer accounts – try to avoid it whenever possible, it also exposes a lot less properties than LDAP

    Here’s my code I’d write:

    static string[] userIDs = new string[] "user1","user2","user3","user4","user5","user6","user7","user8"...,"user1300"};
    
    DirectoryEntry searchRoot = new DirectoryEntry("LDAP://cn=Users,dc=YourComp,dc=com");
    
    List<string> nonExistingUsers = new List<string>();
    List<string> ExistingUsers = new List<string>();
    
    foreach (string s in userIDs)
    {
       DirectorySearcher search = new DirectorySearcher(searchRoot);
    
       search.SearchScope = SearchScope.Subtree;
       search.Filter = string.Format("(&(objectCategory=person)(anr={0}))", s);
    
       SearchResultCollection resultCollection = ds.FindAll();
    
       if(resultCollection != null && resultCollection.Count > 0)
          ExistingUsers.Add(s);
       else
          nonExistingUsers.Add(s);
    }
    

    Does that work in your scenario??

    Also, if you’re using .NET 3.5 or higher, things got a lot easier – see:

    Managing Directory Security Principals in the .NET Framework 3.5

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 309k
  • Answers 309k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I used the verify command and I cannot find some… May 13, 2026 at 9:53 pm
  • Editorial Team
    Editorial Team added an answer Please refer this. You can compile multiple schema at once… May 13, 2026 at 9:53 pm
  • Editorial Team
    Editorial Team added an answer Try this: EXEC Dbo.CREATE_A_PEACEFULL_WORL 'A',2 ,SELECT CONVERT(varchar(10),@yourSmallDate,104) +' '+CONVERT(varchar(8),@yourSmallDate,108) May 13, 2026 at 9:53 pm

Related Questions

In a C++ application that can use just about any relational database, what would
I have created some extra functionality on my Linq-to-SQL classes to make things easier
I have a case where I need to translate (lookup) several values from the
Can I have one query to check if an entry is in a mysql
We have a couple of mirrored SQL Server databases. My first problem - the

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.