473,396 Members | 1,702 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Enumerating domains in a forest C#

Hi guys,

I've been plugging away at this for a few hours and it's really frustrating me. I have a requirement where I must enumerate domains (and all child domains) in a large forest where I do not know any details about the forest. The solution must be in C#.net 2005. I located a vbscript that worked ok but it must be in C#.

The part I am stuck on is objRootDSE = GetObject("LDAP://RootDSE") which has no equivelent in c#. I'm very firmiliar with System.DirectoryServices but it seems that using that one must specify a ldap path to bind to when creating the connection to the directoryEntry and this requirement dictates that I do not know any details about the forest or even what domain the machine/user is attached to.

I researched using IADs but I really have no clue as to how to functionally use that.
Feb 22 '07 #1
1 4334
Well I had to go about it in a backwards sort of way but ultimately I had to formulate a query to locate the OU that stores the domain information, enumerate, compare and exstrapulate based on that .

So basically, I capture the user's friendly domain name based on their login ID (domain\username) and send that to this function that translates that friendly name into the fqdn which later on I use to get their group memberships

Expand|Select|Wrap|Line Numbers
  1.         private string GetDomainFqdn(string domainName)
  2.         {            
  3.             //Get the forest primary partition
  4.             DirectoryEntry RootDSE = new DirectoryEntry("LDAP://RootDSE");
  5.             DirectoryEntry ConfigContainer = new DirectoryEntry("LDAP://" +
  6.                             RootDSE.Properties["configurationNamingContext"].Value);
  7.             DirectorySearcher ConfigSearcher = new DirectorySearcher(ConfigContainer);
  8.             //Get the distinguishedName for the primary partition
  9.             ConfigSearcher.Filter = "(&(objectClass=crossRefContainer))";
  10.             ConfigSearcher.PropertiesToLoad.Add("distinguishedName");
  11.             ConfigSearcher.SearchScope = SearchScope.OneLevel;
  12.             SearchResult result = ConfigSearcher.FindOne();
  13.             string OUdn = result.Properties["distinguishedName"][0].ToString();
  14.             string ldapPath = string.Empty;
  15.  
  16.             try
  17.             {
  18.                 //Enumerate the primary partition table for domain children
  19.                 string bindGroup = "LDAP://" + OUdn;
  20.                 DirectoryEntry ent = new DirectoryEntry(bindGroup);
  21.  
  22.                 foreach (DirectoryEntry child in ent.Children)
  23.                 {
  24.                     //Match friendly domain name with FQDN domain partition location
  25.                     string ldapMatch = System.Text.RegularExpressions.Regex.Match(child.Path.ToString(),
  26.                             domainName, System.Text.RegularExpressions.RegexOptions.IgnoreCase).ToString();
  27.  
  28.                     if (ldapMatch != "")
  29.                     {
  30.                         //Found a match, fetch ldap domain distinguishedName 
  31.                         ldapPath = child.Properties["nCName"].Value.ToString();
  32.                     }
  33.                     child.Close();
  34.                     child.Dispose();
  35.                 }
  36.                 ent.Close();
  37.                 ent.Dispose();
  38.              }
  39.             catch (DirectoryServicesCOMException e)
  40.             {
  41.                 _error = e.Message.ToString();
  42.             }
  43.  
  44.             return ldapPath;
  45.             this._ldapDomain = ldapPath;
  46.         }
  47.  
Feb 22 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Silver Arrow | last post by:
Iframe ReturnValue returns undefined if domains are not the same I am using a modal window and an iFrame to try and pull a return value back. IMPORTANT : I am doing this across domains. Main...
1
by: Paul Fi | last post by:
I have some confusions about the two, is it possible to create threads inside a particular domain and have other threads created in another domain or do we create threads that will manipulate app...
6
by: Ludvig | last post by:
I have various domains using the same application/assembly They differ in contents and design, based on a "site id", and get its information from an SQL server. Now I have to deploy the...
2
by: Tony | last post by:
I have this problem - I have a hashtable, containing a list of filenames. Every 60 seconds, I have a thread that enumerates thru this hashtable, and based on some simple logic, some of the items...
7
by: Doug | last post by:
An ASP.NET session cookie set on "www.mydomain.com" can not be accessed on "search.mydomain.com"; hence, a new session and cookie are being created on every sub-domain. This is occuring because...
1
by: | last post by:
Can anyone help me on how to enumerate all the domains in a forest with VB.NET. Thanks, Dan Rhoads
2
by: Pucca | last post by:
Hi, Can someone show me an example code of how to get a list of all domains existed in a Forest? -- Thanks.
1
by: manojdagar | last post by:
here now stands that small knot of villages known as the Endians, a mighty forest once stood. Indeed, legand has it that you could have stoodon the edge of the wood and seen it stretch out for miles,...
2
by: sathishc58 | last post by:
Hi All I recently studied about "Forest" Datastructures. Supposed we have two trees. Could not represent diagramatically Tree a ====== 1) 10 is the root node 2) 9 is the
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.