2010
06.10








I needed to retrieve list of active users with email address from Active Directory. After scratching head, and googling for a while, the following page helped me immensely:

Note: You need to include Active DS COM Library in your project references. Your project references should roughly similar to the picture below.

ad.project

Here are teh codez:

using System;
using System.Collections.Generic;
using System.Text;
using System.DirectoryServices;
using System.DirectoryServices.ActiveDirectory;
using System.IO;

namespace ActiveDirectoryQuery
{
    class Program
    {
        static void Main(string[] args)
        {
            DirectorySearcher ds = new DirectorySearcher();
            ds.SearchRoot = new DirectoryEntry("");
            ds.Filter = "objectCategory=user";
            ds.PropertiesToLoad.Add("name");
            ds.PropertiesToLoad.Add("email");
            ds.PropertyNamesOnly = true;
            ds.Sort = new SortOption("name", SortDirection.Ascending);
            SearchResultCollection results = ds.FindAll();

            //Write output
            StreamWriter writer = new StreamWriter(@"c:\ad.output.txt");        

            foreach (SearchResult sr in results)
            {
                DirectoryEntry entry = sr.GetDirectoryEntry();
                if (entry.Properties["name"].Value != null && entry.Properties["mail"].Value != null && entry.Properties["accountExpires"].Value != null)
                {
                    //Must Add COM Active DS Library in the reference (ActiveDS.TLB)
                    ActiveDs.LargeInteger li = (ActiveDs.LargeInteger)entry.Properties["accountExpires"].Value;
                    Int64 liTicks = li.HighPart * 0x100000000 + li.LowPart;
                    Int64 curTicks = DateTime.Now.Ticks;

                    //List only those account with no expiration/expired in the future
                    if (liTicks == 0 || liTicks > curTicks)
                    {
                        writer.Write(entry.Properties["name"].Value + ";" + entry.Properties["mail"].Value);
                        writer.WriteLine();
                        writer.Flush();
                    }
                }               

            }
            writer.Close();
        }
    }
}

Share and Enjoy

  • Facebook
  • Twitter
  • Google
  • Delicious
  • Digg
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS

 

Possibly Related Posts

 

Incoming Search Term

how to retrieve email addresses from active directory (2), vb net Properties(accountExpires) Value (2), retrieve email address from active directory (2), PropertiesToLoad Add(email) (2), asp net directorysearcher get email (2), retrieve email address from active directory vb (1), retreiving email address from active (1), properties[accountexpires] value (1), net get users email address from ads (1), how to set email id for ad user in asp net (1), retrieve email id from active directory (1), retrieve user name and email from active directory asp (1), set accountexpirationdate = null C# (1), System DirectoryServices SearchResult visual basic 2010 user name (1), username displayed name or email address newbie (1), vb net 2010 directorysearcher find email (1), vb net active directory current user email (1), vb net get active directory name from browser (1), visual basic 2010 directorysearcher (1), how to retrieve users email address in visual basic (1), /wwf/ username displayed name or email address (1), how to retrieve email address from active directory (1), Active directory C# (1), Active Directory ds references (1), active directory retrieve email (1), ad get username and mail (1), ads accountexpirationdate in vb net (1), asp net vb visual studio active directory get e-mail (1), c# get AccountExpirationDate from searchresult (1), directorysearcher get username (1)

Advertise Here

No Comment

Add Your Comment

* Copy this password:

* Type or paste password here: