c# - Single Level Search with PrincipalSearcher -


i started learning c# due requirements @ work, trying write method query specific ou in active directory, , ou, no sub-ous. how method looks:

public list<string> getallactiveusers() {     principalcontext oprincipalcontext = getprincipalcontext();     userprincipal ouserprincipal = new userprincipal(oprincipalcontext)      {          enabled = true      };     principalsearcher oprincipalsearcher = new principalsearcher(ouserprincipal);      list<string> allusers = new list<string>();      foreach (var found in oprincipalsearcher.findall())     {         allusers.add(found.displayname.tostring());     }     allusers.sort();     return allusers; } 

the method now, pull user enabled user accounts, problem pulls accounts in subous, not desireable. have googled quite time, no real answer question, novice @ best, if there suggestion change code, please show me final method like.

any , appreciated.

thanks!

update: apparently didn't google search enough, took whack @ it, came across suggestion using getunderlyingsearcher() work, still had no idead how use it. additional research yielded needed, here's updated method:

public list<string> getallactiveusers()     {         principalcontext oprincipalcontext = getprincipalcontext();         userprincipal ouserprincipal = new userprincipal(oprincipalcontext) { enabled = true };         principalsearcher oprincipalsearcher = new principalsearcher(ouserprincipal);         //setting search scope going down directorysearcher itself, it's not possible set         //via principalsearcher directly         ((directorysearcher)oprincipalsearcher.getunderlyingsearcher()).searchscope = searchscope.onelevel;          list<string> allusers = new list<string>();          foreach (var found in oprincipalsearcher.findall())         {             allusers.add(found.displayname.tostring());         }         allusers.sort();         return allusers;     } 

thanks suggestions!

this bit of code resolved issue:

((directorysearcher)oprincipalsearcher.getunderlyingsearcher()).searchscope = searchscope.onelevel; 

with final method looking this:

public list<string> getallactiveusers()     {         principalcontext oprincipalcontext = getprincipalcontext();         userprincipal ouserprincipal = new userprincipal(oprincipalcontext) { enabled = true };         principalsearcher oprincipalsearcher = new principalsearcher(ouserprincipal);         //setting search scope going down directorysearcher itself, it's not possible set         //via principalsearcher directly         ((directorysearcher)oprincipalsearcher.getunderlyingsearcher()).searchscope = searchscope.onelevel;          list<string> allusers = new list<string>();          foreach (var found in oprincipalsearcher.findall())         {             allusers.add(found.displayname.tostring());         }         allusers.sort();         return allusers;     } 

Comments