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