private DirectoryEntry GetNewDirectoryEntry(string dn) { if (_bindingPrefix == null) { _bindingPrefix = "LDAP://" + _context.GetServerName() + "/"; } _pathCracker.Set(dn, NativeComInterfaces.ADS_SETTYPE_DN); string escapedDN = _pathCracker.Retrieve(NativeComInterfaces.ADS_FORMAT_X500_DN); return(Bind(_bindingPrefix + escapedDN, _context.UserName, _context.Password, _context.useServerBind())); }
private void GetSites() { NativeComInterfaces.IAdsPathname pathname = null; pathname = (NativeComInterfaces.IAdsPathname) new NativeComInterfaces.Pathname(); ArrayList propertiesToLoad = new ArrayList(); pathname.EscapedMode = 4; string str = "siteList"; propertiesToLoad.Add(str); ArrayList list2 = (ArrayList)Utils.GetValuesWithRangeRetrieval(this.cachedEntry, "(objectClass=*)", propertiesToLoad, 0)[str.ToLower(CultureInfo.InvariantCulture)]; if (list2 != null) { for (int i = 0; i < list2.Count; i++) { string bstrADsPath = (string)list2[i]; pathname.Set(bstrADsPath, 4); string siteName = pathname.Retrieve(11).Substring(3); ActiveDirectorySite site = new ActiveDirectorySite(this.context, siteName, true); this.sites.Add(site); } } }
public static ActiveDirectorySubnet FindByName(DirectoryContext context, string subnetName) { ValidateArgument(context, subnetName); // work with copy of the context context = new DirectoryContext(context); // bind to the rootdse to get the configurationnamingcontext DirectoryEntry de; try { de = DirectoryEntryManager.GetDirectoryEntry(context, WellKnownDN.RootDSE); string config = (string)PropertyManager.GetPropertyValue(context, de, PropertyManager.ConfigurationNamingContext); string subnetdn = "CN=Subnets,CN=Sites," + config; de = DirectoryEntryManager.GetDirectoryEntry(context, subnetdn); } catch (COMException e) { throw ExceptionHelper.GetExceptionFromCOMException(context, e); } catch (ActiveDirectoryObjectNotFoundException) { // this is the case where the context is a config set and we could not find an ADAM instance in that config set throw new ActiveDirectoryOperationException(Res.GetString(Res.ADAMInstanceNotFoundInConfigSet, context.Name)); } try { ADSearcher adSearcher = new ADSearcher(de, "(&(objectClass=subnet)(objectCategory=subnet)(name=" + Utils.GetEscapedFilterValue(subnetName) + "))", new string[] { "distinguishedName" }, SearchScope.OneLevel, false, /* don't need paged search */ false /* don't need to cache result */); SearchResult srchResult = adSearcher.FindOne(); if (srchResult == null) { // no such subnet object Exception e = new ActiveDirectoryObjectNotFoundException(Res.GetString(Res.DSNotFound), typeof(ActiveDirectorySubnet), subnetName); throw e; } else { string siteName = null; DirectoryEntry connectionEntry = srchResult.GetDirectoryEntry(); // try to get the site that this subnet lives in if (connectionEntry.Properties.Contains("siteObject")) { NativeComInterfaces.IAdsPathname pathCracker = (NativeComInterfaces.IAdsPathname) new NativeComInterfaces.Pathname(); // need to turn off the escaping for name pathCracker.EscapedMode = NativeComInterfaces.ADS_ESCAPEDMODE_OFF_EX; string tmp = (string)connectionEntry.Properties["siteObject"][0]; // escaping manipulation pathCracker.Set(tmp, NativeComInterfaces.ADS_SETTYPE_DN); string rdn = pathCracker.Retrieve(NativeComInterfaces.ADS_FORMAT_LEAF); Debug.Assert(rdn != null && Utils.Compare(rdn, 0, 3, "CN=", 0, 3) == 0); siteName = rdn.Substring(3); } // it is an existing subnet object ActiveDirectorySubnet subnet = null; if (siteName == null) { subnet = new ActiveDirectorySubnet(context, subnetName, null, true); } else { subnet = new ActiveDirectorySubnet(context, subnetName, siteName, true); } subnet.cachedEntry = connectionEntry; return(subnet); } } catch (COMException e) { if (e.ErrorCode == unchecked ((int)0x80072030)) { // object is not found since we cannot even find the container in which to search throw new ActiveDirectoryObjectNotFoundException(Res.GetString(Res.DSNotFound), typeof(ActiveDirectorySubnet), subnetName); } else { throw ExceptionHelper.GetExceptionFromCOMException(context, e); } } finally { if (de != null) { de.Dispose(); } } }
public static ActiveDirectorySubnet FindByName(DirectoryContext context, string subnetName) { DirectoryEntry directoryEntry; ActiveDirectorySubnet activeDirectorySubnet; ActiveDirectorySubnet activeDirectorySubnet1; ActiveDirectorySubnet.ValidateArgument(context, subnetName); context = new DirectoryContext(context); try { directoryEntry = DirectoryEntryManager.GetDirectoryEntry(context, WellKnownDN.RootDSE); string propertyValue = (string)PropertyManager.GetPropertyValue(context, directoryEntry, PropertyManager.ConfigurationNamingContext); string str = string.Concat("CN=Subnets,CN=Sites,", propertyValue); directoryEntry = DirectoryEntryManager.GetDirectoryEntry(context, str); } catch (COMException cOMException1) { COMException cOMException = cOMException1; throw ExceptionHelper.GetExceptionFromCOMException(context, cOMException); } catch (ActiveDirectoryObjectNotFoundException activeDirectoryObjectNotFoundException) { object[] name = new object[1]; name[0] = context.Name; throw new ActiveDirectoryOperationException(Res.GetString("ADAMInstanceNotFoundInConfigSet", name)); } using (directoryEntry) { try { string[] strArrays = new string[1]; strArrays[0] = "distinguishedName"; ADSearcher aDSearcher = new ADSearcher(directoryEntry, string.Concat("(&(objectClass=subnet)(objectCategory=subnet)(name=", Utils.GetEscapedFilterValue(subnetName), "))"), strArrays, SearchScope.OneLevel, false, false); SearchResult searchResult = aDSearcher.FindOne(); if (searchResult != null) { string str1 = null; DirectoryEntry directoryEntry1 = searchResult.GetDirectoryEntry(); if (directoryEntry1.Properties.Contains("siteObject")) { NativeComInterfaces.IAdsPathname pathname = (NativeComInterfaces.IAdsPathname)(new NativeComInterfaces.Pathname()); pathname.EscapedMode = 4; string item = (string)directoryEntry1.Properties["siteObject"][0]; pathname.Set(item, 4); string str2 = pathname.Retrieve(11); str1 = str2.Substring(3); } if (str1 != null) { activeDirectorySubnet = new ActiveDirectorySubnet(context, subnetName, str1, true); } else { activeDirectorySubnet = new ActiveDirectorySubnet(context, subnetName, null, true); } activeDirectorySubnet.cachedEntry = directoryEntry1; activeDirectorySubnet1 = activeDirectorySubnet; } else { Exception exception = new ActiveDirectoryObjectNotFoundException(Res.GetString("DSNotFound"), typeof(ActiveDirectorySubnet), subnetName); throw exception; } } catch (COMException cOMException3) { COMException cOMException2 = cOMException3; if (cOMException2.ErrorCode != -2147016656) { throw ExceptionHelper.GetExceptionFromCOMException(context, cOMException2); } else { throw new ActiveDirectoryObjectNotFoundException(Res.GetString("DSNotFound"), typeof(ActiveDirectorySubnet), subnetName); } } } return(activeDirectorySubnet1); }
public static ActiveDirectorySubnet FindByName(DirectoryContext context, string subnetName) { DirectoryEntry directoryEntry; ActiveDirectorySubnet subnet2; ValidateArgument(context, subnetName); context = new DirectoryContext(context); try { directoryEntry = DirectoryEntryManager.GetDirectoryEntry(context, WellKnownDN.RootDSE); string str = (string)PropertyManager.GetPropertyValue(context, directoryEntry, PropertyManager.ConfigurationNamingContext); string dn = "CN=Subnets,CN=Sites," + str; directoryEntry = DirectoryEntryManager.GetDirectoryEntry(context, dn); } catch (COMException exception) { throw ExceptionHelper.GetExceptionFromCOMException(context, exception); } catch (ActiveDirectoryObjectNotFoundException) { throw new ActiveDirectoryOperationException(Res.GetString("ADAMInstanceNotFoundInConfigSet", new object[] { context.Name })); } try { SearchResult result = new ADSearcher(directoryEntry, "(&(objectClass=subnet)(objectCategory=subnet)(name=" + Utils.GetEscapedFilterValue(subnetName) + "))", new string[] { "distinguishedName" }, SearchScope.OneLevel, false, false).FindOne(); if (result == null) { Exception exception2 = new ActiveDirectoryObjectNotFoundException(Res.GetString("DSNotFound"), typeof(ActiveDirectorySubnet), subnetName); throw exception2; } string siteName = null; DirectoryEntry entry2 = result.GetDirectoryEntry(); if (entry2.Properties.Contains("siteObject")) { NativeComInterfaces.IAdsPathname pathname = (NativeComInterfaces.IAdsPathname) new NativeComInterfaces.Pathname(); pathname.EscapedMode = 4; string bstrADsPath = (string)entry2.Properties["siteObject"][0]; pathname.Set(bstrADsPath, 4); siteName = pathname.Retrieve(11).Substring(3); } ActiveDirectorySubnet subnet = null; if (siteName == null) { subnet = new ActiveDirectorySubnet(context, subnetName, null, true); } else { subnet = new ActiveDirectorySubnet(context, subnetName, siteName, true); } subnet.cachedEntry = entry2; subnet2 = subnet; } catch (COMException exception3) { if (exception3.ErrorCode == -2147016656) { throw new ActiveDirectoryObjectNotFoundException(Res.GetString("DSNotFound"), typeof(ActiveDirectorySubnet), subnetName); } throw ExceptionHelper.GetExceptionFromCOMException(context, exception3); } finally { if (directoryEntry != null) { directoryEntry.Dispose(); } } return(subnet2); }