根据一AD的帐号获另一个AD的帐号, 他们之间通过EmployeeID联系
public class ADManager : IDisposable
{
DirectoryEntry sourceRoot, targetRoot;
public ADManager(string sourcePath, string targetPath)
{
SourcePath = sourcePath;
TargetPath = targetPath;
sourceRoot = new DirectoryEntry(sourcePath.ToUpper());
targetRoot = new DirectoryEntry(targetPath.ToUpper());
}
public string GetLoginID(string sAMAccountName)
{
string employeeID = GetEmployeeID(sAMAccountName, sourceRoot);
string loginID = GetLoginID(employeeID, targetRoot);
return loginID;
}
private string GetEmployeeID(string sAMAccountName, DirectoryEntry sourceRoot)
{
string employeeID = string.Empty;
if (string.IsNullOrEmpty(sAMAccountName))
{
return employeeID;
}
using (DirectorySearcher searcher = new DirectorySearcher(sourceRoot, "(&(objectClass=user)(sAMAccountName=" + sAMAccountName + "))"))
{
searcher.PropertyNamesOnly = true;
searcher.PageSize = 512;
AddStandardUserPropertiesToSearch(searcher);
SearchResult results = searcher.FindOne();
if (results != null)
{
using (DirectoryEntry user = results.GetDirectoryEntry())
{
employeeID = user.Properties["employeeID"][0].ToString();
}
}// end results.Count >0
}
return employeeID;
}
private string GetLoginID(string employeeID, DirectoryEntry targetRoot)
{
string loginID = string.Empty;
if (string.IsNullOrEmpty(employeeID))
{
return loginID;
}
using (DirectorySearcher searcher = new DirectorySearcher(targetRoot, "(&(objectClass=user)(employeeID=" + employeeID + "))"))
{
AddStandardUserPropertiesToSearch(searcher);
//SearchResult results = searcher.FindOne();
//if (results != null)
//{
// using (DirectoryEntry user = results.GetDirectoryEntry())
// {
// employeeID = user.Properties["sAMAccountName"][0].ToString();
// }
//}
using (SearchResultCollection results = searcher.FindAll())
{
List<string> logins = new List<string>();
foreach (SearchResult result in results)
{
using (DirectoryEntry user = result.GetDirectoryEntry())
{
string id = user.Properties["sAMAccountName"][0].ToString();
logins.Add(id);
}
}
if (logins.Count > 0)
{
loginID = logins[0];
}
}
}
return loginID;
}
private static void AddStandardUserPropertiesToSearch(DirectorySearcher search)
{
search.PropertiesToLoad.Add("mail");
search.PropertiesToLoad.Add("sAMAccountName");
search.PropertiesToLoad.Add("employeeID");
search.SearchScope = SearchScope.Subtree;
search.PropertyNamesOnly = true;
search.PageSize = 512;
}
public void Dispose()
{
if (sourceRoot != null)
sourceRoot.Dispose();
if (targetRoot != null)
targetRoot.Dispose();
}
public string SourcePath { private set; get; }
public string TargetPath { private set; get; }
}
public class PrincipalManager
{
PrincipalContext sourceContext, targetContext;
public PrincipalManager(string sourceDomain, string targetDomain)
{
SourceDomain = sourceDomain;
TargetDomain = targetDomain;
sourceContext = new PrincipalContext(ContextType.Domain, sourceDomain.ToUpper());
targetContext = new PrincipalContext(ContextType.Domain, targetDomain.ToUpper());
}
public string GetLoginID(string sAMAccountName)
{
string employeeID = GetEmployeeID(sAMAccountName, sourceContext);
string loginID = GetLoginID(employeeID, targetContext);
return loginID;
}
private string GetEmployeeID(string sAMAccountName, PrincipalContext sourceContext)
{
string employeeID = string.Empty;
if (string.IsNullOrEmpty(sAMAccountName))
{
return employeeID;
}
using (UserPrincipal user = new UserPrincipal(sourceContext))
{
user.SamAccountName = sAMAccountName;
using (PrincipalSearcher pS = new PrincipalSearcher(user))
{
using (Principal results = pS.FindOne())
{
UserPrincipal princial = results as UserPrincipal;
if (princial != null)
{
employeeID = princial.EmployeeId;
}
}
}
}
return employeeID;
}
private string GetLoginID(string employeeID, PrincipalContext targetContext)
{
string loginID = string.Empty;
if (string.IsNullOrEmpty(employeeID))
{
return loginID;
}
using (UserPrincipal user = new UserPrincipal(targetContext))
{
user.EmployeeId=employeeID;
using (PrincipalSearcher pS = new PrincipalSearcher(user))
{
using (PrincipalSearchResult<Principal> results = pS.FindAll())
{
foreach (Principal item in results)
{
UserPrincipal princial = item as UserPrincipal;
if (princial != null)
{
loginID = princial.SamAccountName;
}
}
}
}
}
return loginID;
}
public void Dispose()
{
if (sourceContext != null)
sourceContext.Dispose();
if (targetContext != null)
targetContext.Dispose();
}
public string SourceDomain { private set; get; }
public string TargetDomain { private set; get; }
}
http://blog.csdn.net/shrenk/article/details/40991775
http://www.codeproject.com/Articles/18102/Howto-Almost-Everything-In-Active-Directory-via-C#46
http://macaalay.com/2010/06/28/active-directory-c/