2

I'm looking for a way to find Active Directory on the network (probably thru DNS/DHCP settings assigned) and then trying to access it, getting all the things like AD servername, BaseDN or domain name from within a code without asking user to provide information about his network. Is there a way to go?

    private  string strADServerName = "SERVERAD";
    private  string strBaseDN = "DC=DOMAIN,DC=COM";
    private  string strAccountFilter = "sAMAccountName";
    private  string domainName = "DOMAIN";
MadBoy
  • 10,824
  • 24
  • 95
  • 156

1 Answers1

2

When you install a Active-Directory you need a Dynamic DNS (DDNS). During his installation AD registers information in the DNS, as you can see in the following capture. On a given Windows computer the WMI class win32_ComputerSystem can provide you the domain name.

enter image description here

You can see here under the detail of _ldap entry :

enter image description here

This SRV entry provide DNS address and port of the domain controllers of your domain. _ldap entry can have multiple values as you've got multiple domain controllers in your domain.

Using NSLOOKUP.EXE tool, you can get this information as shown here under :

> set type=SRV
> _ldap._tcp.dom.fr
Serveur :   UnKnown
Address:  192.168.183.100

_ldap._tcp.dom.fr       SRV service location:
          priority       = 0
          weight         = 100
          port           = 389
          svr hostname   = wm2008r2ent.dom.fr
wm2008r2ent.dom.fr      internet address = 192.168.183.100

If you don't know the domain you can just query for _ldap._tcp like in the sample above :

> _ldap._tcp
Serveur :   UnKnown
Address:  192.168.183.100

_ldap._tcp.dom.fr       SRV service location:
          priority       = 0
          weight         = 100
          port           = 389
          svr hostname   = wm2008r2ent.dom.fr
wm2008r2ent.dom.fr      internet address = 192.168.183.100

On the C# point of view, I am not sure that the class System.Net.DNS allow you to query SRV records. You can find in this codeplex entry DnDNS assembly seems to do it.

HasaniH
  • 8,232
  • 6
  • 41
  • 59
JPBlanc
  • 70,406
  • 17
  • 130
  • 175
  • I know about nslookup method .. although you still have to type _ldap._tcp.dom.fr don't you? so you have to know what you're looking for ? – MadBoy Apr 28 '11 at 06:18
  • win32_ComputerSystem can give you the "dom.fr" part. – JPBlanc Apr 28 '11 at 06:55
  • Will it supply that on non-domain computer as well ? – MadBoy Apr 28 '11 at 07:03
  • Of course not. What do you really want to do ? discover AD on a network from nothing ? – JPBlanc Apr 28 '11 at 07:07
  • Well yes. I have computer which isn't in domain but I need it to be able to discover Active Directory when it's attached to the network. So that in program I will write I won't have to type in the domain name and so on but I could just pick it from choice menu and program would do the rest. Considering that DHCP assings me DNS which is also Active Directory this should be doable. – MadBoy Apr 28 '11 at 07:14
  • with '_ldap._tcp' if the 'DNS suffix for this conexion' is correctly parametered in the IP stack it should be doable. – JPBlanc Apr 28 '11 at 07:32