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

  • SEARCH
  • Home
  • 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 3790732
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T12:21:38+00:00 2026-05-19T12:21:38+00:00

I have a requirement to search for a user in an OU. My solution

  • 0

I have a requirement to search for a user in an OU. My solution so far is as follows :-

// s = "ou=myou1,ou=myou2,ou=muou3,dc=myad,dc=com" & t = "myad.com"

PrincipalContext context = new PrincipalContext(ContextType.Domain, t, s);
UserPrincipal user = UserPrincipal.FindByIdentity(context, "boborwhoever");
if (user != null) found him!

(please pardon the obvious pseudocode but you get the picture)

The issue I am having is that although my userPrincipal user is populated and I find ‘bob’ if he is in ‘myou3’ I can change s to “ou=myou1,dc=myad,dc=com” and still find ‘bob’. So it seems that the UserPrincipal.FindByIdentity also check in sub OU’s.

How do I get it to just check the OU stated? Or maybe I’m miles out and should be doing the whole thing in a better way 🙂

Thanks
Steve

  • 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-19T12:21:38+00:00Added an answer on May 19, 2026 at 12:21 pm

    This is how i would do this

    using System.DirectoryServices
    
    DirectoryEntry de = new DirectoryEntry();
    de.Path = "LDAP://**Your connection string here**";
    de.AuthenticationType = AuthenticationTypes.Secure;
    
    DirectorySearcher search = new DirectorySearcher(de);
    search.Filter = "(SAMAccountName=" + account + ")";
    
    //What properties do we want to return?
    search.PropertiesToLoad.Add("displayName");
    search.PropertiesToLoad.Add("mail");
    
    search.SearchScope = SearchScope.OneLevel //this makes it only search the specified level
    
    SearchResult result = search.FindOne();
    
    if (result != null)
    {
         //Get Him!    }
    else
    {
        //Not Found
    }
    

    have used this in sharepoint workflows and it functioned fine.

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

Sidebar

Related Questions

I have a requirement which lets the user search for properties using friendly urls.
I am working on Android Application. Now I have requirement of search the entered
I have a search textbox on silverlight app where user types in search term
I have a requirement to produce a list of possible duplicates before a user
In my iPhone app, I have requirement to search the SQLite database. I will
I have requirement where some times I would like to load children as well
I have requirement to disable copy/paste/cut operations on a textbox. For this purpose I
I have requirement, wherein I have to calculate totals for cash and credit cards
I have requirement of specifying web part connections in onet.xml. So when site is
I have requirement like, suppose I have a 'property' table which has 'ListingKey' field

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.