-1

I want to pass some IP address, preferably as string how can I do it for domain ?
Using C#. Needed for Directory services.

private Domain domain;
Rathan Naik
  • 993
  • 12
  • 25
  • You cannot. There is no nexus between a network IP address and Microsoft Active Directory. To what does the IP address belong - a workstation in the domain, the domain controller, etc? – Serge Feb 27 '17 at 07:04
  • **It belongs to Domain.** But it has to resolve domain name to IP address. So if i could directly give known IP it must be possible right ? – Rathan Naik Feb 27 '17 at 07:17
  • So you want to get an object's fully qualified domain name (ie `10.2.3.4` => `pc1.mydomain.local`) from the IP address, and then query AD for `mydomain.local`? – Serge Feb 27 '17 at 07:20
  • **I know that method.** But i want more flexibilty. Im using this method DirectoryContext(). Here I want to pass IP.Is there alternate method for IP ? – Rathan Naik Feb 27 '17 at 07:23
  • If I pass IP in place of domain name. Specified domain does not exist error comes. – Rathan Naik Feb 27 '17 at 07:25
  • Of course it does, because IP addresses and domain names are completely different objects. You have to query DNS. Or use some other method to infer the domain name from the IP address. For example, if the IP address refers to a workstation on the domain, you could use WMI to [query the domain to which the workstation belongs](http://stackoverflow.com/questions/5798096/netbios-domain-of-computer-in-powershell). – Serge Feb 27 '17 at 07:28

1 Answers1

0

Domains do not have IP addresses.

Suppose you have an IP address for a workstation that belongs to a domain. You can find out the domain to which the workstatio belongs and then run LDAP queries against it.

Find domain to which a workstation addressed by IP belongs

//using System.Management;

private static string getWorkstationDomain(string workstationName)
{
    ManagementObjectSearcher searcher =
        new ManagementObjectSearcher(@"\\workstationName\root\CIMV2",
        "SELECT * FROM Win32_ComputerSystem");

    var results = searcher.Get();

    var domain = results
        .OfType<ManagementObject>()
        .Select(mo => (string)mo.Properties["Domain"].Value)
        .FirstOrDefault();

    return domain;
}

You can then connect to this domain to run LDAP queries:

DirectoryEntry directoryEntry = new DirectoryEntry("LDAP://" + domain);
Serge
  • 3,986
  • 2
  • 17
  • 37