Click here to Skip to main content
15,887,821 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I was trying to find some example to retrieve user from Active Directory, based on Date Range.

For Eg: if i give a date like 1/01/2011 to 31/01/2011 in my windows application.

I wanna retrieve the user info , created on these date ranges.

For Eg: User Bob Created on 15/01/2001,then i have to retrieve that user name.

but am not sure what's the "Search Filter" for that.

Any help appreciated
Posted

can you find all users and then use an IF statement to display the user created within these dates?

if(user.properties["whenCreated"].value > StartDate && user.properties["whenCreated"].value < EndData)
{
user.properties["whenCreated"].value.ToString()
}
 
Share this answer
 
Comments
shan1395 24-Feb-11 6:30am    
Awesome Philip,that was an amazing idea, i was keep on thinking to put condition in Search filter.

This logic works and thank you so much
Philip Lane 24-Feb-11 6:32am    
Glad i could help
shan1395 24-Feb-11 6:37am    
please Correct me,if am not followed your logic in the below code

using System;
using System.Text;
using System.DirectoryServices;

namespace activeDirectoryLdapExamples
{
class Program
{
static void Main(string[] args)
{
Console.Write("Enter property: ");
String property = Console.ReadLine();

try
{
DirectoryEntry myLdapConnection = createDirectoryEntry();

DirectorySearcher search = new DirectorySearcher(myLdapConnection);
search.PropertiesToLoad.Add("cn");
search.PropertiesToLoad.Add(property);

SearchResultCollection allUsers = search.FindAll();

foreach(SearchResult result in allUsers)
{
if(user.properties["whenCreated"].value > StartDate && user.properties["whenCreated"].value < EndData)
{
user.properties["whenCreated"].value.ToString()
}

}
}

catch (Exception e)
{
Console.WriteLine("Exception caught:\n\n" + e.ToString());
}
}

static DirectoryEntry createDirectoryEntry()
{
// create and return new LDAP connection with desired settings

DirectoryEntry ldapConnection = new DirectoryEntry("rizzo.leeds-art.ac.uk");
ldapConnection.Path = "LDAP://OU=staffusers,DC=leeds-art,DC=ac,DC=uk";
ldapConnection.AuthenticationType = AuthenticationTypes.Secure;
return ldapConnection;
}
}
}
The logic is good, you just need to adapt the example i gave to fit your programme:
you'll have to create 2 DateTime values for startDate and endDate.
so within the foreach loop you'll have something that look like the below:


<pre lang="xml">DirectoryEntry user = new DirectoryEntry("LDAP://" + result.GetDirectoryEntry().Properties["distinguishedName"].Value.ToString());

               if(user.Properties["whenCreated"].Value > startDate && user.Properties["whenCreated"].Value < endDate)
               {
                   Console.WriteLine(user.Properties["cn"].Value.ToString() + " - " + user.Properties["whenCreated"].Value.ToString());
               }

 
Share this answer
 
Comments
Bevin wc 12-Oct-17 6:02am    
I am trying like this: https://www.codeproject.com/Questions/1210193/Search-users-by-filtering-using-whencreated-in-act
Please help ...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900