private ArrayList GetSites () { ArrayList arrayLists = new ArrayList (); try { DirectoryEntry directoryEntry = DirectoryEntryManager.GetDirectoryEntry (this.context, this.directoryEntryMgr.ExpandWellKnownDN (WellKnownDN.SitesContainer)); ADSearcher aDSearcher = new ADSearcher (directoryEntry, "(&(objectClass=site))", new string[] { "cn" }, SearchScope.OneLevel); SearchResultCollection searchResultCollections = null; searchResultCollections = aDSearcher.FindAll (); foreach(SearchResult result in searchResultCollections) { var entry = result.GetDirectoryEntry (); var site = new System.DirectoryServices.ActiveDirectory.ActiveDirectorySite(this.context, (string)entry.Properties["cn"].Value, true); arrayLists.Add (site); } } catch (Exception ex) { var msg = ex.Message; } /* IntPtr zero = IntPtr.Zero; IntPtr intPtr = IntPtr.Zero; IntPtr zero1 = IntPtr.Zero; try { this.GetDSHandle(out zero, out intPtr); IntPtr procAddress = UnsafeNativeMethods.GetProcAddress(DirectoryContext.ADHandle, "DsListSitesW"); if (procAddress != (IntPtr)0) { NativeMethods.DsListSites delegateForFunctionPointer = (NativeMethods.DsListSites)Marshal.GetDelegateForFunctionPointer(procAddress, typeof(NativeMethods.DsListSites)); int num = delegateForFunctionPointer(zero, out zero1); if (num != 0) { throw ExceptionHelper.GetExceptionFromErrorCode(num, this.context.GetServerName()); } else { try { DsNameResult dsNameResult = new DsNameResult(); Marshal.PtrToStructure(zero1, dsNameResult); IntPtr intPtr1 = dsNameResult.items; for (int i = 0; i < dsNameResult.itemCount; i++) { DsNameResultItem dsNameResultItem = new DsNameResultItem(); Marshal.PtrToStructure(intPtr1, dsNameResultItem); if (dsNameResultItem.status == 0) { string value = Utils.GetDNComponents(dsNameResultItem.name)[0].Value; arrayLists.Add(new ActiveDirectorySite(this.context, value, true)); } intPtr1 = (IntPtr)((long)intPtr1 + (long)Marshal.SizeOf(dsNameResultItem)); } } finally { if (zero1 != IntPtr.Zero) { procAddress = UnsafeNativeMethods.GetProcAddress(DirectoryContext.ADHandle, "DsFreeNameResultW"); if (procAddress != (IntPtr)0) { UnsafeNativeMethods.DsFreeNameResultW dsFreeNameResultW = (UnsafeNativeMethods.DsFreeNameResultW)Marshal.GetDelegateForFunctionPointer(procAddress, typeof(UnsafeNativeMethods.DsFreeNameResultW)); dsFreeNameResultW(zero1); } else { throw ExceptionHelper.GetExceptionFromErrorCode(Marshal.GetLastWin32Error()); } } } } } else { throw ExceptionHelper.GetExceptionFromErrorCode(Marshal.GetLastWin32Error()); } } finally { if (zero != (IntPtr)0) { Utils.FreeDSHandle(zero, DirectoryContext.ADHandle); } if (intPtr != (IntPtr)0) { Utils.FreeAuthIdentity(intPtr, DirectoryContext.ADHandle); } } */ return arrayLists; }
private string DoAdLookups(bool bDoPosts) { string sRet = string.Empty; bool bFail = false; string sMailbox = txtTargetMailbox.Text.Trim(); StringBuilder oSB = new StringBuilder(); DirectoryEntry oRootDSE = null; DirectoryEntry oQuery = null; DirectorySearcher oDirectorySearcher = null; SearchResultCollection oSearchResultCollection = null; //string scpUrl = null; //const string ScpUrlGuidString = @"77378F46-2C66-4aa9-A6A6-3E7A48B19596"; this.Cursor = Cursors.WaitCursor; oSB.AppendFormat("- DoAdLookups - Find SCP records: {0}\r\n", DateTime.Now); //string sPostUrl = "https://autodiscover-s.outlook.com/autodiscover/autodiscover.svc"; // #1 Get AD entries try { oSB.AppendFormat(" Start inital binding to LDAP://rootDSE\r\n"); oRootDSE = new DirectoryEntry("LDAP://rootDSE"); oSB.AppendFormat(" Finished binding to LDAP://rootDSE\r\n"); oSB.AppendFormat(" Obtaining configurationNamingContext - if it fails then Autodiscover should be used.\r\n"); string ConfigurationNamingContext = (string)oRootDSE.Properties["configurationNamingContext"].Value; oSB.AppendFormat(" configurationNamingContext {0}\r\n", ConfigurationNamingContext); string sQuery = "LDAP://" + ConfigurationNamingContext; oSB.AppendFormat(" Query: {0}\r\n", sQuery); string sFilter = "(&(objectcategory=serviceConnectionPoint)(|(keywords=67661D7F-8FC4-4fa7-BFAC-E1D7794C1F68)( keywords=77378F46-2C66-4aa9-A6A6-3E7A48B19596)))"; oSB.AppendFormat(" Filter: {0}\r\n", sFilter); oQuery = new DirectoryEntry(sQuery); oDirectorySearcher = new DirectorySearcher(oQuery); oDirectorySearcher.PropertiesToLoad.Add("cn"); oDirectorySearcher.PropertiesToLoad.Add("name"); oDirectorySearcher.PropertiesToLoad.Add("serviceBindingInformation"); oDirectorySearcher.PropertiesToLoad.Add("legacyExchangeDN"); oDirectorySearcher.PropertiesToLoad.Add("Domain"); oDirectorySearcher.PropertiesToLoad.Add("site"); oDirectorySearcher.PropertiesToLoad.Add("keywords"); oDirectorySearcher.Filter = sFilter; oSB.AppendFormat("\r\n"); oSB.AppendFormat("+ Searching AD for CAS URLs: {0}\r\n", DateTime.Now); DateTime oStart = DateTime.Now; oSearchResultCollection = oDirectorySearcher.FindAll(); TimeSpan oTimeSpan = DateTime.Now.Subtract(oStart); oSB.AppendFormat(" Time to do Search: {0} miliseconds\r\n", oTimeSpan.TotalMilliseconds.ToString()); oSB.AppendFormat("- Searching AD for CAS URLs: {0}\r\n", DateTime.Now); } catch (System.Runtime.InteropServices.COMException) { bFail = true; System.Runtime.InteropServices.COMException COMExceptionEx = new System.Runtime.InteropServices.COMException(); Console.WriteLine(COMExceptionEx.Message); oSB.AppendFormat("!! Error: "); oSB.AppendFormat(" Message: {0}\r\n", COMExceptionEx.Message); oSB.AppendFormat(" InnerException: {0}\r\n", COMExceptionEx.InnerException); oSB.AppendFormat(""); oSB.AppendFormat(" StackTrace: {0}\r\n", COMExceptionEx.StackTrace); oSB.AppendFormat("- Searching: {0}\r\n", DateTime.Now); } catch (InvalidOperationException) { bFail = true; InvalidOperationException InvOpEx = new InvalidOperationException(); Console.WriteLine(InvOpEx.Message); oSB.AppendFormat("!! Error: "); oSB.AppendFormat(" Message: {0}\r\n", InvOpEx.Message); oSB.AppendFormat(" InnerException: {0}\r\n", InvOpEx.InnerException); oSB.AppendFormat(""); oSB.AppendFormat(" StackTrace: {0}\r\n", InvOpEx.StackTrace); oSB.AppendFormat("- Searching: {0}\r\n", DateTime.Now); } catch (NotSupportedException) { bFail = true; NotSupportedException NotSuppEx = new NotSupportedException(); Console.WriteLine(NotSuppEx.Message); oSB.AppendFormat("!! Error: "); oSB.AppendFormat(" Message: {0}\r\n", NotSuppEx.Message); oSB.AppendFormat(" InnerException: {0}\r\n", NotSuppEx.InnerException); oSB.AppendFormat(""); oSB.AppendFormat(" StackTrace: {0}\r\n", NotSuppEx.StackTrace); oSB.AppendFormat("- Searching: {0}\r\n", DateTime.Now); } catch (Exception ex) { bFail = true; Console.WriteLine(ex.Message); oSB.AppendFormat("!! Error: "); oSB.AppendFormat(" Message: {0}\r\n", ex.Message); oSB.AppendFormat(" InnerException: {0}\r\n", ex.InnerException); oSB.AppendFormat(""); oSB.AppendFormat(" StackTrace: {0}\r\n", ex.StackTrace); oSB.AppendFormat("- Searching: {0}\r\n", DateTime.Now); } System.DirectoryServices.ActiveDirectory.ActiveDirectorySite site = System.DirectoryServices.ActiveDirectory.ActiveDirectorySite.GetComputerSite(); string computerSiteName = site.Name; oSB.AppendFormat("Site Name: " + computerSiteName + "\r\n"); if (bFail != true) { oSB.AppendFormat("\r\n"); oSB.AppendFormat(" Results found: {0} URLs\r\n", oSearchResultCollection.Count.ToString()); oSB.AppendFormat("+ List Results: {0}\r\n", DateTime.Now); oSB.AppendFormat("\r\n"); string sUrl = string.Empty; try { foreach (SearchResult oSearchResult in oSearchResultCollection) { oSB.AppendFormat(" Result Path: {0}\r\n", oSearchResult.Path); if (oSearchResult.Properties["cn"].Count != 0) { oSB.AppendFormat(" cn: {0}\r\n", oSearchResult.Properties["cn"][0].ToString()); } if (oSearchResult.Properties["name"].Count != 0) { oSB.AppendFormat(" name: {0}\r\n", oSearchResult.Properties["name"][0].ToString()); } if (oSearchResult.Properties["serviceBindingInformation"].Count != 0) { oSB.AppendFormat(" serviceBindingInformation: {0}\r\n", oSearchResult.Properties["serviceBindingInformation"][0].ToString()); } if (oSearchResult.Properties["legacyExchangeDN"].Count != 0) { oSB.AppendFormat(" legacyExchangeDN: {0}\r\n", oSearchResult.Properties["legacyExchangeDN"][0].ToString()); } if (oSearchResult.Properties["Domain"].Count != 0) { oSB.AppendFormat(" Domain: {0}\r\n", oSearchResult.Properties["Domain"][0].ToString()); } if (oSearchResult.Properties["site"].Count != 0) { oSB.AppendFormat(" site: {0}\r\n", oSearchResult.Properties["site"][0].ToString()); } //if (oSearchResult.Properties["keywords"].Count != 0) //{ // if (!string.IsNullOrEmpty(computerSiteName)) // { // // Search for SCP entries. // string sitePrefix = "Site="; // string siteMatch = sitePrefix + computerSiteName; // System.Collections.Generic.List<string> scpListNoSiteMatch = new System.Collections.Generic.List<string>(); // System.DirectoryServices.ResultPropertyValueCollection entryKeywords = oSearchResult.Properties["keywords"]; // // Identify SCP URLs. // if (AppHelpers.CollectionHelpers.CollectionContains(entryKeywords, ScpUrlGuidString)) // { // // Get the SCP URL. // scpUrl = oSearchResult.Properties["serviceBindingInformation"][0] as string; // // If the SCP URL matches the exact ComputerSiteName. // if (CollectionHelpers.CollectionContains(entryKeywords, siteMatch)) // { // // Priority 1 SCP URL. Add SCP URL to the list if it's not already there. // oSB.Append(" Priority 1 type. SCP URL matches the exact ComputerSiteName. Top of the list.\r\n"); // oSB.Append(" Path: " + oSearchResult.Path + " \r\n"); // oSB.Append(" SCP URL: " + scpUrl + "\r\n"); // //scpUrlList.Add(scpUrl); // } // } // // No match between the SCP URL and the ComputerSiteName // else // { // bool hasSiteKeyword = false; // // Check if SCP URL entry has any keyword starting with "Site=" // foreach (string keyword in entryKeywords) // { // hasSiteKeyword |= keyword.StartsWith(sitePrefix, System.StringComparison.InvariantCultureIgnoreCase); // } // // Add SCP URL to the scpListNoSiteMatch list if it's not already there. // if (!CollectionHelpers.CollectionContains(scpListNoSiteMatch, scpUrl)) // { // // Priority 2 SCP URL. SCP entry doesn't have any "Site=<otherSite>" keywords, insert at the top of list. // if (!hasSiteKeyword) // { // oSB.Append(" Priority 2 type. SCP entry doesn't have any \"Site=<otherSite>\" keywords\r\n"); // oSB.Append(" Path: " + oSearchResult.Path + " \r\n"); // oSB.Append(" SCP URL: " + scpUrl + "\r\n"); // scpListNoSiteMatch.Insert(0, scpUrl); // } // // Priority 3 SCP URL. SCP entry has at least one "Site=<otherSite>" keyword, add to the end of list. // else // { // oSB.Append(" Priority 3 type. SCP entry has at least one \"Site=<otherSite>\" keyword. This should be at the end of the list.\r\n"); // oSB.Append(" Path: " + oSearchResult.Path + " \r\n"); // oSB.Append(" SCP URL: " + scpUrl + "\r\n"); // scpListNoSiteMatch.Insert(0, scpUrl); // } // } // } // } //} int iTimeoutSeconds = Int32.Parse(cmboTimeout.Text.Trim()); if (bDoPosts == true) { oSB.Append("\r\n"); sUrl = oSearchResult.Properties["serviceBindingInformation"][0].ToString(); if (sUrl.StartsWith("LDAP:")) { oSB.AppendFormat(" ! Skipping POST due to URL starting with LDAP\r\n"); } else { oSB.Append(DoPostToUrl(sUrl, sMailbox, iTimeoutSeconds)); } } oSB.AppendFormat("\r\n"); oSB.AppendFormat("--------\r\n"); oSB.AppendFormat("\r\n"); } oSB.AppendFormat("- List Results: {0}\r\n", DateTime.Now); } catch (System.Runtime.InteropServices.COMException) { bFail = true; System.Runtime.InteropServices.COMException COMExceptionEx = new System.Runtime.InteropServices.COMException(); Console.WriteLine(COMExceptionEx); oSB.AppendFormat("!! Error"); oSB.AppendFormat(" Message: {0}\r\n", COMExceptionEx.Message); oSB.AppendFormat(" InnerException: {0}\r\n", COMExceptionEx.InnerException); oSB.AppendFormat("\r\n"); oSB.AppendFormat(" StackTrace: {0}\r\n", COMExceptionEx.StackTrace); oSB.AppendFormat("- List Results: {0}\r\n", DateTime.Now); } catch (InvalidOperationException) { bFail = true; InvalidOperationException InvOpEx = new InvalidOperationException(); Console.WriteLine(InvOpEx.Message); oSB.AppendFormat("!! Error"); oSB.AppendFormat(" Message: {0}\r\n", InvOpEx.Message); oSB.AppendFormat(" InnerException: {0}\r\n", InvOpEx.InnerException); oSB.AppendFormat("\r\n"); oSB.AppendFormat(" StackTrace: {0}\r\n", InvOpEx.StackTrace); oSB.AppendFormat("- List Results: {0}\r\n", DateTime.Now); } catch (NotSupportedException) { bFail = true; NotSupportedException NotSuppEx = new NotSupportedException(); Console.WriteLine(NotSuppEx.Message); oSB.AppendFormat("!! Error"); oSB.AppendFormat(" Message: {0}\r\n", NotSuppEx.Message); oSB.AppendFormat(" InnerException: {0}\r\n", NotSuppEx.InnerException); oSB.AppendFormat("\r\n"); oSB.AppendFormat(" StackTrace: {0}\r\n", NotSuppEx.StackTrace); oSB.AppendFormat("- List Results: {0}\r\n", DateTime.Now); } } oSB.AppendFormat(""); oSB.AppendFormat("- DoAdLookups - Find SCP records: {0}\r\n", DateTime.Now); sRet = oSB.ToString(); this.Cursor = Cursors.Default; return(sRet); // #2 Do test posts. // //[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true } }