Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get user by email address from active directory

Status
Not open for further replies.

sirnose1

Programmer
Nov 1, 2005
133
0
16
US
I am trying to write some code which will search a a domain forest and find a user by thier email address. I can get it to find and loop through all the domains in the forest, but i cant get the results. Program says that the person is in all domains. Thier is an issue with my code. Please advise. Thanks!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.DirectoryServices;
using System.DirectoryServices.ActiveDirectory;
using System.Diagnostics;

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
var allDomains = Forest.GetCurrentForest().Domains.Cast<Domain>();
int x = 0;


foreach (Domain d in allDomains)
{
string email = "someone@somewhere.com";
var searcher = new DirectorySearcher(new DirectoryEntry("LDAP://" + d.Name));
searcher.Filter = "(ObjectClass=user)" ;
SearchResult result = searcher.FindOne();
ResultPropertyCollection myResult;

if (result != null)
{
myResult = result.Properties;
x++;
Console.WriteLine("Found it!" + email + "is on domain " + d.ToString());
Console.WriteLine("Display Name : " + myResult.PropertyNames.ToString());
}
}
}

}
}
 
You're not actually "filtering". You just grabbed the whole domain and validated that there was AN entry.

Your filter should have something like:
Code:
searcher.Filter = "(ObjectClass=user) (email=" + email + ")";

Lodlaiden

You've got questions and source code. We want both!
Oh? That? That's not an important password. - IT Security Admin (pw on whiteboard)
 
I did this:
Code:
var allDomains = Forest.GetCurrentForest().Domains.Cast<Domain>();
int x = 0;

bool breaking = false;
foreach (Domain d in allDomains)
{
	string email = "", displayName = "", samaccountname  = "";
    var searcher = new DirectorySearcher(new DirectoryEntry("LDAP://" + d.Name));
    searcher.Filter = "(ObjectClass=user)";
    SearchResultCollection result = searcher.FindAll();

    foreach (SearchResult sr in result)
    {
		if (sr.Properties["mail"].Count > 0)
        {
			if (sr.Properties["mail"][0].ToString().ToLower().Equals("emailaddress@domain.com"))
            {
				samaccountname = sr.Properties["samaccountname"][0].ToString();
                email = sr.Properties["mail"][0].ToString();
				displayName = sr.Properties["displayname"][0].ToString();

                Console.WriteLine("Found it! " + email + " is on domain " + d.ToString());
                Console.WriteLine("Display Name : " + displayName);
                Console.WriteLine("Account Name: " + samaccountname);

                breaking = true;
                break;
			}
		}
	}
	searcher = null;
    if (breaking) { break; }
}
 
(&(ObjectClass=user) (email=" + email + "))" was it! Thanks a lot. Had to add the & due to .net.
 
The code finds the users sometimes but not all the time, even if it is the same user. It seems like it times out. I put a couple of threads into the code to slow it down, but to no evail. I noticed that It wont find users at all located on the west coast. I heard something about "paging". Is this a network issue? I can't seem to narrow it down.

static void Main(string[] args)
{
var allDomains = Forest.GetCurrentForest().Domains.Cast<Domain>();
int x = 0;
Console.Write("Enter an email address: ");
string email = Console.ReadLine();

foreach (Domain d in allDomains)
{
Thread.Sleep(500);
var searcher = new DirectorySearcher(new DirectoryEntry("LDAP://" + d.Name));
searcher.Filter = "(&(ObjectClass=user)(mail=" + email + "))";
SearchResult result = searcher.FindOne();
ResultPropertyCollection myResult;

//If found in Active Directory
if (result != null)
{
Thread.Sleep(500);
myResult = result.Properties;
x++;
string ADName = result.Properties["name"][0].ToString();
string[] splitADName = ADName.Split(',');
string firstName = splitADName[1];
string lastName = splitADName[0];
Console.WriteLine("Found " + firstName + " " + lastName + " is on domain " + d.Name);
}
}
if (x == 0)
{
email = "";
Console.WriteLine("Not Here");
}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top