Query to Active Directory

Discussion in 'ASP .Net' started by greg.hart@alpinebiomed.com, Mar 18, 2009.

  1. Guest

    Hello all,

    I just wanted to see if someone can see something I am missing. This
    code works, but does not seem to apply the filter. It pulls back all
    the users whereas I am just looking for the ones that have name and
    telephone populated and are not disabled. When I run this filter
    directly inside the AD query tool I get the expected results. Just
    doesn't seem to work right from my code. Thanks.

    PrincipalContext ctx = new PrincipalContext(ContextType.Domain,
    <Domain>,<UserName>,<Password>);
    UserPrincipal u = new UserPrincipal(ctx);
    u.AdvancedSearchFilter.Equals("(&(objectCategory=person)
    (objectClass=user)(name=*)(telephoneNumber=*)(!userAccountControl:
    1.2.840.113556.1.4.803:=2))");

    PrincipalSearcher ps = new PrincipalSearcher(u);
    PrincipalSearchResult<Principal> fr = ps.FindAll();

    foreach (UserPrincipal usr in fr)
    {

    Response.Write(usr.DisplayName + "&nbsp;&nbsp;" +
    usr.VoiceTelephoneNumber + "<br />");

    }
    , Mar 18, 2009
    #1
    1. Advertising

  2. On Mar 18, 9:47 pm, wrote:
    > Hello all,
    >
    > I just wanted to see if someone can see something I am missing.  This
    > code works, but does not seem to apply the filter.  It pulls back all
    > the users whereas I am just looking for the ones that have name and
    > telephone populated and are not disabled.  When I run this filter
    > directly inside the AD query tool I get the expected results.  Just
    > doesn't seem to work right from my code.  Thanks.
    >
    >  PrincipalContext ctx = new PrincipalContext(ContextType.Domain,
    > <Domain>,<UserName>,<Password>);
    >             UserPrincipal u = new UserPrincipal(ctx);
    >             u.AdvancedSearchFilter.Equals("(&(objectCategory=person)
    > (objectClass=user)(name=*)(telephoneNumber=*)(!userAccountControl:
    > 1.2.840.113556.1.4.803:=2))");
    >
    >             PrincipalSearcher ps = new PrincipalSearcher(u);
    >             PrincipalSearchResult<Principal> fr = ps.FindAll();
    >
    >             foreach (UserPrincipal usr in fr)
    >             {
    >
    >                 Response.Write(usr.DisplayName + "&nbsp;&nbsp;" +
    > usr.VoiceTelephoneNumber + "<br />");
    >
    >             }


    You can't use AdvancedSearchFilter in that way. First of all, Equals
    function does nothing, but returns a boolean result. Then the
    PrincipalSearcher class required to set its properties to create a
    searcher and you can't use LDAP queries for setting up search filters
    (objectClass=user), etc to do this.

    Instead of line with u.AdvancedSearchFilter.Equals....

    use:

    u.Enabled = false;
    u.Name = "*";

    and you should get the correct result.

    Read more about PrincipalSearcher

    http://msdn.microsoft.com/en-us/lib...ices.accountmanagement.principalsearcher.aspx
    http://msdn.microsoft.com/en-us/magazine/cc135979.aspx (Finding
    Matches)

    Hope this helps
    Alexey Smirnov, Mar 19, 2009
    #2
    1. Advertising

  3. Guest

    On Mar 19, 2:16 am, Alexey Smirnov <> wrote:
    > On Mar 18, 9:47 pm, wrote:
    >
    >
    >
    >
    >
    > > Hello all,

    >
    > > I just wanted to see if someone can see something I am missing.  This
    > > code works, but does not seem to apply the filter.  It pulls back all
    > > the users whereas I am just looking for the ones that have name and
    > > telephone populated and are not disabled.  When I run this filter
    > > directly inside the AD query tool I get the expected results.  Just
    > > doesn't seem to work right from my code.  Thanks.

    >
    > >  PrincipalContext ctx = new PrincipalContext(ContextType.Domain,
    > > <Domain>,<UserName>,<Password>);
    > >             UserPrincipal u = new UserPrincipal(ctx);
    > >             u.AdvancedSearchFilter.Equals("(&(objectCategory=person)
    > > (objectClass=user)(name=*)(telephoneNumber=*)(!userAccountControl:
    > > 1.2.840.113556.1.4.803:=2))");

    >
    > >             PrincipalSearcher ps = new PrincipalSearcher(u);
    > >             PrincipalSearchResult<Principal> fr = ps.FindAll();

    >
    > >             foreach (UserPrincipal usr in fr)
    > >             {

    >
    > >                 Response.Write(usr.DisplayName + "&nbsp;&nbsp;" +
    > > usr.VoiceTelephoneNumber + "<br />");

    >
    > >             }

    >
    > You can't use AdvancedSearchFilter in that way. First of all, Equals
    > function does nothing, but returns a boolean result. Then the
    > PrincipalSearcher class required to set its properties to create a
    > searcher and you can't use LDAP queries for setting up search filters
    > (objectClass=user), etc  to do this.
    >
    > Instead of line with u.AdvancedSearchFilter.Equals....
    >
    > use:
    >
    > u.Enabled = false;
    > u.Name = "*";
    >
    > and you should get the correct result.
    >
    > Read more about PrincipalSearcher
    >
    > http://msdn.microsoft.com/en-us/lib...soft.com/en-us/magazine/cc135979.aspx(Finding
    > Matches)
    >
    > Hope this helps- Hide quoted text -
    >
    > - Show quoted text -


    Thank you Alexy!

    I was testing this inside a SharePoint aspx page and did have the
    Intellisense for the UserPrincipal. Here is the code I used, maybe it
    can help someone. It returns the users from AD that are enabled, have
    a populated first name, and have a phone number.


    PrincipalContext ctx = new PrincipalContext(ContextType.Domain,
    <Domain>,<UserName>,<Password>);
    UserPrincipal u = new UserPrincipal(ctx);
    u.Enabled = true;
    u.Name = "*";
    u.VoiceTelephoneNumber = "*";

    PrincipalSearcher ps = new PrincipalSearcher(u);
    PrincipalSearchResult<Principal> fr = ps.FindAll();

    foreach (UserPrincipal usr in fr)
    {

    Response.Write(usr.DisplayName + "&nbsp;&nbsp;" +
    usr.VoiceTelephoneNumber + "<br />");

    }
    , Mar 19, 2009
    #3
  4. On Mar 19, 4:58 pm, wrote:
    > On Mar 19, 2:16 am, Alexey Smirnov <> wrote:
    >
    >
    >
    > > On Mar 18, 9:47 pm, wrote:

    >
    > > > Hello all,

    >
    > > > I just wanted to see if someone can see something I am missing.  This
    > > > code works, but does not seem to apply the filter.  It pulls back all
    > > > the users whereas I am just looking for the ones that have name and
    > > > telephone populated and are not disabled.  When I run this filter
    > > > directly inside the AD query tool I get the expected results.  Just
    > > > doesn't seem to work right from my code.  Thanks.

    >
    > > >  PrincipalContext ctx = new PrincipalContext(ContextType.Domain,
    > > > <Domain>,<UserName>,<Password>);
    > > >             UserPrincipal u = new UserPrincipal(ctx);
    > > >             u.AdvancedSearchFilter.Equals("(&(objectCategory=person)
    > > > (objectClass=user)(name=*)(telephoneNumber=*)(!userAccountControl:
    > > > 1.2.840.113556.1.4.803:=2))");

    >
    > > >             PrincipalSearcher ps = new PrincipalSearcher(u);
    > > >             PrincipalSearchResult<Principal> fr = ps.FindAll();

    >
    > > >             foreach (UserPrincipal usr in fr)
    > > >             {

    >
    > > >                 Response.Write(usr.DisplayName + "&nbsp;&nbsp;" +
    > > > usr.VoiceTelephoneNumber + "<br />");

    >
    > > >             }

    >
    > > You can't use AdvancedSearchFilter in that way. First of all, Equals
    > > function does nothing, but returns a boolean result. Then the
    > > PrincipalSearcher class required to set its properties to create a
    > > searcher and you can't use LDAP queries for setting up search filters
    > > (objectClass=user), etc  to do this.

    >
    > > Instead of line with u.AdvancedSearchFilter.Equals....

    >
    > > use:

    >
    > > u.Enabled = false;
    > > u.Name = "*";

    >
    > > and you should get the correct result.

    >
    > > Read more about PrincipalSearcher

    >
    > >http://msdn.microsoft.com/en-us/library/system.directoryservices.acco...
    > > Matches)

    >
    > > Hope this helps- Hide quoted text -

    >
    > > - Show quoted text -

    >
    > Thank you Alexy!
    >
    > I was testing this inside a SharePoint aspx page and did have the
    > Intellisense for the UserPrincipal.  Here is the code I used, maybe it
    > can help someone.  It returns the users from AD that are enabled, have
    > a populated first name, and have a phone number.
    >
    >    PrincipalContext ctx = new PrincipalContext(ContextType.Domain,
    > <Domain>,<UserName>,<Password>);
    >             UserPrincipal u = new UserPrincipal(ctx);
    >             u.Enabled = true;
    >             u.Name = "*";
    >             u.VoiceTelephoneNumber = "*";
    >
    >             PrincipalSearcher ps = new PrincipalSearcher(u);
    >             PrincipalSearchResult<Principal> fr = ps.FindAll();
    >
    >             foreach (UserPrincipal usr in fr)
    >             {
    >
    >                 Response.Write(usr.DisplayName + "&nbsp;&nbsp;" +
    > usr.VoiceTelephoneNumber + "<br />");
    >
    >             }


    Glad, it works for you
    Alexey Smirnov, Mar 20, 2009
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Marty Underwood

    Active Directory using LDAP query

    Marty Underwood, Nov 27, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    605
    Marty Underwood
    Nov 27, 2003
  2. Jim Adams
    Replies:
    1
    Views:
    3,693
    Tim Heuer
    Jan 7, 2004
  3. Andrew Connell

    Query Active Directory from ASP.NET

    Andrew Connell, Jan 16, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    687
    Andrew Connell
    Jan 16, 2004
  4. Arvind P Rangan

    Active Directory Query

    Arvind P Rangan, Feb 24, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    469
    Martin Dechev
    Feb 24, 2004
  5. =?Utf-8?B?VGFzaA==?=

    active directory query using directorysearcher

    =?Utf-8?B?VGFzaA==?=, Jul 19, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    707
    =?Utf-8?B?RWx0b24gVw==?=
    Jul 19, 2005
Loading...

Share This Page