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";
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.
You can see here under the detail of _ldap
entry :
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.
精彩评论