private static Domain GetDomainObject(string domain) { var key = domain ?? NullKey; if (DomainObjectMap.TryGetValue(key, out var domainObj)) { return(domainObj); } try { if (key == NullKey) { domainObj = Domain.GetCurrentDomain(); } else { var context = new DirectoryContext(DirectoryContextType.Domain, domain); domainObj = Domain.GetDomain(context); } DomainObjectMap.TryAdd(key, domainObj); return(domainObj); } catch { DomainObjectMap.TryAdd(key, null); return(domainObj); } }
/// <summary> /// Gets the name of the forest associate with the domain /// </summary> /// <param name="domain"></param> /// <returns></returns> internal static string GetForestName(string domain = null) { try { if (domain == null) { return(Forest.GetCurrentForest().Name); } var domainObject = Domain.GetDomain(new DirectoryContext(DirectoryContextType.Domain, domain)); return(domainObject.Forest.Name); } catch { return(domain); } }