HOWTO filter away disabled users when searching in AD using DirectorySearcher in .NET??

HOWTO filter away disabled users when searching in AD using DirectorySearcher in .NET??

Post by Martin Emanuelsso » Wed, 10 Jul 2002 17:27:20



Hi all!

I have an ASP.NET  application written in C# where our employees can update
their own AD-information.

When we retrieve information about our employees we search for all users in
a specific OU, this retrieves both enabled and disabled users and I was
wondering if there is some way that I can only get the enabled ones?

A short snippet of the code looks like this:

================================
objDirEnt = new DirectoryEntry(strLDAP, strUserName, strPassword);

DirectorySearcher mySearcher = new DirectorySearcher(objDirEnt);

mySearcher.Filter = ("(objectClass=User)");

mySearcher.PropertiesToLoad.Add("givenname");

mySearcher.PropertiesToLoad.Add("sn");

mySearcher.PropertiesToLoad.Add("displayName");

mySearcher.PropertiesToLoad.Add("adspath");

mySearcher.PropertiesToLoad.Add("mail");

mySearcher.PropertiesToLoad.Add("l");

mySearcher.PropertiesToLoad.Add("facsimileTelephoneNumber");

mySearcher.PropertiesToLoad.Add("ipPhone");

mySearcher.PropertiesToLoad.Add("mobile");

mySearcher.PropertiesToLoad.Add("telephoneNumber");

mySearcher.PropertiesToLoad.Add("pager");

mySearcher.PropertiesToLoad.Add("cn");

DataRow newRow;

foreach(SearchResult resEnt in mySearcher.FindAll())

================================

Does anyone have any ideas about how I can do this?

Best regards

Martin Emanuelsson

Gothenburg, Sweden

 
 
 

HOWTO filter away disabled users when searching in AD using DirectorySearcher in .NET??

Post by Kurt Jense » Wed, 10 Jul 2002 17:35:26


Hi Martin,

Why not just make an If ..then.. statement when you look up the users to see
if the account is disabled - If so, then skip it..

I hope you can use the following code example

Dim Domain
Dim UserAccount
Domain = GetObject("WinNT://<ServerName>")
Dim filter As System.Object() = {"user"}
Domain.Filter = filter
For Each UserAccount In Domain
    If UserAccount.AccountDisabled = True Then
                ..................
    End If
Next

/Kurt


Quote:> Hi all!

> I have an ASP.NET  application written in C# where our employees can
update
> their own AD-information.

> When we retrieve information about our employees we search for all users
in
> a specific OU, this retrieves both enabled and disabled users and I was
> wondering if there is some way that I can only get the enabled ones?

> A short snippet of the code looks like this:

> ================================
> objDirEnt = new DirectoryEntry(strLDAP, strUserName, strPassword);

> DirectorySearcher mySearcher = new DirectorySearcher(objDirEnt);

> mySearcher.Filter = ("(objectClass=User)");

> mySearcher.PropertiesToLoad.Add("givenname");

> mySearcher.PropertiesToLoad.Add("sn");

> mySearcher.PropertiesToLoad.Add("displayName");

> mySearcher.PropertiesToLoad.Add("adspath");

> mySearcher.PropertiesToLoad.Add("mail");

> mySearcher.PropertiesToLoad.Add("l");

> mySearcher.PropertiesToLoad.Add("facsimileTelephoneNumber");

> mySearcher.PropertiesToLoad.Add("ipPhone");

> mySearcher.PropertiesToLoad.Add("mobile");

> mySearcher.PropertiesToLoad.Add("telephoneNumber");

> mySearcher.PropertiesToLoad.Add("pager");

> mySearcher.PropertiesToLoad.Add("cn");

> DataRow newRow;

> foreach(SearchResult resEnt in mySearcher.FindAll())

> ================================

> Does anyone have any ideas about how I can do this?

> Best regards

> Martin Emanuelsson

> Gothenburg, Sweden


 
 
 

HOWTO filter away disabled users when searching in AD using DirectorySearcher in .NET??

Post by Martin Emanuelsso » Wed, 10 Jul 2002 22:42:35


Hi Kurt!

Do you know how I can access this value using .NET and
System.DirectoryServices?

/Martin


> Hi Martin,

> Why not just make an If ..then.. statement when you look up the users to
see
> if the account is disabled - If so, then skip it..

> I hope you can use the following code example

> Dim Domain
> Dim UserAccount
> Domain = GetObject("WinNT://<ServerName>")
> Dim filter As System.Object() = {"user"}
> Domain.Filter = filter
> For Each UserAccount In Domain
>     If UserAccount.AccountDisabled = True Then
>                 ..................
>     End If
> Next

> /Kurt



> > Hi all!

> > I have an ASP.NET  application written in C# where our employees can
> update
> > their own AD-information.

> > When we retrieve information about our employees we search for all users
> in
> > a specific OU, this retrieves both enabled and disabled users and I was
> > wondering if there is some way that I can only get the enabled ones?

> > A short snippet of the code looks like this:

> > ================================
> > objDirEnt = new DirectoryEntry(strLDAP, strUserName, strPassword);

> > DirectorySearcher mySearcher = new DirectorySearcher(objDirEnt);

> > mySearcher.Filter = ("(objectClass=User)");

> > mySearcher.PropertiesToLoad.Add("givenname");

> > mySearcher.PropertiesToLoad.Add("sn");

> > mySearcher.PropertiesToLoad.Add("displayName");

> > mySearcher.PropertiesToLoad.Add("adspath");

> > mySearcher.PropertiesToLoad.Add("mail");

> > mySearcher.PropertiesToLoad.Add("l");

> > mySearcher.PropertiesToLoad.Add("facsimileTelephoneNumber");

> > mySearcher.PropertiesToLoad.Add("ipPhone");

> > mySearcher.PropertiesToLoad.Add("mobile");

> > mySearcher.PropertiesToLoad.Add("telephoneNumber");

> > mySearcher.PropertiesToLoad.Add("pager");

> > mySearcher.PropertiesToLoad.Add("cn");

> > DataRow newRow;

> > foreach(SearchResult resEnt in mySearcher.FindAll())

> > ================================

> > Does anyone have any ideas about how I can do this?

> > Best regards

> > Martin Emanuelsson

> > Gothenburg, Sweden

 
 
 

HOWTO filter away disabled users when searching in AD using DirectorySearcher in .NET??

Post by Martin Emanuelsso » Wed, 10 Jul 2002 23:10:57


Nevermind Kurt, I found it out, thanks a lot for your help...

/Martin


> Hi Kurt!

> Do you know how I can access this value using .NET and
> System.DirectoryServices?

> /Martin



> > Hi Martin,

> > Why not just make an If ..then.. statement when you look up the users to
> see
> > if the account is disabled - If so, then skip it..

> > I hope you can use the following code example

> > Dim Domain
> > Dim UserAccount
> > Domain = GetObject("WinNT://<ServerName>")
> > Dim filter As System.Object() = {"user"}
> > Domain.Filter = filter
> > For Each UserAccount In Domain
> >     If UserAccount.AccountDisabled = True Then
> >                 ..................
> >     End If
> > Next

> > /Kurt



> > > Hi all!

> > > I have an ASP.NET  application written in C# where our employees can
> > update
> > > their own AD-information.

> > > When we retrieve information about our employees we search for all
users
> > in
> > > a specific OU, this retrieves both enabled and disabled users and I
was
> > > wondering if there is some way that I can only get the enabled ones?

> > > A short snippet of the code looks like this:

> > > ================================
> > > objDirEnt = new DirectoryEntry(strLDAP, strUserName, strPassword);

> > > DirectorySearcher mySearcher = new DirectorySearcher(objDirEnt);

> > > mySearcher.Filter = ("(objectClass=User)");

> > > mySearcher.PropertiesToLoad.Add("givenname");

> > > mySearcher.PropertiesToLoad.Add("sn");

> > > mySearcher.PropertiesToLoad.Add("displayName");

> > > mySearcher.PropertiesToLoad.Add("adspath");

> > > mySearcher.PropertiesToLoad.Add("mail");

> > > mySearcher.PropertiesToLoad.Add("l");

> > > mySearcher.PropertiesToLoad.Add("facsimileTelephoneNumber");

> > > mySearcher.PropertiesToLoad.Add("ipPhone");

> > > mySearcher.PropertiesToLoad.Add("mobile");

> > > mySearcher.PropertiesToLoad.Add("telephoneNumber");

> > > mySearcher.PropertiesToLoad.Add("pager");

> > > mySearcher.PropertiesToLoad.Add("cn");

> > > DataRow newRow;

> > > foreach(SearchResult resEnt in mySearcher.FindAll())

> > > ================================

> > > Does anyone have any ideas about how I can do this?

> > > Best regards

> > > Martin Emanuelsson

> > > Gothenburg, Sweden

 
 
 

HOWTO filter away disabled users when searching in AD using DirectorySearcher in .NET??

Post by Willy Denoyette [MVP » Tue, 16 Jul 2002 04:05:27


Why not set the samAccountName property to the logon name, and add it to the search filter.

    mySearcher.Filter = "(&(objectClass=user)(samAccountName=" + SamAccount +"))";
   SearchResult resEnt = mySearcher.FindOne();

This will just return a single entry.

Willy.


> Hi all!

> I have an ASP.NET  application written in C# where our employees can update
> their own AD-information.

> When we retrieve information about our employees we search for all users in
> a specific OU, this retrieves both enabled and disabled users and I was
> wondering if there is some way that I can only get the enabled ones?

> A short snippet of the code looks like this:

> ================================
> objDirEnt = new DirectoryEntry(strLDAP, strUserName, strPassword);

> DirectorySearcher mySearcher = new DirectorySearcher(objDirEnt);

> mySearcher.Filter = ("(objectClass=User)");

> mySearcher.PropertiesToLoad.Add("givenname");

> mySearcher.PropertiesToLoad.Add("sn");

> mySearcher.PropertiesToLoad.Add("displayName");

> mySearcher.PropertiesToLoad.Add("adspath");

> mySearcher.PropertiesToLoad.Add("mail");

> mySearcher.PropertiesToLoad.Add("l");

> mySearcher.PropertiesToLoad.Add("facsimileTelephoneNumber");

> mySearcher.PropertiesToLoad.Add("ipPhone");

> mySearcher.PropertiesToLoad.Add("mobile");

> mySearcher.PropertiesToLoad.Add("telephoneNumber");

> mySearcher.PropertiesToLoad.Add("pager");

> mySearcher.PropertiesToLoad.Add("cn");

> DataRow newRow;

> foreach(SearchResult resEnt in mySearcher.FindAll())

> ================================

> Does anyone have any ideas about how I can do this?

> Best regards

> Martin Emanuelsson

> Gothenburg, Sweden

 
 
 

1. .NET and Functoid Add-ins

Anyone have success in implmenting a functoid in .NET.  I wrote a super
simple one that implements the IFunctoid interface and registered as well as
made sure it was in the MapEdit Functoids implemented categories.  When i
view the class through the OLE com obejct viewer and navigate to the mapedit
category the class shows up but doesnt browse correctly.

Also since each functoid is supposed to export a bitmap reource identified
by a Res ID and the fact that .NET managed code doesnt support resource
files.... How did you get around this???

Thanks for any help.

Bryan

2. cc-mode

3. Searching by SID in AD Using .NET Framework

4. Auto file conversion

5. Using ADS SID of individual users to authenticate ADS users

6. Risc PC start-up freeze

7. Phonebook Settings disappear and I have to reinstall INS file

8. Diskeeper's Boot-Time defragmentation problem. Please help :)

9. dial-ins can't access shares

10. RAS dial-ins can't see web

11. sbs console/snap ins

12. issue with client log ins some can some cant any help please

13. Adding Snap-Ins can find Add/Remove option