From 43fc62212110f9f5226293f034acff1c3aa44b28 Mon Sep 17 00:00:00 2001 From: Gary Sharp Date: Thu, 17 Apr 2014 17:37:59 +1000 Subject: [PATCH] Use AD site-servers where possible --- .../ActiveDirectory/Internal/ADInterop.cs | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/Disco.Services/Interop/ActiveDirectory/Internal/ADInterop.cs b/Disco.Services/Interop/ActiveDirectory/Internal/ADInterop.cs index b40ecc8f..98c7b201 100644 --- a/Disco.Services/Interop/ActiveDirectory/Internal/ADInterop.cs +++ b/Disco.Services/Interop/ActiveDirectory/Internal/ADInterop.cs @@ -445,7 +445,19 @@ namespace Disco.Services.Interop.ActiveDirectory.Internal private static IEnumerable SearchDomain(ActiveDirectoryDomain Domain, DomainController DomainController, string SearchRoot, string LdapFilter, int? ResultLimit, string[] LoadProperties) { - string ldapServer = DomainController == null ? Domain.DnsName : DomainController.Name; + string ldapServer; + if (DomainController != null) + { + ldapServer = DomainController.Name; + } + else + { + var domainDC = Site.RetrieveReachableDomainControllers(Domain).FirstOrDefault(); + if (domainDC != null) + ldapServer = domainDC.Name; + else + ldapServer = Domain.DnsName; + } string searchRoot = SearchRoot ?? Domain.DistinguishedName; string ldapPath = string.Format(@"LDAP://{0}/{1}", ldapServer, searchRoot); @@ -551,9 +563,15 @@ namespace Disco.Services.Interop.ActiveDirectory.Internal // Find Domain var domain = GetDomainByDistinguishedName(DistinguishedName); + if (domain == null) + throw new ArgumentException(string.Format("Unknown domain for DistinguishedName: {0}", DistinguishedName), "DistinguishedName"); + + var domainDC = Site.RetrieveReachableDomainControllers(domain).FirstOrDefault(); + var ldapServer = domainDC != null ? domainDC.Name : domain.DnsName; + Domain = domain; - return new DirectoryEntry(string.Format(@"LDAP://{0}/{1}", domain.DnsName, DistinguishedName)); + return new DirectoryEntry(string.Format(@"LDAP://{0}/{1}", ldapServer, DistinguishedName)); } public static DomainController RetrieveWritableDomainController(this ActiveDirectoryDomain domain)