Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Query to Active Directory

Reply
Thread Tools

Query to Active Directory

 
 
greg.hart@alpinebiomed.com
Guest
Posts: n/a
 
      03-18-2009
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=p erson)
(objectClass=user)(name=*)(telephoneNumber=*)(!use rAccountControl:
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 />");

}
 
Reply With Quote
 
 
 
 
Alexey Smirnov
Guest
Posts: n/a
 
      03-19-2009
On Mar 18, 9:47*pm, (E-Mail Removed) 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=p erson)
> (objectClass=user)(name=*)(telephoneNumber=*)(!use rAccountControl:
> 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/libr...lsearcher.aspx
http://msdn.microsoft.com/en-us/magazine/cc135979.aspx (Finding
Matches)

Hope this helps
 
Reply With Quote
 
 
 
 
greg.hart@alpinebiomed.com
Guest
Posts: n/a
 
      03-19-2009
On Mar 19, 2:16*am, Alexey Smirnov <(E-Mail Removed)> wrote:
> On Mar 18, 9:47*pm, (E-Mail Removed) 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=p erson)
> > (objectClass=user)(name=*)(telephoneNumber=*)(!use rAccountControl:
> > 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/libr...9.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 />");

}
 
Reply With Quote
 
Alexey Smirnov
Guest
Posts: n/a
 
      03-20-2009
On Mar 19, 4:58*pm, (E-Mail Removed) wrote:
> On Mar 19, 2:16*am, Alexey Smirnov <(E-Mail Removed)> wrote:
>
>
>
> > On Mar 18, 9:47*pm, (E-Mail Removed) 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=p erson)
> > > (objectClass=user)(name=*)(telephoneNumber=*)(!use rAccountControl:
> > > 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/libr...yservices.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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
active directory query using directorysearcher =?Utf-8?B?VGFzaA==?= ASP .Net 1 07-19-2005 05:41 PM
Active Directory Query Arvind P Rangan ASP .Net 1 02-24-2004 09:27 AM
Query Active Directory from ASP.NET Andrew Connell ASP .Net 0 01-16-2004 08:11 PM
NTLM credentials -> query Active Directory -> email address Jim Adams ASP .Net 1 01-07-2004 09:48 PM
Active Directory using LDAP query Marty Underwood ASP .Net 0 11-27-2003 02:00 AM



Advertisments