×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Get user by email address from active directory

Get user by email address from active directory

Get user by email address from active directory

(OP)
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());
}
}
}

}
}

RE: Get user by email address from active directory

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)

RE: Get user by email address from active directory

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; }
}

RE: Get user by email address from active directory

(OP)
"(&(ObjectClass=user) (email=" + email + "))" was it! Thanks a lot. Had to add the & due to .net.

RE: Get user by email address from active directory

(OP)
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");
}
}

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close