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());
}
}
}
}
}
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
Your filter should have something like:
CODE
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
CODE
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
RE: Get user by email address from active directory
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");
}
}